Hi, I’m having a problem changing the background of a button card. Allow me to explain my problem in detail.
Imagine two buttons, both of which will set the scene of my Hue lights.
Button 1 changes to scene = “wohnzimmer_tv_bloody_sunday”
Button 2 changes to scene = “wohnzimmer_tv_miami_vice”
I’ve created a script that I send the scene name to (more or less working generically) scenes changing perfectly.
The problem is I need a way to show visually which scene is active. So I thought I might be able to do it while giving the background of the button a gradient. The thing is how do I know which scene is active?
Something like this (which does not work) of course:
entity: input_select.aktive_szene
tap_action:
action: call-service
service: script.activate_scene
service_data:
scene_name: wohnzimmer_tv_bloody_sunday
styles:
card:
- padding: 20px
- background: >
{% if is_state('input_select.aktive_szene',
'wohnzimmer_tv_bloody_sunday') %}
linear-gradient(135deg, #ff6a00, #ee0979)
{% else %}
black
{% endif %}
I guess you get the idea. I’m relatively new to all of this so it’s a bit hard to wrap my head around it.
This is the script:
activate_scene:
description: "Aktiviere eine Szene und setze den Status in input_select"
fields:
scene_name:
description: "Name der Szene"
example: "bloody_sunday"
sequence:
- action: persistent_notification.create
data:
title: "DEBUG MSG"
message: "Übergebener scene_name: {{ scene_name }}"
- action: scene.turn_on
data:
entity_id: "{{ 'scene.' ~ scene_name }}"
- action: input_select.select_option
data:
entity_id: input_select.active_scene
option: "{{ scene_name }}"
The input helper holds the available scenes.
Can you clarify what card type you are using? Most core cards do not support the use of templates, but some custom cards do.
Hello,
Yes, of course, my mistake, I should have mentioned that in the first post.
type: custom:button-card
After spending several hours on this problem yesterday, I actually managed to change the background. Also, there was a problem in my script that was easy to fix.
This is what I have so far:
entity: input_select.aktive_szene
tap_action:
action: call-service
service: script.activate_scene
service_data:
scene_name: wohnzimmer_tv_miami_vice
styles:
card:
- padding: 20px
- background: >
[[[ return states['input_select.aktive_szene'].state ===
'wohnzimmer_tv_miami_vice'
? 'var(--active-big)'
: 'black'
]]]
That works fine, one last step is missing, I need to implement another check to make sure the light is on, otherwise the gradient needs to be removed again, but that shouldn’t be a big deal. Thanks for your attention really appreciated!
Sorry, what is a purpose of that input select entity? Only to show a current scene? Or to select a scene as well?
Basically the script changes the scene name of the input select (its kind of a workaround) assume one light 4 buttons for 4 different scenes I needed a way to indicate the scene change otherwise all 4 buttons would have a gradient as background (indicating they are active) that wouldn’t make sense.
Using commas will make your post clearer.
If you are using input select only to show an active scene - consider using a template sensor instead, keeping either a name of the active scene or something like “Nothing” if none scene is activated.
And in the button-card, you may check a state of this template sensor and compare it to a scene name.
Alternatively, keep using the input select filled with names of scenes.
Create an automation triggered by a change of this input select entity, then activate a selected scene in this automation.
In button-card tap-action: select a corresponding option of input select.
Using commas will make your post clearer.
Well while this may be correct and understandable you might consider that english is not my first language nor I’m a fluent English speaker but I try my best.
Anyways thank you for the tips
I’ve got everything working the way it should in the meantime. What’s not clear to me is why I should use an automation when a script does the job just fine? Just for clarification…
Have you considered his first language might not be English?
Your comment is pretty pointless, sorry to say…
Why should I consider what language he speaks or do you see a comment from me somewhere where his English bothers me?
My doubts about input select are caused by a fact that it is purposed for INPUT.
Using it only to keep a value (set by a script) is a bit unusual.
Thank you very much for your answer and the valuable tips.
I need to familiarize myself further with this very complex topic. I’m still at the very beginning.
At the moment I’m just happy that everything is working as expected.
I think once I’ve got to the point where I understand the whole system well, I’ll definitely improve a few things.
1 Like