Question about data_template and service_template

Hi and as this is my first post: Thanks a lot for making this great project! I just switched from openHAB and I am impressed. Quick to get started, great results, great integrations!

Perhaps you can help me with some code that is not working:

- alias: Schlafzimmer Musikquelle einstellen
  description: ''
  trigger:
  - platform: state
    entity_id: input_select.schlafzimmer_nutzer

  condition: []

  action:
  - service: snapcast.unjoin
    entity_id: media_player.snapcast_client_b8_27_eb_f6_bf_a6
  - service_template: >
      {% if is_state("input_select.schlafzimmer_nutzer", "bjoern") -%}
        media_player.select_source
      {% elif is_state("input_select.schlafzimmer_nutzer", "maira") -%}
        snapcast.join
      {% endif %}
    entity_id: media_player.snapcast_client_b8_27_eb_f6_bf_a6
    data_template: >
      {% if is_state("input_select.schlafzimmer_nutzer", "bjoern") -%}
        source: bjoern
      {% elif is_state("input_select.schlafzimmer_nutzer", "maira") -%}
        master: media_player.snapcast_client_b8_27_eb_9f_ef_37
      {% endif %}

Basically I want to call media_player.select_source with the source: bjoern - If Maira is using the player, it should simply connect to another snapcast instance.

My solution is not working. Checking the config results in some for me cryptic:

Invalid config for [automation]: expected a dictionary for dictionary value @ data['action'][1]['data_template']. Got None. (See /config/configuration.yaml, line 132). 

What am I doing wrong?

Thanks for help!

You can’t template objects like you’re trying to do, you can only use templates within text fields. service_template works like you’re showing because at the end of the day your template results in one text value. With data_template though you’re trying to customize the fields of the object itself using a template, that won’t work. data_template just lets you use templates within the fields of that object.

So to give an example, this is how data_template is intended to be used:

 data_template:
   source: >-
     {% if is_state("input_select.schlafzimmer_nutzer", "bjoern") %}
       bjoern
     {% elif is_state("input_select.schlafzimmer_nutzer", "maira") %}
       media_player.snapcast_client_b8_27_eb_9f_ef_37
     {% endif %}

See how at the end of the day the template results in one piece of text that is then used to set the value of the source field? That’s how data_template works.

Of course that means it doesn’t work for your use case since you need to provide either the source field or the master field as data depending on the service you’re calling. Since the data s different per service you’re going to need to separate actions. And since HA automations don’t have if-else support for actions you’re going to have to move one to a separate script. I believe the best way to do this is like this:

automation:
- alias: Schlafzimmer Musikquelle einstellen
  description: ''
  trigger:
  - platform: state
    entity_id: input_select.schlafzimmer_nutzer

  condition: []

  action:
  - service: snapcast.unjoin
    entity_id: media_player.snapcast_client_b8_27_eb_f6_bf_a6
  - service: script.snapcast_join
    data: {}
  - condition: state
    entity_id: input_select.schlafzimmer_nutzer
    state: bjoern
  - service: media_player.select_source
    entity_id: media_player.snapcast_client_b8_27_eb_f6_bf_a6
    data:
      source: bjoern

script:
  snapcast_join:
    alias: Snapcast Join
    sequence:
    - condition: state
      entity_id: input_select.schlafzimmer_nutzer
      state: maira
    - service: snapcast.join
      entity_id: media_player.snapcast_client_b8_27_eb_f6_bf_a6
      data:
        master: media_player.snapcast_client_b8_27_eb_9f_ef_37

So what’ll happen then is first it will call your script which will either immediately end or proceed depending on if the value of your input_select is maira or not. Then it will continue but only if hte value is bjoern.

It’s a bit less elegant then what you were showing but with different data being input for each service I’m not sure if there’s an easier way to do it. Maybe someone else has a better trick here.

1 Like

Thanks for your answer. i think I get the idea. See if this works.

And it works. Thanks a lot!

1 Like