Parsing to state in automation template

Hi There,

i was referring to this post https://community.home-assistant.io/t/send-entity-state-to-notification-using-ha-automations/150181

this got me what i needed for my home alarm telegram notification to tell me what sensor friendly name triggered, and what the state is. however, i’m monitoring doors/windows opening when the alarm panel is armed. i have my device classes set proper, but that only seems to adjust the UI state string (instead of on/off, the ui shows open/closed).

when i get the notification event, i get the state as “on” and ‘off’ . i want it to parse to show opened/closed

how could i write that in the data_template?

this is what i have now

- service_template: notify.telegram_alert
  data_template:
    title: "[Alarm] Triggered"
    message: >
      "The {{ trigger.to_state.attributes.friendly_name }} has {{ trigger.to_state.state}} !"

i saw on the automation templating page in home assistant, that they show a test for when you are manually trying to test the data, can something like this be used within my data template to parse the string to what i want?

{% set trigger={'to_state':{'state': 'heat'}} %}
{% set option = trigger.to_state.state %}
{{ 'on' if option == 'heat' else 'off' }}

yep. that did the trick. answered my own question. here is what i did.

- service_template: notify.telegram_alert
  data_template:
    title: "[Alarm] Triggered"
    message: >
      "The {{ trigger.to_state.attributes.friendly_name }} has {% set option = trigger.to_state.state %}{{ 'opened' if option == 'on' else 'closed'}} !"

If you wish, you can shorten it a bit:

- service_template: notify.telegram_alert
  data_template:
    title: "[Alarm] Triggered"
    message: >
      "The {{trigger.to_state.name}} has {{'opened' if trigger.to_state.state == 'on' else 'closed'}}!"

thanks @123! anything to streamline the code (and in this case i feel make it even more human readable is greatly appreciated!

1 Like