button-card icon indicating copy to clipboard operation
button-card copied to clipboard

Use templates in tap_action

Open redfusion83 opened this issue 1 year ago • 4 comments

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!

redfusion83 avatar Jul 04 '23 11:07 redfusion83

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

Pico1965 avatar Jul 17 '23 12:07 Pico1965

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

Mariusthvdb avatar Jul 17 '23 12:07 Mariusthvdb

Many tanks!!

Pico1965 avatar Jul 17 '23 12:07 Pico1965

@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}`; ]]]

RomRider avatar Jul 23 '23 23:07 RomRider