Sensor status rule with Timestamp

Im trying to make a rule which change the value of an status attribute from “Connected” to “Disconnected” when the last update of the attribute is 5min or longer. Where can i start reading? Thanks!

Hi,

It sounds like RULE_EVENT configuration item is what you want, info about it can be found in the code comments:

RULE_EVENT configuration item

You can use that in combination with RULE_EVENT_EXPIRES with a value of PT5M this will mean that any values written to the attribute will be stored in the rules engine for 5 minutes.

In your rule when condition you can then use facts.matchAssetEvent() to see if any asset events exist; if they don’t then you know the attribute hasn’t been updated for ~5 mins something like below (not tested):

facts.matchAssetEvent(
   new AssetQuery()
      .ids(assetId)
      .attributes(attributeName)
).count() == 0

Hi Rich, i add the items “Rule Event” and “Rule Event Expires” to the attribute. This is the code:

package demo.rules

import org.openremote.manager.rules.RulesBuilder
import org.openremote.model.asset.*
import org.openremote.model.attribute.*
import org.openremote.model.value.*
import org.openremote.model.rules.*
import java.util.logging.*
import org.openremote.model.query.*


Logger LOG = binding.LOG
RulesBuilder rules = binding.rules
Assets assets = binding.assets

rules.add()
        .name("Estado Parq")
        .when(
            {facts ->
                return !facts.matchAssetEvent(
                  new AssetQuery()
                    .ids("4Vscn06hs73Hm7IaZ3rMNW")
                    .attributes("estadoParq")
                  ).count() == 0

        })
        .then(
            {facts ->
            assets.dispatch(new AttributeEvent("4Vscn06hs73Hm7IaZ3rMNW", "estadoParq", "Desconectado"))
        })

I get an error, the log:

At least one rules engine is in an error state, skipping: AssetState{id='4Vscn06hs73Hm7IaZ3rMNW', name='Parquimetro Altec', parentName='null', type='ThingAsset', attributeName='estadoParq', attributeValueDescriptor=ValueDescriptor{name='text', type=class java.lang.String, arrayDimensions=null, constraints=[], format=null, units=null}, value=Optional[Conectado], timestamp=1635765627746, oldValue=Optional[Conectado], oldValueTimestamp=1635765627066, source=ATTRIBUTE_LINKING_SERVICE}

No more information about the error. I delete the “Rule State” item from the attribute, if that helps!

On RulesEngine{id='RulesEngineId{scope=TenantRuleset, realm='master', assetId='null'}', running='true', deployments='[RulesetDeployment{id=1770, name='estado_parq', version=46, status=DEPLOYED}]'}, error executing rules of: RulesetDeployment{id=1770, name='estado_parq', version=46, status=DEPLOYED} -- Error evaluating condition of rule 'Estado Parq': No signature of method: org.openremote.model.query.AssetQuery.attributes() is applicable for argument types: (String) values: [estadoParq]
Possible solutions: attributes([Lorg.openremote.model.query.filter.AttributePredicate;), attributes(org.openremote.model.query.LogicGroup), attributeName(java.lang.String), attributeNames([Ljava.lang.String;), attributeValue(java.lang.String)

Hi,

As the log entry shows the problem is with your AssetQuery call to attributes(); if you look at the source code you can see the attributes() method takes attribute predicates not strings, you can use attributeName() instead.

Hi Rich,
thanks for the reply. The rule is working!
Best regards