Condition template in Automation

I have 3 conditional templates within the same action. For some reason I can only get one of them to work. Is my yaml incorrect?

action:
- condition: template
  value_template: "{{ is_state('input_boolean.johns_echo_playing_boolean', 'on') }}"
- service: media_player.media_play
  entity_id: media_player.johns_echo
- condition: template
  value_template: "{{ is_state('input_boolean.living_room_dot_playing_boolean', 'on') }}"
- service: media_player.media_play
  entity_id: media_player.living_room_dot
- condition: template
  value_template: "{{ is_state('input_boolean.bedroom_spot_playing_boolean', 'on') }}"
- service: media_player.media_play
  entity_id: media_player.bedroom_spot

Not sure what you mean, but in case you don’t know, if a condition template evaluates to false the entire rest of the script is aborted, not just the next step. So, e.g., if input_boolean.johns_echo_playing_boolean is off, none of the media will play.

Ok, so how do I accomplish what I want to do? This is part of a Door Bell script, where I paused all the media players and then I wan’t to only set those to play that were playing before.
I have that stored in the 3 booleans. If on, they were playing before I ran the script and need to be turned back on.

So basically just 3 different if then statements within the same action

I believe you can call the media_player.media_play service (like many services) with a list of entity_ids, where one of them doesn’t actually exist. So, you can use a data_template to “build up” the list of entity_ids to turn back on, something like this:

action:
  service: media_player.media_play
  data_template:
    entity_id: >
      media_player.dummy
      {% if is_state('input_boolean.johns_echo_playing_boolean', 'on') %}
        ,media_player.johns_echo
      {% endif %}
      {% if is_state('input_boolean.living_room_dot_playing_boolean', 'on') %}
        ,media_player.living_room_dot
      {% endif %}
      {% if is_state('input_boolean.bedroom_spot_playing_boolean', 'on') %}
        ,media_player.bedroom_spot
      {% endif %}
1 Like

Interesting. I will test it tomorrow. Thank you

Just tested it and it works like a charm!! Thank you!!

1 Like

I find it interesting that Home Assistant does not log an error when a service is given a non-existent entity.

I would’ve expected the following code to fail the Config Check or report a run-time error concerning the non-existent entity. However, Home Assistant doesn’t care; it passes the check and no errors are reported (not even Warning or Info).

action:
  service: switch.turn_on
  data:
     entity_id: switch.dummy

My takeaway is that you have to be careful when typing an entity’s name. If you misspell it in an automation or script, nothing in the system will report the non-existent entity.

Yeah, I’m not sure what the thinking was. Could it be on purpose, so that, say for example, you can write an automation or script that interacts with an entity that exists only sometimes but not all the time? (Like maybe a plug in Z-Wave device that you only use during the holidays.) Or is it just an accident of how the person who wrote that code chose to write it?

I based my suggestion above on how it currently works. There is at least one other way to do it that doesn’t depend on this “feature”, but it gets more involved.