"This triggers the rule if an item state has changed" - Problem with previous state "zero"

I want to trigger an action when the item state changes, but only if the previous state was 0.

Without the “previous state” condition, the rule triggers on every item change. However, with the additional condition, the rule no longer triggers.

I’ve tried the following values for zero:

  • 0
  • 0.0
  • "0.0 mm/h"

    I suspect the issue lies in determining the correct representation of zero, but I’m unsure what value I need to use.

Here is the item configuration:

Get rid of the quotes.

0 mm/h

But beware that floating point numbers are almost never exactly 0 and that 0.0 mm/h is likely rounded. The Item’s state could be 0.04321 mm/h which is not 0 but it works appear in the UI as 0. mm/h.

If this is in fact the case, you might need to use a rule inline script condition to test if the previous state is less than 0.5 mm/h or something like that.

@rlkoshak
You’re absolutely right, comparing a float against zero isn’t the best approach.

now I tried it:

        const previousState = event.oldState;
        console.log("Previous State: " + previousState);   

and it resulted in: :frowning:
event.oldState === undefined

Is using event.oldState incorrect?

See JavaScript Scripting - Automation | openHAB.

It’s event.oldItemState.

1 Like

You are right… to defend myself, I can say that the code was autogenerated by ChatGPT :wink:

And this is why I’m really anti-chatbots for OH Rules generation. I’ve yet to see code generated by a chatbot, even which a really good promptly that wasn’t subtly wrong in ways you’d have to basically be an expert in OH rules to discover and fix. Which a poor prompt the code generated is often almost meaningless.

It’s an anti-tool, ultimately waisting more time than if you’d just coded the rule yourself.

They are getting better but still a long way from usable for OH rules.