Automation template if

Hi guys,
Just reinstalled my Home Assistant after some time without it. I put back my old automation file and get som weird new error, anyone who can help me?

Error: Error while executing automation automation.changed_to_sonos_from_tv. Invalid data for call_service at pos 1: not a valid value for dictionary value @ data[‘entity_id’]

  • id: toTVfromSONOS
    alias: Changed to TV from SONOS
    trigger:
    • platform: state
      entity_id: input_select.active_status
      from: ‘SONOS’
      to: ‘TV’
      action:
    • service: switch.turn_on
      data_template:
      entity_id: >-
      {% if is_state(‘switch.pioneer’, ‘off’) %}
      switch.pioneer
      {% endif %}
    • service: homeassistant.turn_on
      entity_id: script.pioneer_volume_down_20
    • delay: ‘00:00:03’
    • service: homeassistant.turn_on
      entity_id: script.optisk_1

Please format your code. YAML’s syntax (indenting and quotation characters) is critical to its operation. If presented unformatted, it’s difficult to tell if you have syntax errors.

The error message you are receiving is due to the template.

  • When the if statement is true, entity_id is set to switch.pioneer.
  • When the if statement is false, entity_id is set to nothing. That’s an invalid option for the switch.turn_on service.
- id: toTVfromSONOS
  alias: Changed to TV from SONOS
  trigger:
    - platform: state
      entity_id: input_select.active_status
      from: 'SONOS'
      to: 'TV'
  action:
    - service: switch.turn_on
      data_template:
        entity_id: >-
          {% if is_state('switch.pioneer', 'off') %}
            switch.pioneer
          {% endif %}
    - service: homeassistant.turn_on
      entity_id: script.pioneer_volume_down_20
    - delay: '00:00:03'
    - service: homeassistant.turn_on
      entity_id: script.optisk_1

Thank you for your reply. Above you can see my code presented in codeblocks. Second thing, what do I do when i only want to start my surround system when it’s off, and do nothing when its on?

Thank you for formatting the code.

Use a condition to check if switch.pioneer is off. The action will be executed only if switch.pioneer is off.

- id: toTVfromSONOS
  alias: Changed to TV from SONOS
  trigger:
    - platform: state
      entity_id: input_select.active_status
      from: 'SONOS'
      to: 'TV'
  condition:
    condition: state
    entity_id: switch.pioneer
    state: 'off'
  action:
    - service: switch.turn_on
      entity_id: switch.pioneer
    - service: homeassistant.turn_on
      entity_id: script.pioneer_volume_down_20
    - delay: '00:00:03'
    - service: homeassistant.turn_on
      entity_id: script.optisk_1

Thanky you for your reply Sir.

But what if it’s on? Then it wont do the last three things that I still want it to do?

Do you want it to only run if it’s off or run if it’s on or off?

That requirement wasn’t clear from your original automation (where the template would simply fail if switch.pioneer was already on).

Question for you: What happens if you turn on switch.pioneer when it’s already on?

My guess is that it simply stays on and nothing bad happens (other than we needlessly sent the on command). If that’s true then the automation is simplified:

- id: toTVfromSONOS
  alias: Changed to TV from SONOS
  trigger:
    - platform: state
      entity_id: input_select.active_status
      from: 'SONOS'
      to: 'TV'
  action:
    - service: switch.turn_on
      entity_id: switch.pioneer
    - service: homeassistant.turn_on
      entity_id: script.pioneer_volume_down_20
    - delay: '00:00:03'
    - service: homeassistant.turn_on
      entity_id: script.optisk_1

What happens is that it turns switch.pioneer off when I run switch.turn_on when it’s already on. Sadly…

In that case, I recommend you restructure the automation’s action. You are already using scripts so I suggest you call one script when switch.pioneer is off and another script when it is on.

- id: toTVfromSONOS
  alias: Changed to TV from SONOS
  trigger:
    - platform: state
      entity_id: input_select.active_status
      from: 'SONOS'
      to: 'TV'
  action:
    - service: script.turn_on
      data_template:
        entity_id: script.pioneer_is_{{states('switch.pioneer') | lower}}

Create two scripts:

  • script.pioneer_is_on
  • script.pioneer_is_off
script.pioneer_is_on:
  sequence:
    - service: homeassistant.turn_on
      entity_id: script.pioneer_volume_down_20
    - delay: '00:00:03'
    - service: homeassistant.turn_on
      entity_id: script.optisk_1

script.pioneer_is_off:
  sequence:
    - service: switch.turn_on
      entity_id: switch.pioneer
    - service: script.turn_on
      entity_id: script.pioneer_is_on