Custom Sensor and colour changing

Hello

I have been trying for ages to get some ping sensors to change colour when they are online of offline. So far I have failed! Anyone know why please? This is what I currently have

Config:

Sensor:
  - platform: template
    sensors:
      arcade_state:
        value_template: '{% if is_state("device_tracker.arcade", "home") %}Online{% else %}Offline{% endif %}'   
        friendly_name: 'Arcade Machine'    

customize.yaml

binary_sensor.arcade_state:
  friendly_name: Arcade Machine
  icon: mdi:desktop-classic
  templates:
    icon: >
    rgb_color: "if (state === 'Online') return [251, 210, 41]; else return [54, 95, 140];"

I’ve tried Binary sensors, globs, etc but nothing changes the colour of the icon.

Any idea please?

Cheers
Mark

You had an extra “icon:>” for some reason.

binary_sensor.arcade_state:
  friendly_name: Arcade Machine
  icon: mdi:desktop-classic
  templates:
    rgb_color: "if (state === 'Online') return [251, 210, 41]; else return [54, 95, 140];"

thanks… same problem. I am starting to think this was pulled in 0.66 and I have to use Custom UI?

I’m not sure, there’s a guy who uses this all the time. Maybe he knows. @Mariusthvdb any ideas why this isn’t working?

i always check 2 things:

  • is the state being referenced the correct one? in this case, does the binary sensor really show state Online and Offline?
  • does it work in Chrome. Im a Safari user, but many customizations of this kind only show correctly in Chrome.

Example:

customization for a binary_sensor:

binary_sensor.asus_router:
  templates:
    theme: >
      if (state === 'on') return 'green';
      return 'red';

Chrome:

08

Safari:

23

dev_state

the rgb color is done like this (change the color of the icon, if you want the whole line to change, use theme):

sensor.audio_auditorium_state:
  templates:
    icon: >
      if (state === 'on') return 'mdi:music';
      return 'mdi:music-off';
    icon_color: >
      if (state === 'on') return 'rgb(251, 210, 41)';
      return 'rgb(54, 95, 140)';

You can also use the binary_sensor directly for this, dont need a template sensor:

##########################################################################################
# Binary sensors
# https://home-assistant.io/components/binary_sensor


##########################################################################################
# https://home-assistant.io/components/binary_sensor.ping/
##########################################################################################

- platform: ping
  name: Asus router
  host: 192.168.1.1

in which case you can customize as follows:

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

Cheers,
Marius

1 Like

thank you so much… mine aren’t set as Binary Sensors. Does that matter? how does your Asus router binary sensor look please?

i have 3 :wink:

20

I use the ping platform next to the Asuswrt in Device_trackers, creating both devices in the known_devices. Add that to the binary_sensor, and you have 3 sensors checking the status. believe me, they’re not always showing the same state, so having 3 ways to check an be helpful with the system critical devices

- platform: ping
  interval_seconds: 20
  consider_home: 60
  hosts:
    ping_asus_router: 192.168.1.1

05

thank you… This works great now thanks… Cannot get RGB to work… thank you for the detailed reply! You do need custom UI installed for it to work.

binary_sensor.arcade:
  templates:
    icon_color: >
      if (state === 'on') return 'black';
      return 'red'
    _stateDisplay: >
      if (state === 'on') return 'Online';
      return 'Offline';
device_tracker.arcade:
  templates:
    icon: >
      if (state === 'home') return 'mdi:music';
      return 'mdi:music-off';
    icon_color: >
      if (state === 'home') return 'black';
      return 'blue';
    _stateDisplay: >
      if (state === 'home') return 'Online';
      return 'Offline';