Best way to check change in state?

Hey all

I have 2 rules set up on the same switch. One for when the state is updated (regardless of change), and one for when the state changes. However they both do things the other relies on (setting other switches etc) so it can get out of sync.

So I want to consolidate the script: whenever the switch receives a command and if it has changed, do something. If it has not changed, do something else.

I’m using blockly (and would prefer to continue to do so) and still quite new to the ecosystem, so just not sure exactly what blocks to use to make the comparison between the previous and new value. I saw some persistence ones but I’m not sure they are what I want. There were also context ones which look promising but I haven’t found them again.

Would love and appreciate any suggestions!

Using 4.2

You are correct that the context variables are the way to go. You can find them again in the Run and Process group in the contextual info block’s dropdown menu:
image

image

Thanks! Both new state and previous state are coming up as undefined. Do I have to get the context from somewhere?

When the rule was triggered by a Received command trigger, you will not get a previous state or new state data, because it’s irrelevant in this instance.

You will get received command data instead, because at the time of the trigger, that’s all the system knows and is telling you about.

Those previous state and new state context / data is available on a item changed state trigger event.

So one way to fix your script is to change your trigger from received command to changed as shown here:

If you want another rule when the state is updated (regardless of change), you’d use the was updated trigger. In this case, only new state is available. There’s no previous state context for an updated event.

For completeness:

  • You can have one rule with multiple triggers, and the triggers can be of different types. It’s still the same situation though:
  • When your rule got triggered by an actual change event, you’ll get previous + new state info.
  • When your rule got triggered by an update event, you’ll only get new state info
  • When your rule got triggered by a received command event, you’ll get what command was received? info
    and so on. There are all sorts of events and their corresponding / relevant information for the event. You can see the list here: JSR223 Scripting | openHAB

Sure! I’m keeping it simple right now.
The triggers:
image

And the “code”:
image

Thank you

Your trigger and your context information do not match.

Your trigger is “received a command” so the context information available is the command that has been received. If you want to have the state context information then you need one of the triggers that relates to an item state: “was updated” or “changed”.

Yup. That would make sense, thank you. I will give it a try on update!

oops I edited my post above instead of making a new reply.

That’s okay, I got it!

I’m hoping I can work with it. At the moment I have separate change/update events but I only want the update event to fire after the change event has been run (which can take 15 seconds), so am hoping I can get them in the same rule to control for that.

In my experience things can usually be simplified rather than trying to rely on really specific timing back and forth issues like this. I think you might be able to accomplish what you want with these updates.

  • Modify the item so that it doesn’t automatically update its state when a command is received. This will keep the state the same when you send a command until you choose to change it.
  • Keep your command received rule the same, but add another step after your inline script to post an update to the item. This takes care of the timing issues if I understand your desire correctly.
  • Run a separate command on updated state like described above.
  • You could also potentially put a “from” and “to” in your state change trigger or “command” in your received command trigger which would allow you to split out the commands into all the different bits. Something to consider, but up to you if it’s useful or not.

Hope that helps!

Thanks Jonny. I couldn’t get #1 and #2 working as well as I’d like. I was able to simplify my solution a bit but it’s pretty janky, It works though so I’m not really game to optimise it!