Triggering item vs. cron


I am using sometimes cron in combination with item changes like this:

rule "Presence state monitoring"
Item Presence changed or
Time cron "22 0/10 * * * ?"
if(previousState == NULL) return; // failing fast
//do stuff...

I would like to avoid this beeing triggered by the item presence during startup (e.g. NULL -> ON).
does this if(previousState help in this regard?

use return without “;”

use item.previousState.state
but, souldn’t you use Presence.state == NULL

I think you have to use the ; after return. This being one of the few exceptions in the DSL

“return;” ist correct

i use this a lot in rules where just one item change triggers the rule and wanted to keep it simple, but you are right I could simply use:

if(Presence.previousState == NULL) return;

Actually this doesn’t work:
Let’s assume Presence changes from NULL to ON. the rule will return accordingly.
If it’s triggered later by the cron, previousState == NULL ist still true.
–> this does not work

I guess this should be working, right?

    if( == "Presence" && Presence.previousState == NULL) return;

When the Rule is triggered by the cron trigger previousState should be null, not NULL so the comparison should work. However, where this might not work is if previousState doesn’t even exist when the Rule is triggered by the cron trigger. In that case you might get an error saying previousState doesn’t exist.

I don’t think this will work because I don’t think that NULL gets saved to persistence and the previousState method will only return the most recent value in the database. Therefore this condition will always return false.

Also, you would need to use Presence.previousState.state == NULL because previousState returns a HistoricItem, not just the State.

You can easily handle this through the Rule trigger itself though

    Item Presence changed from ON to OFF or
    Item Presence changed from OFF to ON or
    Time cron "22 0/10 * * * ?"

Then the Rule will never be triggered on a change from NULL and you don’t need the test in the Rule anymore.

You are absolutely right.
Thanks, I will check your detailed response tomorrow. It’s late… :wink: