button-card
button-card copied to clipboard
Use variables in triggers_update
Is your feature request related to a problem? Please describe. The triggers_update feature is great, but it only works with static text. I have some fields that I define as variables for a template, but I have to list them again under triggers_update to get the card to update with them.
Describe the solution you'd like Process variables in the triggers_update field. Listing the fields is great, but let me reference the variable, as well.
Describe alternatives you've considered The status quo works, but is redundant.
Additional context Here's the template I'm working with:
control_motion:
variables:
motion_boolean: "input_boolean.null"
double_tap_action:
action: call-service
service: input_boolean.toggle
service_data:
entity_id: >
[[[ return variables.motion_boolean ]]]
custom_fields:
motion: >
[[[
if (states[variables.motion_boolean].state == 'on') {
return '<ha-icon icon="mdi:motion-sensor"></ha-icon>';
} else {
return '<ha-icon icon="mdi:motion-sensor-off"></ha-icon>';
}
]]]
In my UI code, I have to list the motion_boolean
field twice, both in variables
and in triggers_update
:
- entity: light.stairs_hallway
type: custom:button-card
name: Stairs/Hallway Lights
template:
- general
- inside
- control_motion
variables:
motion_boolean: input_boolean.motion_light_hall
triggers_update:
- input_boolean.motion_light_hall
The whole purpose of triggers update was to avoid unnecessary rebuild of the card (and if the parsing was not finding the appropriate entities). The problem with introducing a feature like the one you request is that for EVERY state change in HA, I'd have to parse templates and the whole thing would get sluggish again. I can probably come up with something that is lightweight though... Let me think about it.
I have no idea how this works behind the scenes, so please bear with me. 😄
Would it somehow be possible to just do a static insert of the raw variable value once? Instead of trying to parse it as actual JavaScript, loop through the values in the triggers_update
array, and if any of them starts with variables.
, replace it.
EDIT: Heh, seems like you might have been thinking about exactly that here already.. 🙂
Hi, I'd like to add my 2 cents: I do an intensive use of templates in my lovelace, working extremely with variables. I have button cards with 5 icons (just icons, not nested cards) inside, each with its logics related to a specific sensor that can be built starting from the main entity of the card. So, adding these sensors as triggers in the definition of the card means loosing power of template itself. In this case, js in triggers sections may help...
@RomRider, would it be feasible to track an array on this
of what you find in the parsing (I suspect you might already do that?) but when parsing states[...] you have a branch for states[variables.*] that fetches the value of the variable to add to the array? This feature would eliminate my quest for having javascript evaluation for triggers_update, which sounded like a more expensive and less intuitive thing to do. (my other question was here)
@RomRider, I should clarify that when I say variables I don't mean javascript variables. I mean the variables feature of your card. I think that might be easier to manage than javascript variables.
@RomRider, I was a bit tired and focused on my personal scenario and wasn't thinking about how the variables can themselves need javascript evaluation. I am better seeing the challenge with auto-discovery of entities.
I would just like to add that this would be very useful, even if just on the initial build of the card. Here's my example. I have a switch that reports power usage as well. However this is all done thru tasmota and when the switch turns on and off, it takes a predefined period of time for the power usage to update (defined in teleperiod in tasmota). So if i turn the switch on as an example, the power updates in say 10-15 seconds.. but by that the time state has already changed on the main entity (the switch itself) so the button card never catches the power usage update. I have the power usage displayed on the button itself. In addition, the power usage is represented by a sensor entity and i pass this entity as a button-card variable in my yaml config. Just like below:
- type: custom:button-card
entity: switch.topgreener2
name: Plant Tent Grow Light
icon: custom:ha-grow-light
template:
- power_reading_switch
variables:
secondary_info: "TopGreener Plug (Tasmota)"
monthly_power_sensor: "sensor.tasmota_topgreener_2_energy_total"
current_power_sensor: "sensor.tasmota_topgreener_2_energy_power"
So what happens here is switch goes on, but power still says 0W.. but when i click the switch off, the button card updates to 13W, even tho the swithc is now off, because it takes 'teleperiod' of time to update the power usage.
What i wanted to do is use the variables defined in the button card to be the 'triggers_update' so when they change, the card updates. However unfortunately i cannot get it to work for reasons for which i now understand (templating a list).
This is my template config (or some of it anyway:
power_reading_switch:
template:
- dimmer_cover_switch_base
triggers_update: >
[[[ return ['variables.current_power_sensor'] ]]]
tap_action:
action: call-service
service: switch.toggle
service_data:
entity_id: >
[[[ return variables.entity_id ]]]
haptic: medium
This doesnt work but you can get the idea of what id like to accomplish. That would be my use case anyway.. hope that all makes sense.
:tada: This issue has been resolved in version 4.1.0-dev.1 :tada:
The release is available on GitHub release
Your semantic-release bot :package::rocket:
:tada: This issue has been resolved in version 4.1.0 :tada:
The release is available on GitHub release
Your semantic-release bot :package::rocket: