@RomRider Yes, I waited but it never updated. When it’s working, it only takes a couple seconds to update. Another odd thing is that as long as I have the lovelace card in the UI editor, the buttons work correctly as well but as soon as I click “Save” and return to the page, I get the bad behavior I described
To try and debug further, I moved the color change to a “styles:” block instead of trying to associate it with state. Since the state of the hub doesn’t really change when the activity changes, I thought maybe it just wasn’t picking up the change. I also added some logging to the console. This is what my style block looks like (after the raw ui editor gets done with it):
styles:
icon:
- color: >-
[[[ console.log(entity); return (entity.attributes.current_activity
=== variables.var_activity ? "rgb(59,66,249)" : "rgb(239,239,239)");
]]]
That’s in the activity_button
template. What I see is that when I refresh the page, I see 9 dumps of the object (I have 3 activity buttons). Then I see no more updates if I just sit there for awhile. I then press another activity button and the TV switches, but I see no additional output in the console and I see no update in the UI. If I have the card opened in the UI editor and I click another activity button, I see 3 more outputs to the console and everything works.
If I put one of the activity buttons back to having all the config in the card, then I also see the console updating when I change activities even if I’m not choosing the button that has the full code in the card.
So, here is my current activity template:
activity_button:
variables:
var_name: Activity Name
var_activity: Activity
entity: remote.harmony_hub
name: '[[[ return variables.var_name; ]]]'
color_type: icon
icon: 'mdi:remote'
styles:
icon:
- color: >-
[[[ console.log(entity); return (entity.attributes.current_activity
=== variables.var_activity ? "rgb(59,66,249)" : "rgb(239,239,239)");
]]]
tap_action:
action: |
[[[
return (entity.attributes.current_activity === variables.var_activity ? 'none' : 'call-service' );
]]]
service: remote.turn_on
service_data:
entity_id: '[[[ return entity.entity_id; ]]]'
activity: '[[[ return variables.var_activity; ]]]'
hold_action:
action: none
double_tap_action:
action: none
And this is the code in the card definition:
- cards:
- type: 'custom:button-card'
template: activity_button
variables:
var_name: Watch TV
var_activity: Watch TV
entity: remote.harmony_hub
- type: 'custom:button-card'
entity: remote.harmony_hub
name: Watch Mi Box
state: null
styles:
icon:
- color: |-
[[[
if (states['remote.harmony_hub'].attributes.current_activity === "Watch Mi Box")
return 'rgb(59,66,249)';
else
return 'rgb(239,239,239)';
]]]
tap_action:
action: |
[[[
var is_on = (states['remote.harmony_hub'].attributes.current_activity === 'Watch Mi Box');
return (is_on ? 'none' : 'call-service' );
]]]
service: remote.turn_on
service_data:
entity_id: remote.harmony_hub
activity: Watch Mi Box
hold_action:
action: none
double_tap_action:
action: none
- type: 'custom:button-card'
template: activity_button
variables:
var_name: Watch OTA
var_activity: Watch OTA
entity: remote.harmony_hub
One difference between the card definition and the template is that for some reason, I find the entity
object is never defined within the card configuration, so I have to use the states
object to determine the current activity. That was going to be a separate question after I sorted all this out.
At this point I’m pretty baffled. I don’t understand why having just one button defined directly in the card makes everything work perfectly. I see no exceptions or errors in the console. Next I think I’ll try removing the variables from the template. Since I’m only using 2, I think I can easily override those values in the card without using variables.