Don't execute rule if previous value was NULL

If have a simple rule which sends a mail if an item is changed

rule "Check item"
when
    Item Item_tobe_Checked changed
then
    val mailActions = getActions("mail","mail:smtp:general")
    mailActions.sendMail("xxx@test.com", "item changed", "item changed: " + Item_tobe_Checked.state.toString )
end

But when I eg restart openHAB the item restores it’s value:

Item_tobe_Checked changed from NULL to previous text

How can I change the when part to make the rule not to trigger when changing from NULL ?

You can’t.

But you can add an if() to your rule and see what the previousState implicit variable contains.

1 Like

Thx, I was also thinking of this approach, but I hoped there was another solution. With simple switch items I use ‘changed from on to off’ or ‘changed from off to on’

Quickly follow-up question on this:

This implies that, assume you do it that way, you can‘t continue to use Blockly on an existing code (which you only want to fire if the previous value was not NULL), since you can’t include custom code into Blockly without converting the entire Blockly into code, correct?

No idea. Which part do you think cannot be done in Blockly?

Thanks for your response!

The way I understood Blockly, I can only chose from the blocks that are available (e.g. this one:
image).

What I have not found so far is an off-the-shelf block that gets previousState of an item, nor the ability to define a custom block or include custom code that gets previousState. Or am I misunderstanding something?

See “contextual info” by the look of it.

previousState in this usage is not “the previous state of a specific named Item” it is instead “the previousState of whatever triggered this rule”, thats the contextual part.

1 Like

Great, thanks! :slight_smile: Didn’t know this block so far, this did the trick.