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'}