Switch sensor - color change doesn't work

Hello,

I would like to display the status of a switch (I don’t want to be able to power it on/off), but I would still like to have a different icon-color dependent on the state. For this, I have created the following sensor:

sensor.yaml:

  - platform: template
    sensors:
      steckdose_wohnzimmer_statusonoff:
        value_template: '{{ (states.switch.steckdose_wohnzimmer.state) }}'

customize.yaml:

sensor.steckdose_wohnzimmer_statusonoff:
  icon: mdi:power
  friendly_name: 'Steckdose Wohnzimmer'
  templates:
    rgb_color: >
      if (state === 'On') return 'rgb(0,255,0)';
      return 'rgb(54, 95, 140)';

The sensor is working, but the color doesn’t change. I have tried many different variations I found in the forum, but none of them is working. Any ideas?

Thank you!

A couple of notes:

  • By default, HA does not support changing the icon colour. There is an extension that may support this.
  • If you already use a template sensor, then it’s typically easier to do everything in one place (instead of doing some things through customisation).
  • There is a much simpler solution (if you do not insist on the particular colour change): Create a binary template sensor, and pick a suitable device class. That way, the icon automatically changes colour, and you have a nice icon, and it’s much easier to use this binary sensor state for example in automations.

Thanks for your hint, the binary template sensor - solution sounds good, but I can’t get the current state of my switch… with the regular sensor, the above sensor was working, but with the binary sensor, the state is always stays off, even when the switch is on.

Do I have to change the “value_template” - part to something else? I also tried “value_template: switch.steckdose_wohnzimmer” and some other variations, but it’s not working…

Thank you!!

configuration.yaml:

binary_sensor: !include binary_sensor.yaml

binary_sensor.yaml:

  - platform: template
    sensors:
      steckdose_wohnzimmer_statusonoff:
        value_template: '{{ (states.switch.steckdose_wohnzimmer.state) }}'
        friendly_name: 'Steckdose Wohnzimmer'
        device_class: power

This should work:

        value_template: "{{ is_state('switch.steckdose_wohnzimmer', 'on') }}"

Reason is that the switch state is on or off, but the template’s result must be true or false.

That’s it, thank you!!

Is anyone else having issue with the binary sensor color state no longer changing yellow? All my binary sensors remain the same color regardless of state (i.e. open/closed, motion/none, on/off, etc). State icon changes appropriately. HA version 106.2

See here for more details:

1 Like