Multiple options in 'when' statement in rule

Hi,

Writing a few new rules that run on an item changing but also implicitly rely on running only if my presence item is ON, ie I am home, or OFF I am away.

The bulk of my rules run:

rule "My new rule"
when
    Item someitem changed
then
    if(vPresent.state == ON)
    {
        // do some stuff when I am home
    }
    else
    {
        // do some other stuff as vPresent must be OFF
    }
end

Simples.

However, as some of my rules run implicitly ONLY when say vPresent = ON so I’m thinking could I cut down on my code and just add this to my ‘when’ expression.

rule "My new rule 2"
when
    Item someitem changed and vPresent.state == ON
then
        // do some stuff when I am home
end

I know you can have multiple ‘or’ in the when part but can you have an ‘and’ there and then reduce the code in the main then part? Seems less code.

If so, what is the best practise, and why?

This is not possible. You will need to use the conditional in the body of the rule.

1 Like

Because the rules are triggered with events not states

Ahhhh, <penny drops>, of course…

Thanks both…every day is a school day…

1 Like

I thought you could use or in the when for multiple triggers. You mean or is permissible but not and?

Yes. You can trigger with several triggers with or
but not and. it doesn’t make sense. You will NEVER have two events at exactly the same time.

Remember the rules are triggered by EVENTS not STATES

1 Like