Need template help (newbie)

I’m struggling with templates…and the NOT condition.

I want to execute an automation when

state.sensorA NOT equal to either “apple” or “pear”

I just can’t figure out how to write this as a template.

Any help appreciated.

value_template: "{{ states('sensor.a') not in ['apple', 'pear'] }}"

so simple when I look at your template…so hard without an example…thanks.

I’ve seen that type of code used elsewhere and it always reads like it’s backwards to me.

I always think it should read in human speak that “is ‘apple’ or ‘pear’ contained in sensor.a”.

So would it still work and render the same result if it was written in reverse? Like:

{{ ['apple', 'pear'] not in states('sensor.a') }}

It would not (you’ll get ‘error rendering template’). The structure is:

is this item in this list

For example, this template:

{{ 'kiwi' not in ['apple', 'pear' ] }}

would produce True (kiwi is not in the list containing apple and pear).

Your ‘reverse example’ is asking if “a list is not in this item” which cannot be evaluated.

1 Like

OK, got it.

Obviously, I was reading what I thought it was saying incorrectly.