That looks fine Tom
I snuck that edit in before your reply
Also worth mentioning that trigger.from_state.name
will fall back to an entity_id if no friendly name exists.
I’ve always liked that ‘fail-safe’ behavior because it works exactly the same way in Premise; when I first encountered it (many years ago) it struck me as being a simple but thoughtful touch.
Yeah it’s a definite improvement to the template.
I believe the format in the OP would have worked but there is a misunderstanding…I think…
You are using the “is_state” method but the trigger doesn’t use that (not sure exactly the terminology but I think that the trigger isn’t a “state object” so you can’t use the is_state() method on it).
trigger.to_state.state returns a string.
try this as a test to see if it works (I think it should):
message: "{{ trigger.from_state.attributes.friendly_name }} is now {% if trigger.to_state.state == 'on') %}Open{% else %}Closed{% endif %}!"
@123’s is a bit more compact but you were on the right track except for that tweak.
In fact it was easier than that, OP just needed to remove the single quotes from around trigger.to_state.state in the template as trigger is a variable.
That said, it is messy so I’m glad they asked and @123 cleaned it up.
so just to clarify you are saying this would work:
{% if is_state(trigger.to_state.state, 'on') %}Open{% else %}Closed{% endif %}
Yes. That is the ‘extended’ version of the second half of Taras’ compacted template.
Hmm…interesting.
I didn’t know you could use is_state() with variables as the tested object.
I’m pretty certain, and Taras had used the same convention so I’m in good company - but I’ll happily stand corrected
Yeah, now that I think about it a bit more I guess that the trigger is a state object since it actually does contain the “state” and all of the other data contained inside.
I just never picked up on that being considered a variable that could be used (without the quotes) in the state() or its variations.
Hey everyone,
Thank you for all the answers, glad I picked your mind and created some constructing conversation
I wish i could try out all the options and tell you what’s up but right now it seems the whole notify.lg_tv thingy service is down. It doesnt work at all.
So ill troubleshoot this first and come back with results.
I figured it out, i specified a notifier group with the same name as the entity in it and it messed the whole thing up.
Anyways, back to the subject.
I am afraid none of them work.
"{{ trigger.from_state.name }} is now {{ 'Open' if is_state(trigger.to_state.state, 'on') else 'Closed' }}!"
and
"{{ trigger.from_state.attributes.friendly_name }} is now {% if is_state(trigger.to_state.state, 'on') %}Open{% else %}Closed{% endif %}!"
Both give me Closed in both the situations where i open and close the door (binary sensor on and off)
Have you tried my version from my first post above:
but Taras’ version should work I think if nothing else does.
Gimme a sec, trying them now.
message: "{{ trigger.from_state.attributes.friendly_name }} is now {% if trigger.to_state.state == 'on') %}Open{% else %}Closed{% endif %}!"
Does not work at all, no notification appears.
@123 is giving closed in both states
"{{ trigger.from_state.name }} is now {{ 'Open' if is_state(trigger.to_state.state, 'on') else 'Closed' }}!"
@tom_l’s worked:
message: "{{ trigger.from_state.attributes.friendly_name }} is now {{ 'Open' if trigger.to_state.state == 'on' else 'Closed' }}."
We have a winner!
Try this one too:
message: "{{ trigger.from_state.attributes.friendly_name }} is now {{ 'Open' if trigger.to_state.state == 'on' else 'Closed' }}."
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
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.