Notification Message based on if else evaluation of trigger state

Hi All,

I am creating a notification automation for when a garage door is open or closed using trigger template to make it more dynamic and not have a notification per door. I got it to work in a basic form, but when is basic ever enough?

This works and notifies me of which door’s binary sensor is ‘on’ or ‘off’:

{{ trigger.to_state.attributes.friendly_name }} is {{ trigger.to_state.state  }}

The problem is that the binary sensor states are ‘on’ or ‘off’ although it is a garage_door class. In the entity’s history I can see it was opened or closed, but I am unable to extract those states for the message body.

Instead I am trying to do something like this, but it is not working:

{{ trigger.to_state.attributes.friendly_name }} is {% if is_states('trigger.to_state.state','on') %) open {% else %} closed {% endif %}

I have gone through various syntax iterations and should have noted them to share here as some could save, but would not evaluate the state and only give the message that the door is closed.

I am not sure if this is possible or if I should do something more elaborate with a proper value template

The automation thus far:

alias: Notify Garage Door
description: ''
trigger:
  - platform: state
    entity_id: >-
      binary_sensor.main_garage_door,binary_sensor.small_garage_door,binary_sensor.back_garage_door
condition: []
action:
  - service: notify.mobile_app_xxxxxx
    data:
      message: >-
        {{ trigger.to_state.attributes.friendly_name }} is {% if is_states('trigger.to_state.state','on') %) open {% else %} closed {% endif %}
mode: single

Thanks in advance!

Try this:

message: >
  {{ trigger.to_state.attributes.friendly_name}} is {{'open.' if is_state('trigger.to_state.state','on') else 'closed.' }}

Thanks, but it does not work. It either skips the if statement or the statement does not return any value and just says closed.

Sorry too many .states. Try this:

message: >
  {{ trigger.to_state.attributes.friendly_name}} is {{'open.' if is_state('trigger.to_state','on') else 'closed.' }}

Sadly still only closed

How are you testing this?

If you trigger it manually with the trigger automation button or service there will be no trigger.to_state.

Why are you treating trigger.to_state like an entity name and use it in is_state? I don’t understand.

message: >
  {{ trigger.to_state.attributes.friendly_name}} is {{'open.' if trigger.to_state.state == 'on' else 'closed.' }}

Because it’s ages since I used it last and just perpetuated the error with copy/paste.

1 Like

I am testing this by opening and closing the door as I understand you cannot test this using the template test.

In short, the automation can trigger on any of the 3 separate entities. I want the notification to indicate which entity triggered the automation and what the state of that trigger was, be it opening or closing. The binary sensor only has ‘on’ or ‘off’ states and I want to translate this into ‘open’ or ‘closed’

I am open to suggestions on how to do this and avoid having 3 or 6 notification automations to do this.

Use the template BurningStone posted above. I stuffed mine up.

Nevermind. I just noticed the template it is different and tested it and it is working!

Thank you to both for assisting with this. It is highly appreciated.

2 Likes

Great to hear. And yes you were testing it the correct way. The trigger.to_ objects will not have values unless you actually trigger the automation with one of the trigger entities.

Dear
I am looking for prayer time automation in single automation .
I need to play if Fajr prayer (Prayer_Fajr.mp3) if other any prayers need to play (Prayer_Azan.mp3)
Here my code

alias: Prayer Time New
trigger:
  - platform: time
    at:
      - sensor.fajr_prayer
      - sensor.dhuhr_prayer
      - sensor.asr_prayer
      - sensor.maghrib_prayer
      - sensor.isha_prayer
condition: []
action:
  - service: media_player.media_stop
    target:
      entity_id: media_player.ytube_music_player
    data: {}
  - service: media_player.volume_set
    data:
      volume_level: 0.5
    target:
      entity_id:
        - media_player.bedroom_speaker
  - data:
      media_content_id: >-
        /local/Prayer_{{'Fajr' if trigger.to_state.object_id == 'dhuhr_prayer' else 'Azan' }}.mp3
      media_content_type: audio/mp3
    service: media_player.play_media
    target:
      entity_id:
        - media_player.bedroom_speaker
  - type: turn_on
    device_id: aea2a8c949351712c46de6ed0dcd31c5
    entity_id: switch.table_lamp_switch_1
    domain: switch
  - service: notify.mobile_app_my_oppo
    data:
      message: Prayer Time
      title: Prayer Time
      data:
        image: /media/local/Images/Rasulullah.jpeg
mode: single



Unfortunately getting bellow errors

Logger: homeassistant.components.automation.prayer_time_new
Source: helpers/script.py:1375
Integration: Automation (documentation, issues)
First occurred: 2:12:58 PM (1 occurrences)
Last logged: 2:12:58 PM

Prayer Time New: Error executing script. Error for call_service at pos 3: Error rendering data template: UndefinedError: 'dict object' has no attribute 'to_state'
Logger: homeassistant.helpers.template
Source: helpers/template.py:1834
First occurred: 2:12:58 PM (1 occurrences)
Last logged: 2:12:58 PM

Template variable error: 'dict object' has no attribute 'to_state' when rendering '/local/Prayer_{{'Fajr' if trigger.to_state.object_id == 'dhuhr_prayer' else 'Azan' }}.mp3'

May I know how can I fix this

This is a year old solved topic that has very little to do with your question.

Please start your own topic.