First attempt at data template not passing config check

Please could someone with a better eye than I take a look at my code below and point out the error of my ways?

The error I get is:

Invalid config for [automation]: invalid template (TemplateSyntaxError: Encountered unknown tag 'endif'.) for dictionary value @ data['action'][0]['data_template']['entity_id']. Got None. (See /config/configuration.yaml, line 157). Please check the docs at https://home-assistant.io/components/automation/

- alias: "Auto Change Channel from timer"
  trigger:
    platform: state
    entity_id: input_boolean.auto_channel_change_active
    to: 'on'
  action:
    - service: automation.turn_on
      data_template:
        entity_id: >
          {& if is_state('input_select.channel_select', 'BBC 1') %}
            automation.bbc1
          {& elif is_state('input_select.channel_select', 'BBC 2') %}
            automation.bbc2
          {& if is_state('input_select.channel_select', 'ITV') %}
            automation.itv
          {& elif is_state('input_select.channel_select', 'Forumla 1') %}
            automation.formula1
          {& if is_state('input_select.channel_select', 'Sky News') %}
            automation.sky_news
          {& elif is_state('input_select.channel_select', 'BT Sport 1') %}
            automation.bt_sport_1
          {& if is_state('input_select.channel_select', 'BT Sport 2') %}
            automation.bt_sport_2
          {& elif is_state('input_select.channel_select', 'BT Sport 3') %}
            automation.bt_sport_3
          {& if is_state('input_select.channel_select', 'Eurosport 1') %}
            automation.eurosport_1
          {& elif is_state('input_select.channel_select', 'Eurosport 2') %}
            automation.eurosport_2
          {% endif %}
    - service: input_boolean.turn_off
      entity_id: input_boolean.auto_channel_change_active
    - service: input_boolean.turn_off
      entity_id: input_boolean.tv_channel_change_clock_status

The & should be a %

{& if is_state('input_select.channel_select', 'BBC 1') %}
^^^

Thank you!

I’ve been staring at that for hours and not seen it.

Uh, another thing, you are intermixing if and else if all over the place. your syntax should be

if
elif
elif
elif
elif
elif
else
end

you’re doing

if
elif
elif
if
elif
if
elif
if
elif
end

Also, just food for thought…

If you rename the following automations:

automation.bbc1 -> automation.bbc_1
automation.bbc2 -> automation.bbc_2
automation.formula1 -> automation.formula_1

your automation and template can be simplified to this:

- alias: "Auto Change Channel from timer"
  trigger:
    platform: state
    entity_id: input_boolean.auto_channel_change_active
    to: 'on'
  action:
    - service: automation.turn_on
      data_template:
        entity_id: "automation.{{ states('input_select.channel_select').lower().replace(' ','_') }}"
    - service: input_boolean.turn_off
      entity_id: input_boolean.auto_channel_change_active
    - service: input_boolean.turn_off
      entity_id: input_boolean.tv_channel_change_clock_status

And whenever you add a channel, you simply need to name the automation based on your input select:

Input Select Option -> automation.input_select_option

No crazy if statements and very easy to implement a new channel in the future.

2 Likes

Thank you!

That’s excellent.

That works great.

What’s the purpose of lower() in the template?

it makes the string lowercase. So

"SDFJLkwlkfjsldkfjLSDKJF"

becomes

"sdfjlkwlkfjsldkfjlsdkjf"

Thank you!