Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use variables in triggers_update #368

Closed
libots opened this issue Aug 24, 2020 · 9 comments
Closed

Use variables in triggers_update #368

libots opened this issue Aug 24, 2020 · 9 comments

Comments

@libots
Copy link

libots commented Aug 24, 2020

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
@libots libots added the FR Feature Request label Aug 24, 2020
@RomRider
Copy link
Collaborator

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.

@slovdahl
Copy link

slovdahl commented Nov 26, 2021

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.. 🙂

@ohipe
Copy link

ohipe commented Jan 31, 2022

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...

@ktownsend-personal
Copy link

ktownsend-personal commented Apr 1, 2022

@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)

@ktownsend-personal
Copy link

@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.

@ktownsend-personal
Copy link

@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.

@u8915055
Copy link

u8915055 commented Apr 3, 2022

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.

github-actions bot pushed a commit that referenced this issue Jul 30, 2023
## [4.1.0-dev.1](v4.0.1...v4.1.0-dev.1) (2023-07-30)

### Features

* **templates:** Support for one time evaluation of js templates in `triggers_update` and `entity` ([#741](#741)) ([b372642](b372642)), closes [#618](#618) [#558](#558) [#368](#368)

### Bug Fixes

* Keep default background color for color_type: card when off ([41dea3f](41dea3f)), closes [#737](#737)
@github-actions
Copy link

🎉 This issue has been resolved in version 4.1.0-dev.1 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

github-actions bot pushed a commit that referenced this issue Aug 3, 2023
## [4.1.0](v4.0.1...v4.1.0) (2023-08-03)

### Features

* **templates:** Support for one time evaluation of js templates in `triggers_update` and `entity` ([#741](#741)) ([b372642](b372642)), closes [#618](#618) [#558](#558) [#368](#368)

### Bug Fixes

* Keep default background color for color_type: card when off ([41dea3f](41dea3f)), closes [#737](#737)
* lock would not display on many browsers ([245441b](245441b)), closes [#740](#740)
* remove margin-top on name/label/state introduced with 4.0.0 which broke many cards ([53fee75](53fee75)), closes [#742](#742) [#744](#744)
@github-actions
Copy link

github-actions bot commented Aug 3, 2023

🎉 This issue has been resolved in version 4.1.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants