custom-cards / button-card

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

Some help with entity attribute appreciated #651

Closed ripvega closed 1 year ago

ripvega commented 1 year ago

I have tested the template below in HA Dev Tools and the logic is all correct.

`{%if (state_attr('climate.central_heating', 'hvac_action') == "off") %} white

{%elif (state_attr('climate.central_heating', 'hvac_action') == "idle") and (states('sensor.central_heating_socket_power') | int < 10) %} blue

{%elif (state_attr('climate.central_heating', 'hvac_action') == "heating") and (states('sensor.central_heating_socket_power') | int < 300) %} blue

{%elif (state_attr('climate.central_heating', 'hvac_action') == "heating") and (states('sensor.central_heating_socket_power') | int > 400) %} orange

{% endif%}`

When I try and use it to change icon color, as below, the first if is always true.

[[[ if (states['climate.central_heating'].attributes.hvac_action == "off") return 'white'; if (states['climate.central_heating'].attributes.hvac_action = "idle") and (states['sensor.central_heating_socket_power'].state < 10) return 'rgb(3, 123, 252)'; // blue if (states['climate.central_heating'].attributes.hvac_action = "heating") and (states['sensor.central_heating_socket_power'].state < 300) return 'rgb(3, 123, 252)'; // blue if (states['climate.central_heating'].attributes.hvac_action = "heating") and (states['sensor.central_heating_socket_power'].state > 400) return 'rgb(252, 86, 3)'; // orange ]]]

Please can you tell me where I am going wrong with the config?

kdw2060 commented 1 year ago

Was searching through the issues here for a related issue I'm experiencing. My guess is button card doesn´t update itself when an entity attribute changes, only when the entity state changes. You can force the updating behaviour with the triggers_update option, but I haven't found yet how to use an attribute there. Will wait a while for someone who might have a suggestion/solution thereon.

A workaround that's probably possible is to create a helper that changes state when the desired attribute changes, then use the helper entity in the triggers_update option.

kdw2060 commented 1 year ago

so, my issue was related and unrelated because I was also dealing with a nested button card. As for yours, you can try 2 things:

ripvega commented 1 year ago

Thank you.

As I was not using the same entity with the card, I ended up using a template sensor to determine the colour change.

It works well, thank you.