Cant get Virtual Sensor to work

Hi everyone, im trying to generate a virtual sensor via Groovy rule. I tried @Rich example by coping the code to a new groovy rule with no results. My rule is:

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.*

Logger LOG = binding.LOG
RulesBuilder rules = binding.rules
Assets assets = binding.assets
String UPDATE_FREQUENCY = "5s" 

rules.add()
        .name("Virtual sensor")
        .when(
            {facts ->
                return !facts.matchFirst(String, {it == "VirtualSensor"}).isPresent()
        })
        .then(
            {facts ->
            facts.putTemporary(UPDATE_FREQUENCY, "VirtualSensor")
            assets.dispatch(new AttributeEvent("3hpRsBhj90efJCz3XqhghM", "estadoParq", Values.create("Disconected"))) 
        })

i added to the attribute estadoParq the item “Rule State”.

Any help is appreciated!

The obvious problem is in the then, Values class and therefore Values.create() no longer exists and now you just supply the value directly; so if it’s a text/string attribute you just need:

assets.dispatch(new AttributeEvent("3hpRsBhj90efJCz3XqhghM", "estadoParq", "Disconected"))

FYI: The facts object that is passed into the when closure is of type RulesFacts, you can explore the classes to figure out what is possible from rules, obviously with the full scripting capabilities of groovy there’s no limitations to what can be achieved.

AttributeEvent can also be explored in the repo.

ATB,

Rich

FYI: If you go to the logs page in the Manager UI you should see some indication of the problem with your rule.

Thanks for the reply Rich, i change Values.create() as you said but the rule is not working. This is the new log:

10/28/2021 13:54:33 	INFO 	RULES 	Rules 	Starting: RulesEngine{id='RulesEngineId{scope=TenantRuleset, realm='master', assetId='null'}', running='false', deployments='[RulesetDeployment{id=1151, name='Virtual Sensor', version=31, status=READY}]'}
10/28/2021 13:54:33 	INFO 	RULES 	Rules 	Compiling ruleset deployment: TenantRuleset{id='1151', version='31', name='Virtual Sensor', lang='GROOVY', createdOn='Thu Oct 28 13:51:29 CEST 2021', lastModified='Thu Oct 28 15:54:33 CEST 2021', enabled='true', meta='{}', realm='master', accessPublicRead='false'}
10/28/2021 13:54:33 	INFO 	RULES 	Rules 	Stopping: RulesEngine{id='RulesEngineId{scope=TenantRuleset, realm='master', assetId='null'}', running='true', deployments='[RulesetDeployment{id=1151, name='Virtual Sensor', version=30, status=DEPLOYED}]'}

Thanks!

I missed the other problem with your rule which relates to the UPDATE_FREQUENCY if using the string overload of putTemporary then the string must be a valid ISO8601 Duriation (i.e. PT5S).

Lots has changed in the platform since you were able to supply 5s as a duration.

FYI: You can always look at the docker logs for additional information as the Manager UI logs only show limited information

docker logs openremote-manager-1

Thanks Rich, the virtual sensor is now working! i would like to check other sensor status, the idea is if the sensor does not report after 5 min, the attribute value goes to disconnected. Is there a way to do that?

when(  {facts -> "ATTRIBUTE NOT UPDATED SINCE x TIME"} )
then( {facts -> "CHANGE ATTRIBUTE VALUE TO DISCONNECTED"} )