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

I am trying to set up a notification everytime a door opens or closes.
The action of the automation looks like this:

    action:
      service: notify.up_lr_lg_tv
      data:
        message: "{{ trigger.from_state.attributes.friendly_name }} is now {{trigger.to_state.state}}" 
    

Which works great, and outputs “Main door sensor is on” or “off” depending to the state.

Next, I am trying to change the on/off to Open/Closed following the documentation like this:

    action:
      service: notify.up_lr_lg_tv
      data:
        message: "{{ trigger.from_state.attributes.friendly_name }} is now {% if is_state('trigger.to_state.state', 'on') %}Open{% else %}Closed{% endif %}!"

But this if, never checks as true. I always get XXX is now Closed, no matter if i open or close the door.

Any ideas?

I see 123 is replying. I want see if my suggestion:

        message: "{{ trigger.from_state.attributes.friendly_name }} is now {{ 'Open' if trigger.to_state.state == 'on' else 'Closed' }}."

Is correct. I have trouble with one line if/else templates.

1 Like

Just add some quotes to the strings and I believe the reference to friendly_name can be shortened.

    action:
      service: notify.up_lr_lg_tv
      data:
        message: "{{ trigger.from_state.name }} is now {{ 'Open' if is_state(trigger.to_state.state, 'on') else 'Closed' }}!"

Without the quotes it will consider Open and Closed to be the names of variables.

1 Like

That looks fine Tom :slight_smile:

1 Like

I snuck that edit in before your reply :slight_smile:

1 Like

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.

1 Like

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.

1 Like

Hmm…interesting.

I didn’t know you could use is_state() with variables as the tested object.

1 Like

I’m pretty certain, and Taras had used the same convention so I’m in good company - but I’ll happily stand corrected :wink:

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 :slight_smile:
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! :smiley: :partying_face:

Try this one too:

message: "{{ trigger.from_state.attributes.friendly_name }} is now {{ 'Open' if trigger.to_state.state == 'on' else 'Closed' }}."
2 Likes