[SOLVED]Problem with logical check in automation with trigger.to_state.state

That’s the one that worked.
Thanks everyone very much, i would have never figured it out on my own.

BTW, i keep realizing how far HA has come through the years.
I was changing the code, hitting C on the front end, searching for “reload automations” hit enter, then go to the developers tools and manually change the state of the sensor to test it. 15 seconds with 3 tabs open…
A couple of years back it would have taken me around 5 minutes for each test :sob:

My mistake, I copied your original template without considering its validity.

This is incorrect:

is_state(trigger.to_state.state, 'on')

because is_state expects the first argument to be an entity. What it’s getting is an entity’s state value and that’s wrong (and it will always evaluate to false which explains why the template only returns Closed).

The correct test is the way tom_I suggested (and you proved empirically). This compares two values:

if trigger.to_state.state == 'on'

Ah! that’s understandable.

I believe this would work (because trigger.entity_id supplies the entity that was responsible for triggering the automation) but I’d stick with tom_I’s version:

is_state(trigger.entity_id, 'on')
1 Like