custom-cards / button-card

❇️ Lovelace button-card for home assistant
MIT License
1.93k stars 233 forks source link

Use templates in tap_action #712

Open redfusion83 opened 1 year ago

redfusion83 commented 1 year ago

I want to create a button that sends a MQTT command to raise a value by 1. However, when I use the javascript templates, they are submitted as plain text in the MQTT command.

Example Code:

tap_action:
  action: call-service
  service: mqtt.publish
  service_data:
    topic: command
    payload: >-
      {"CMD":0,"VALUE":'[[[ return
      states["sensor.target_temp_c"].state+1]]]',"XTIME":0,"INTERVAL":0}

This creates the following MQTT command:

{"CMD":0,"VALUE":'[[[ return states["sensor.target_temp_c"].state+1]]]',"XTIME":0,"INTERVAL":0}

Sorry if I did something wrong, then I'd be happy for a correction. Thanks!

Pico1965 commented 1 year ago

According to the documentation, tap_action cannot use templates.

But I too would need to use templates in the tap_action function.

My scenario:

Several buttons in a section have: [...] tap_action: action: toggle

I added a button that enables all those in the section. So if the master button is disabled, all buttons below it must switch to:

[...] tap_action: action: none

Mariusthvdb commented 1 year ago

According to the documentation, tap_action cannot use templates.

But I too would need to use templates in the tap_action function.

My scenario:

Several buttons in a section have: [...] tap_action: action: toggle

I added a button that enables all those in the section. So if the master button is disabled, all buttons below it must switch to:

[...] tap_action: action: none

sure it can, see: https://github.com/custom-cards/button-card/issues/713#issuecomment-1628875427

Pico1965 commented 1 year ago

Many tanks!!

RomRider commented 1 year ago

@redfusion83, it should be:

tap_action:
  action: call-service
  service: mqtt.publish
  service_data:
    topic: command
    payload: [[[ return `{"CMD":0, "VALUE": ${states["sensor.target_temp_c"].state+1}, "XTIME": 0, "INTERVAL": 0}`; ]]]