Template in script to kick-off 1 or another script

So I want a script which will kick-off 1 or another script depending on the status of a switch.
I tried many many things but don’t succeed.

Currently I have this as a script:

tea:
    alias: Tea
    sequence:
    - service_template: script.turn_on
      data_template: >-
        {% if is_state('switch.Sonoff20a', 'off') %}
          tea1
        {% elif is_state('switch.Sonoff20a', 'on') %}
          tea2
        {% endif %}

When put in the Templates test <> thing this is the result:

tea:
    alias: Tea
    sequence:
    - service_template: script.turn_on
      data_template: >
        
          tea1

Seems good to me, no?

However when trying to start Hass with this, I get the error:

2017-12-21 22:47:35 ERROR (MainThread) [homeassistant.config] Invalid config for [script]: expected a dictionary for dictionary value @ data['script']['tea']['sequence'][0]['data_template']. Got "{% if is_state('switch.Sonoff20a', 'off') %}\n  tea1\n{% elif is_state('switch.Sonoff20a', 'on') %}\n  tea2\n{% endif %}". (See /home/homeassistant/.homeassistant/configuration.yaml, line 475). Please check the docs at https://home-assistant.io/components/script/
2017-12-21 22:47:35 ERROR (MainThread) [homeassistant.setup] Setup failed for script: Invalid config.

Also tried these but all gave errors:

sequence:
  - service: script.turn_on
    data_template:
      {% if is_state('switch.Sonoff20a', 'off') %}
        entity_id: script.tea1
      {% elif is_state('switch.Sonoff20a', 'on') %}
        entity_id: script.tea2
      {% endif %}

sequence:
  - service_template: script.turn_on
    data_template: >-
      {% if is_state('switch.Sonoff20a', 'off') %}
        script.tea1
      {% elif is_state('switch.Sonoff20a', 'on') %}
        script.tea2
      {% endif %}

And a few others.

Who knows how to crack this one?

Your second try looks right (with entity_id and script.tea1), but you’re missing the multi-line symbol after data_template: >

Changed it to this:

tea:
    alias: Tea
    sequence:
      - service: script.turn_on
        data_template: >
          {% if is_state('switch.Sonoff20a', 'off') %}
            entity_id: script.tea1
          {% elif is_state('switch.Sonoff20a', 'on') %}
            entity_id: script.tea2
          {% endif %}

Doesn’t work:

2017-12-21 23:56:12 ERROR (MainThread) [homeassistant.config] Invalid config for [script]: expected a dictionary for dictionary value @ data['script']['tea']['sequence'][0]['data_template']. Got "{% if is_state('switch.Sonoff20a', 'off') %}\n  entity_id: script.tea1\n{% elif is_state('switch.Sonoff20a', 'on') %}\n  entity_id: script.tea2\n{% endif %}\n". (See /home/homeassistant/.homeassistant/configuration.yaml, line 475). Please check the docs at https://home-assistant.io/components/script/
2017-12-21 23:56:12 ERROR (MainThread) [homeassistant.setup] Setup failed for script: Invalid config.

Oh! Instead you should use your template to specify the entity_id, not what data you’re passing.

tea:
	alias: Tea
	sequence:
	  - service: script.turn_on
		data_template:
		  entity_id: >  
			{% if is_state('switch.Sonoff20a', 'off') %}
			  script.tea1
			{% elif is_state('switch.Sonoff20a', 'on') %}
			  script.tea2
			{% endif %}

I’m not 100% on the spacing for the “script.tea1” line, but this is the right approach. Sorry for misleading you earlier!

1 Like

Excellent!!! It’s works! :slight_smile:

Here the code with correct spacing:

tea:
    alias: Tea
    sequence:
    - service: script.turn_on
      data_template:
        entity_id: >
          {% if is_state('switch.Sonoff20a', 'off') %}
             script.tea1
          {% elif is_state('switch.Sonoff20a', 'on') %}
             script.tea2
          {% endif %}