good day all I am trying to figure out how to calculate the daily total people coming into a store. i have a people counter that gives a period total and then accumulative total. I am wanting to create a attribute that does a daily total. is there anyone that can help me with this.
An explanation of the JSON-, Groovy- and Flow rules can be found in the documentation:
.
If you want to do simple value calculations, Flow is probably your best choice.
Fun fact: we’ll soon add more processors like MIN, AVG and MAX.
@martin.peeters thanks I have gone through the document and I am thinking it would need to be a groovy rule for what I need to do. But I am quite confused how to formulate the groovy rule hence I am asking for help.
You could take a look at our documentation on Groovy rules.
There are also some examples, and guidance on how they work.
.
You can also search through the forum, as there are many topics regarding Groovy rules.
Hopefully this helps
@martin.peeters thanks will go through this and see if I can grasp it.
this is the thing asset I have
I am trying to get a daily total count so i have created the attribute.
this is the groovy rule i have so far with the help of chatGPT there is a issue with it that I am trying to find
package org.openremote.setup.integration.rules
import org.openremote.manager.rules.RulesBuilder
import org.openremote.manager.rules.RulesFacts
import org.openremote.model.asset.impl.ThingAsset
import org.openremote.model.query.AssetQuery
import org.openremote.model.rules.AssetState
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import java.util.logging.Logger
Logger LOG = binding.LOG
RulesBuilder rules = binding.rules
// Define time format for daily tracking
final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd")
// Rule to track daily total and reset at midnight
rules.add()
.name("Daily People Counter Total")
.when({ facts ->
def now = LocalDateTime.now()
def formattedDate = now.format(DATE_FORMAT)
// Get the people counter asset
def peopleCounter = facts.matchFirstAssetState(
new AssetQuery().types(ThingAsset).attributeValue("name", "People Counter")
)
if (peopleCounter.isPresent()) {
def totalIn = facts.matchFirstAssetState(
new AssetQuery().ids(peopleCounter.get().id).attributeName("Total_in")
).map { it.value.orElse(0) }.orElse(0)
// Get stored daily total
def dailyTotal = facts.matchFirstAssetState(
new AssetQuery().ids(peopleCounter.get().id).attributeName("dailyTotal")
).map { it.value.orElse(0) }.orElse(0)
// Get stored date to track if the day changed
def lastRecordedDate = facts.matchFirstAssetState(
new AssetQuery().ids(peopleCounter.get().id).attributeName("lastRecordedDate")
).map { it.value.orElse("") }.orElse("")
// Check if the date has changed (reset needed)
if (!lastRecordedDate.equals(formattedDate)) {
facts.bind("resetNeeded", true)
facts.bind("peopleCounterId", peopleCounter.get().id)
} else {
facts.bind("resetNeeded", false)
}
// Bind current total and daily total
facts.bind("totalIn", totalIn)
facts.bind("dailyTotal", dailyTotal)
facts.bind("currentDate", formattedDate)
}
return peopleCounter.isPresent()
})
.then({ facts ->
def resetNeeded = facts.bound("resetNeeded")
def peopleCounterId = facts.bound("peopleCounterId")
def totalIn = facts.bound("totalIn")
def dailyTotal = facts.bound("dailyTotal")
def currentDate = facts.bound("currentDate")
if (resetNeeded) {
LOG.info("Resetting daily total for People Counter at midnight.")
facts.updateAssetState(peopleCounterId, "dailyTotal", totalIn)
facts.updateAssetState(peopleCounterId, "lastRecordedDate", currentDate)
facts.updateAssetState(peopleCounterId, "Total_in", 0) // Reset the counter
} else {
LOG.info("Updating daily total for People Counter.")
facts.updateAssetState(peopleCounterId, "dailyTotal", dailyTotal + totalIn)
}
})
this is a message i am getting in the container manager
2025-01-22 09:00:57 2025-01-22 08:00:57.908 INFO [ContainerExecutor-17072 ] RulesEngine.RealmEngine-intellisec.RULES : Starting: RulesEngineId{scope=RealmRuleset, realm='intellisec', assetId='null'}