How to use service_template with tap_action?

Hello, I am currently trying to have a script triggered by a button. Only I need here service_template or similar. This seems not to work directly on the button. The script should definitely not be outsourced. Does anyone have an idea?

type: button
tap_action:
  action: call-service
  service: climate.set_temperature
  service_data:
    entity_id: climate.hz_ankleidezimmer_group
    temperature: >-
      {{ state_attr('climate.hz_ankleidezimmer_group', 'temperature') | float +
      0.5 | float }}

hold_action:
  action: more-info
show_icon: true
show_name: true
entity: climate.hz_ankleidezimmer_group

As soon as I use service_template instead of service_data I get the error message: Error when calling the service climate/set_temperature. must contain at least one of temperature, target_temp_high, target_temp_low.

You can’t template in lovelace fields. Make a script and call the script instead.

script:
  set_hz_ankleidezimmer_group_temp:
    sequence:
    - service: climate.set_temperature
      data_template:
        entity_id: climate.hz_ankleidezimmer_group
        temperature: >
          {{ state_attr('climate.hz_ankleidezimmer_group', 'temperature') | float + 0.5 }}

Then in lovelace

type: button
tap_action:
  action: call-service
  service: script.set_hz_ankleidezimmer_group_temp
hold_action:
  action: more-info
show_icon: true
show_name: true
entity: climate.hz_ankleidezimmer_group
1 Like

Unfortunately, this is not an alternative. So I would have to create an almost identical script about 30 times. That would be the horror. I try my luck with the button card. Maybe it works by button_template.

There’s still plenty of ways to get what you want probably but you only asked about this single button. What is your intentions with all your buttons? Because you can tailor your script to run anything.

script:
  set_climate_group_temperature:
    sequence:
    - service: climate.set_temperature
      data_template:
        entity_id: "{{ climate }}"
        temperature: >
          {{ state_attr(climate, 'temperature') | float + step | float }}
type: button
tap_action:
  action: call-service
  service: script.set_climate_group_temperature
  service_data:
    climate: climate.hz_ankleidezimmer_group
    step: 0.5
hold_action:
  action: more-info
show_icon: true
show_name: true
entity: climate.hz_ankleidezimmer_group

I want to perform a script that perform the following so that I use it with tap_action in lovelace:

if states("media_player.googlehome7136") in ["off","on"]
  - data:
      uri: spotify:playlist:37i9dQZF1DX3yvAYDslnv8
      device_name: 'Living Room speaker'
    service: spotcast.start
elsif states("media_player.googlehome7136") in ["paused","playing"]
  - service: media_player.media_play_pause
    data: 
      entity_id: media_player.googlehome7136

I cant use service templating since both service have different data no? whats the best way to do that ?

Ive made that work using 3 scripts:

'spotcast_google_home':
  sequence:
    service: spotcast.start
    data:
      entity_id: media_player.googlehome7136
      uri: 'spotify:playlist:37i9dQZF1DX3yvAYDslnv8'
'play_pause_google_home':
  sequence:
    service: media_player.media_play_pause
    data:
      entity_id: media_player.googlehome7136
'play_pause_spotify_google_home':
  alias: play_pause_spotify_google_home
  sequence:
    service_template: >
      {% if states("media_player.googlehome7136") in ["off","on"] %}
      script.spotcast_google_home
      {% else %}
      script.play_pause_google_home
      {% endif %}

But Im not sure if thats the best way since I should create as many script as 3* entity ids ; can i template that ?

You can’t template it because uri is missing in the other service call. What you’ve done is the best approach.

Thanks Petro four your quick feedback , Indeed it would’ve been possible and more flexible if we’re able to template the whole data_template (similarly to the service_template) which means :

sequence:
    service_template: >
      {% if states("media_player.googlehome7136") in ["off","on"] %}
      servicea
      {% else %}
      serviceb
      {% endif %}
    data_template: >
      {% if states("media_player.googlehome7136") in ["off","on"] %}
      data.a1 = xxxx
      data.a2= yyyy
      {% else %}
      data.b = zzzz
      {% endif %}

currently we’re only able to template the values in the dictionnary in data-template

Yep, that’s a yaml limitation. Nothing can be done about it.

EDIT: You can do this with a python script or appdeamon. Just not jinja & yaml.

Thanks again , Indeed Ive never tried python with Hass , Ill try to do my first py that will do that