Change sensor icon based on state on/off

HI,

I have a set of mqtt sensors displaying the ‘state’. these aren’t binary sensors, but just regular sensors subscribed to the topic ‘state’:

- platform: mqtt
  state_topic: 'macaddress/powerswitch-zwave/4e6eeb01/state'
  name: "Tester state"
#  unit_of_measurement: "State"
  force_update: true

they show as:

05

as you can see, the icons display in the regular color, independent of the state. I would like these icons to change in yellow when On, and stay regular when Off.

just like the switches of the same devices:

47

of course the latter are switches and have that builtin, but maybe we can achieve this with the sensors too? Ive tried device_class but that is only for binary_sensors.

would be a nice feature for the custom-ui if not already available. hot-Icon when ‘On’, cold-icon when ‘Off’
@andrey @NotoriousBDG would you know if this can be achieved? Maybe theming? Ive also tried several value_templates, but they only change the value obviously, not the icon. I don’t know technical term for these coloring icons so finding a solution is a bit difficult… Maybe icon_template would be an option?

(ps the Off-badge in the picture are badges for extra_badge.scripts forcing the switches Off if the Hub doesn’t respond correctly. They don’t indicate the real state)
Thanks,
Marius

2 Likes

Just use a binary mqtt sensor instead of a regular one.

Thanks, as a matter of fact i have those too, but there’s technical reasons i need to read the state in a regular sensor.

just found @andrey’s answer to another post i didnt see before: Custom UI - icon color change - #2 by andrey and trying as we write…

working alright! just to find the correct colors, and maybe Glob that my state sensors…

3245

Cheers!
Marius

If you don’t mind explaining, what’s the reason for wanting an on/off state in non-binary form?

not at all.
There’s a bit of unregularity now and then in the results of my zwave sensors which i read off the Zwave Hub through mqtt. Also, i have several automations that use the states of these switches, and their power measurements. Some of these are system critical, so i must be 100% sure of the correct state they show.

some of the sensors, switches and automations of course are ‘derived’ or calculated based on the mqtt topics. So they burden the system by constantly processing these values, rather then only change on state_change. Also, retain values are very helpful for continuous state-streams (like power) but especially in the case of states they are only sent, well, on state changes.
In between restarts and reboots, that has proven to be a bit of a puzzle…showing/ or even rereading the correct state.state

Its a bit of a complicated thing, of which im not sure yet to have found the answer, though with the enormous help of @NotoriousBDG, i feel i’ve reached some ease…

I have a bunch of switches that combine templated sensors and several custom-ui extra_badges and value_templates…

Anyways, State, power and usage are the topics that are subscribed, so i need to have mqtt sensors for these separately. As a base for the other sensors and switches. They help me pindown the moment of miscommunication if en when that happens.

Cheers,
Marius

I suppose I still don’t understand why the state topic can’t be read as a binary sensor if it only publishes on/off values. For power and usage, sure. I wouldn’t suggest reading those as binary since they obviously aren’t, but state still seems binary in nature to me and it just looks like you’re essentially trying to mimic a binary sensor without using a binary sensor, based on the information I have.

solved!

use this:

sensor.tester_state:
  icon: mdi:test-tube
  templates:
    rgb_color: "if (state === 'on') return [251, 210, 41]; else return [54, 95, 140];"

or this, for all sensors of the same format, in customize_glob.yaml:

"sensor.*_state":
  templates:
    rgb_color: "if (state === 'on') return [251, 210, 41]; else return [54, 95, 140];"

for regular Hassio interface colors:
Tester On:
20

Tester Off:
50

11 Likes

Do you use this in customize.yaml? If yes, does it work with other states such as Online/Offline, Detected/Clear etc.

yes, at first i set the customization per sensor, but than i changed that into a global customization in customize_glob.
Havent tried any other states, but that shouldn’t matter, it will work, just check the state machine for your use,and you will see immediately.

I did customize my scripts the same way in Glob, and works like a charm:

“script.*_force_off”:
icon: mdi:power

renders:

47

so not only changing color, but also changing icon.

I tried on three different sensors but I could not get the result of changing color when the status changed.

show us the code and we might be able to help?

So here is my config:

device_tracker:
  - platform: ping
    hosts:
      raspberry_pi: 192.168.1.101

So i didn’t want to have on/off state of tracking my device instead I wanted online/offline so I made a sensor out of it.

sensor:
  - platform: template
    sensors:
      raspberry_pi:
        value_template: '{% if is_state("device_tracker.raspberry_pi", "home") %}Online{% else %}Offline{% endif %}'
        friendly_name: 'raspberry pi'

and finally in customize.yaml i have this:

customize:
  sensor.raspberry_pi:
    friendly_name: Raspberry Pi
    icon: mdi:raspberrypi
    templates:
      rgb_color: "if (state === 'Online') return [251, 210, 41]; else return [54, 95, 140];"

You have to check the status from entity_id, what is display. Some entity_id show on/off then use that state in customize.yaml

From customize
templates:
rgb_color: “if (state === ‘Online’) return [251, 210, 41]; else return [54, 95, 140];” <== this show the state Online and actually refer from entity_id it show home or not_home

sensor.yaml
From sensor.
value_template: ‘{% if is_state(“device_tracker.raspberry_pi”, “home”) %}Online{% else %}Offline{% endif %}’

Just to be sure, do you have custom UI installed and enabled?

Also, you can get rid of the extra template sensor and use the same custom UI to change the displayed value of the device tracker directly if you want.

Nope I did not have that installed and enabled.

I works perfectly now. Thanks a lot guys.

well, im back…

this wont work in my setup, please have a look:

device_tracker.hvbdiskstation:
  value_template: {% if is_state("device_tracker.hvbdiskstation", "home") %}Online{% else %}Offline{% endif %}
  templates:
  theme: >
    if (state === 'home') return 'green'; else return 'red';

showing Home as always, in standard black.

it really is home, no error there:

02
whats wrong?

It didn’t work on my device_tracker as well but it did on my sensors.

we need to ask @andrey if the customization for device_trackers doesn’t work like this?

Please have a look?

Marius

got some progress here:
using this:

device_tracker.hvbdiskstation:
  templates:
    state: if (state==='home') return 'Online' ; else return 'Offline';
    theme: >
      if (state === 'home') return 'green'; else return 'red';

renders:
45

the odd thing is, my device is in fact home, so the template must be wrong.
If i take out the state template, it shows fine. Nice green theme, albeit Home…

If you use state in templates it changes the actual state so you would need to use Online and Offline in the theme template. If you just want to change what the device tracker shows as without changing the actual state, use _stateDisplay instead of state.