Using entity attribute dictionary states in an if_statement

I’m trying to use the ‘etd’ state of a train in an automation that messages me if it’s not ‘On time’.

That state seems to be from an attribute dictionary.

So far I can see the state reported correctly in Template Tools using:

{{ state_attr('sensor.trains_colchester_to_chelmsford_20m_walk', 'service')['etd']}}

In the automation I can get it to message me for a specific train by referencing the entity state:

metadata: {}
data:
  message: >-
    {% if is_state('sensor.trains_colchester_to_chelmsford_20m_walk', '08:54')
    %}The trains are fine{% else %} {{
    states('sensor.trains_colchester_to_chelmsford_20m_walk') }} {% endif %}
action: notify.mobile_app_nicks_iphone

But I can’t get the if statement to be dependent on the ‘etd’ attribute value.

How would I write the if statement in the message to use ‘etd: On time’?

{% if state_attr('sensor.trains_colchester_to_chelmsford_20m_walk','service').etd == 'On time' %}

Thanks for the code.

My message code works and is now:

{% if state_attr('sensor.trains_colchester_to_chelmsford_20m_walk','service').etd == 'On time' %}The trains are fine{% else %} Get the {{
    states('sensor.trains_colchester_to_chelmsford_20m_walk') }} {% endif %}

For future reference: https://community.home-assistant.io/t/how-to-help-us-help-you-or-how-to-ask-a-good-question/114371#oneone-format-it-properly-16

1 Like