Templating problem (service_template and data_template)

Hi all,

I’ve run into a problem with templating. I have the following code (adaptation from code found on this site):

intent_script:
  ActivateObject:
    action:
      - service_template: >
          {%- if OnOff == "on" -%}
            switch.turn_on
          {%- else -%}
            switch.turn_off
          {%- endif -%}
        data_template:
          entity_id: "switch.{{ device | string }}"

When coding in Notepad++ both data_template and entity_id are gray, where I think they should not be. Or I’m I wrong about this?
When I remove the “>” the colors off the text is correct but I get errors the following error:

ERROR (MainThread) [homeassistant.bootstrap] Error loading /config/configuration.yaml: while scanning for the next token found character ‘%’ that cannot start any token

As you can see I’m not to familiar with templates. Is my code correct or what should be changed?

your code is valid YAML

Did you ever figure this out? I read in one thread that people have not been able to get data_template to work inside service_template. All the working examples I’ve seen posted are just a simple service call with an entity_id, so perhaps this isn’t supported. But curious as to whether you made any progress after this post.

Yes I did get it working! Don’t know anymore what the fix was. Also do not use the code anymore because Snips (program that sends the intents) was way to slow for me. So I cannot show it…

But it certainly is possible to use both service template and data template

Hi All, i could use some help regarding using both service_template and data_template.

action:
    service_template: 
      >-
      {% if states.input_boolean.bed_light_alarm_trigger.state == 'on' %}
        switch.turn_on
      {% elif states.input_boolean.hall_light_alarm_trigger.state == 'on' %}
        mqtt.publish
      {% endif %}
    data_template: 
      >-
      {% if states.input_boolean.bed_light_alarm_trigger.state == 'on' %}
        entity_id: switch.jasco_products_14291_in_wall_smart_switch_switch_2_bedroom
      {% elif states.input_boolean.hall_light_alarm_trigger.state == 'on' %}
        topic: "hall/livingRoomLed/set"
        payload: "ON"
      {% endif %}

When i Added the if else logic as seen above i keep getting a configuration error.

Invalid config for [automation]: expected a dictionary for dictionary value @ data['action'][0]['data_template']. Got None. (See /config/configuration.yaml, line 48). Please check the docs at https://home-assistant.io/components/automation/

However if i were to remove the if else and only apply either the

data_template:
  entity_id:  switch.jasco_products_14291_in_wall_smart_switch_switch_2_bedroom

or

data_template:
  topic: "hall/livingRoomLed/set"
  payload: "ON"

its working fine. I dont understand why adding if else logic is breaking the code. Could use a new perspective.

Thanks

I’m having the same problem. Meaning:
Firstly: I’m using service_template to select serviceA or serviceB
Secondly: As service A has different set of arguments than serviceB, I’m using data_template do select appropriate argument sets.

- service_template: >
      {% if state_attr('media_player.spotify', 'source_list') | regex_search('device', ignorecase=FALSE) %}
      script.wlacz_spotify
      {% else %}
      media_player.select_source
      {% endif %}
      data_template: >
        {% if state_attr('media_player.spotify', 'source_list') | regex_search('device', ignorecase=FALSE) %}
        playlista: 'spotify:playlist:3z3UGazW15eN0WrKYRXRaS'
        urzadzenie: 'device'
        {% else %}
        entity_id: media_player.stereo_main
        source: 'net_radio'
        {% endif %}

Is it even possible? If yes, how?
If not, then what is the easiest way to get around this?

  1. You can’t template multiple fields. 1 template to 1 field only. This is a jinja limitation.

e.g.

Valid

playlista: "{{ if template... else.... }}"
urzadzenie: "{{ if template... else.... }}"

Not Valid

"{{ template }}"
   playlista: x
   urzadzenie: y
"{{ else }}"
   ...
  1. You can’t mix and match services if the attributes in data do not match.

The only way to make services match attributes is to make 2 separate scripts that accept values.

Personally, i’d change your wlacz_spotify script instead of creating a second one but here’s with the second one:

wlacz_spotify_2:
  sequence:
    service: media_player.select_source
    data_template:
      playlista: "{{ value1 }}"
      urzadzenie: "{{ value2 }}"

select_source:
  sequence:
    service: media_player.select_source
    data_template:
      entity_id: "{{ entity_id }}"
      source: "{{ value1 }}"

then your action…

- service_template: >
    {% if state_attr('media_player.spotify', 'source_list') | regex_search('device', ignorecase=FALSE) %}
      script.wlacz_spotify
    {% else %}
      script.select_source
    {% endif %}
  data_template:
    entity_id: media_player.stereo_main
    value1: >
      {% if state_attr('media_player.spotify', 'source_list') | regex_search('device', ignorecase=FALSE) %}
        spotify:playlist:3z3UGazW15eN0WrKYRXRaS
      {% else %}
        net_radio
      {% endif %}
    value2: >
      {% if state_attr('media_player.spotify', 'source_list') | regex_search('device', ignorecase=FALSE) %}
        device
      {% else %}
        Doesn't matter, your other script isn't using this.
      {% endif %}

Thank you for a swift reply.
I was afraid that my solution wouldn’t work.
About your approach: I haven’t tested it yet, but I have some doubts about arguments’ names. Shouldn’t they be named the same in both of the scripts? Otherwise how would HA know how to pair values with arguments?

they are named the same… entity_id, value1, and value2…

I’m sorry - I didn’t get it at first.
It works, thank you.

Yeah, you can pass any variable to a script. Doesn’t matter what the name is. If the script doesn’t use it, it doesn’t care and moves on.