If statement in automations help

I’m trying to shorten some automations. i have now:

  - alias: 'Garage door Opened'
    trigger:
      - platform: state
        entity_id: binary_sensor.garage_entry
        to: 'on'
    action:
      - service: media_player.alexa_tts
        entity_id: media_player.dot_2
        data:
          message: "Garage door Opened"

  
  - alias: 'Garage door Closed'
    trigger:
      - platform: state
        entity_id: binary_sensor.garage_entry
        to: 'off'
    action:
      - service: media_player.alexa_tts
        entity_id: media_player.dot_2
        #volume_level: .6
        data:
          message: "Garage door Closed" 

what I tried and has worked is:

  - alias: 'Garage door Test'
    trigger:
      - platform: state
        entity_id: binary_sensor.garage_entry
        # to: 'off'
    action:
      - service: media_player.alexa_tts
        entity_id: media_player.dot_2
        data_template:
          message: >
            {% if is_state('binary_sensor.garage_entry', 'on') %}
              Garage door Closed
            {% elif is_state('binary_sensor.garage_entry', 'off') %}
              Garage door Open
            {% endif %} 

Since in the trigger section i specified entity_id why does this not work?

  - alias: 'Garage door Test2'
    trigger:
      - platform: state
        entity_id: binary_sensor.garage_entry
        # to: 'off'
    action:
      - service: media_player.alexa_tts
        entity_id: media_player.dot_2
        data_template:
          message: >
            {% if is_state('off') %}
              Garage door Closed
            {% elif is_state('on') %}
              Garage door Open
            {% endif %}

also after “message:” what does the “>” mean?

      message: >

to combine both into one is there a better simpler way?

@arun2118, Looking at your code the trigger will never fire, you have an entity_id, but something has to happen with that entity_id in order to fire. You have commented out the firing mechanism ( # to: ‘off’ ). I think what you want to do here is create 2 triggers like this:

- alias: 'Garage door Test'
  trigger:
    - platform: state
      entity_id: binary_sensor.garage_entry
      to: 'off'
  trigger:
    - platform: state
      entity_id: binary_sensor.garage_entry
      to: 'on'
  action:
    - service: media_player.alexa_tts
      entity_id: media_player.dot_2
      data_template:
        message: >
          {% if is_state('binary_sensor.garage_entry', 'on') %}
            Garage door Closed
          {% elif is_state('binary_sensor.garage_entry', 'off') %}
            Garage door Open
          {% endif %} 

With this setup the trigger will fire when the Garage Door is open or closed and with the data template it will determine which action happened and announce it on your media player. As for the your second question about ( message: > ), that is just part of the format for the message template, I hope this helps.

This doesn’t work because you don’t have an entity_id specified in is_state() method, these methods and their inputs are covered here:


the > just means “This is a multi line template”. Without that symbol, you need to have quotes that wrap around your entire template ({%%} and {{ }}), which looks ugly.


This automation should work for you.

  - alias: 'Garage door Test'
    trigger:
      - platform: state
        entity_id: binary_sensor.garage_entry
    action:
      - service: media_player.alexa_tts
        entity_id: media_player.dot_2
        data_template:
          message: >
            {% if trigger.to_state.state == 'on' %}
              Garage door Closed
            {% else %}
              Garage door Open
            {% endif %} 

This uses the trigger.to_state state object. You don’t need to specify a entity_id, it just grabs the current state and checks against it. This is covered here.

1 Like

The format in your post won’t work. You cannot have 2 trigger sections. You can have 2 triggers, not 2 sections. That would look like this:

- alias: 'Garage door Test'
  trigger:
    - platform: state
      entity_id: binary_sensor.garage_entry
      to: 'off'
    - platform: state
      entity_id: binary_sensor.garage_entry
      to: 'on'

It should be noted that > works for all templates and is just not for message templates. The symbol specifically means ‘multi-line template’. If you add a - after, it means ‘mutli-line template with no whitespace’.

@petro, You are right that was a goof on my part, the second trigger line needs to be removed.

Also, for your own info, the to: or from: attributes are optional. When omitted, the automation will fire on every state change regardless of the state itself.

1 Like

Good to know, I learned something new, thanks.

Thanks guys, shortly after posting the forum went down. I’m struggling with a new issue now but:

The code we talked about was for a message to play when the garage door is open, if i just wanted one switch on the frontend to control all switches how can i include more entities? i thought adding a comma would but does not work

   - alias: 'Garage door Test'
    trigger:
      - platform: state
        **entity_id: binary_sensor.garage_entry, binary_sensor.front_door**
        # to: 'off'
    action:
      - service: media_player.alexa_tts
        entity_id: media_player.dot_2
        data_template:
          message: >
            {% if is_state('binary_sensor.garage_entry', 'on') %}
              Garage door Closed
            {% elif is_state('binary_sensor.garage_entry', 'off') %}
              Garage door open
            {% elif  is_state('binary_sensor.front_door', 'on') %}
              front door door Closed
            {% elif is_state('binary_sensor.front_door', 'off') %}
             front door door Open
            {% endif %}

That will work. You can also expand the code I provided to do that for you without getting a message about every door when only 1 door opens:

  - alias: 'Garage door Test'
    trigger:
      - platform: state
        entity_id: binary_sensor.garage_entry, binary_sensor.front_door
    action:
      - service: media_player.alexa_tts
        entity_id: media_player.dot_2
        data_template:
          message: >
            {% if trigger.to_state.state == 'on' %}
              {{ state_attr(trigger.entity_id,'Friendly Name') }} Closed
            {% else %}
              {{ state_attr(trigger.entity_id,'Friendly Name') }} Open
            {% endif %}

I have seen this post and thought I would try out some automations.

I have 3 sensors that are linked to my alarm system. If any of the sensors are ‘on’ (open) then the alarm will not activate.

I want to create an automation that will tell me which are open. I have been testing out the below code but even though the binary_sensor.kitchen_window_l is open it doesn’t get past the front door

service: tts.cloud_say
data:
  entity_id: media_player.google_nest_mini
  message: >-
    {% if is_state('binary_sensor.front_door', 'on') %} 
    The front door is open
    {% elif is_state('binary_sensor.kitchen_window_l', 'on') %}  
    the left kitchen window is open 
    {% elif is_state('binary_sensor.kitchen_window_r', 'on') %}
    the right kitchen window is open             
    {% endif %}

What am I doing wrong? Probably something very simple… I’m a total noob at this

Just realised it needs to be seperate if statements… doh!

    {% if is_state('binary_sensor.front_door', 'on') %}  The front door is open
    {% endif %}
    {% if is_state('binary_sensor.kitchen_window_l', 'on') %}   the left
    kitchen window is open {% endif %}
    {% if is_state('binary_sensor.kitchen_window_r',
    'on') %} the right kitchen window is open              
    {% endif %}