Hello,
for a while, I am struggling with a configuration of the button card running a script on tap action. I want to run the script which checks the state of the switch and power consumption and either turn off or show confirmation.
Firstly, I would like to use variables within the card configuration but I don’t know how. I tried to use variables.sensor in label state definition, but it shows only the name of the variable and not the value of the entity.
Secondly, I do not know how to pass variables to the script and use them there.
Can you help me, please?
button card config
- type: custom:button-card
entity: switch.shenzhen_neo_electronics_co_ltd_power_plug_12a_switch
variables:
sensor: sensor.shenzhen_neo_electronics_co_ltd_power_plug_12a_power
triggers_update: '[[[ return variables.sensor ]]]'
name: Oven
icon: mdi:toaster-oven
show_label: true
show_name: true
show_state: false
show_icon: true
color_type: icon
tap_action:
action: call-service
service: script.plug_toggle
service_data:
switch: entity
sensor: '[[[ return variables.sensor ]]]'
hold_action:
action: toggle
confirmation:
text: Are you sure you want to immediate toggle?
aspect_ratio: 1/1
styles:
card:
- padding: 5%
name:
- color: white
state:
- value: 'on'
operator: '=='
label: >
[[[
if (states['sensor.shenzhen_neo_electronics_co_ltd_power_plug_12a_power'].state < 10) return "STANDBY";
else return 'ON';
]]]
styles:
icon:
- color: >
[[[
if (states['sensor.shenzhen_neo_electronics_co_ltd_power_plug_12a_power'].state > 10) return 'green';
else return 'orange';
]]]
- value: 'off'
operator: '=='
label: 'OFF'
styles:
icon:
- color: grey
card:
- color: grey
script
plug_toggle:
alias: Plug toggle
description: "If consumption is below 10 Watts script allows to turn off the plug"
sequence:
- service_template: >
{% if is_state('entity_id', 'on') and states('sensor') < 10 %}
switch.toggle
{% elif is_state('entity_id', 'off') %}
switch.toggle
{% else %}
notify.notify
{% endif %}
data_template:
entity_id: '{{ switch }}'
sensor: '{{ sensor }}'
Thank you