I have been following this excellent guide:
And modifying it a bit, such that I can use it for my fridge:
I have been trying so many permutations on the states, is_state, is_state_attr, and whatnot, but I cannot figure out how to make the colour of the upper left icon change to red when the fridge is open. You will note that the text changes, but not the colour. Here is the snippet I have been working with:
- type: state-label
entity: binary_sensor.ag514278_door_status_any_open
style:
background-color_template: >-
{% if is_state_attr('binary_sensor.ag514278_door_status_any_open',
'Open', true) %}
#9C27B0
{% else %}
#4CAF50
{% endif %}
width: 25%
top: 2%
left: 0%
border-top-right-radius: 7px
border-bottom-right-radius: 7px
border: 2px solid rgba(0, 0, 0, 0.2)
padding: 5px
font-size: 16px
line-height: 16px
color: black
transform: translate(0%,0%)
What am I missing here, I have gone through so many different variants on the background-color_template
and I canât figure out how to get the colour to trigger off of Open
and âClosedâ.
Are you sure you have a state attribute for binary sensor as Open and its value is True or False? Please share state attribute of the binary sensor to get a better understanding.
I have been trying to many various permutations, I have been trying tons of different options, and I am now out on the fringes with true or false.
The entity itself will only output two states: âOpenâ and âClosedâ.
I have tried variations on:
{% if states("binary_sensor.ag514278_door_status_any_openâ ,âOpenâ %}
{% if is_state("binary_sensor.ag514278_door_status_any_openâ ,âOpenâ %}
I have also played around with the various quotes, and quoting.
I am clearly missing a step here, because it always defaults to the else statement - and I can tell immediately when the template is out of whack as the colour of the upper left icon changes from Green to Grey when the template has a typo.
Go with second one but instead of using Open, just use on
.
Binary sensors are always on/off but they might be visible to user in different ways depending on their device class ( open/close motion detected/no motion power on/off etc). To see the actual state value, rely on developer tools page.
1 Like
That was it! Thats the missing bit!
This code works now:
- type: state-label
entity: binary_sensor.ag514278_door_status_any_open
style:
background-color_template: >-
{% if is_state('binary_sensor.ag514278_door_status_any_open', 'on')
%}
#D22B2B
{% else %}
#4CAF50
{% endif %}