Light color to match battery voltage

I have a fairly basic setup to change the color of one of my house lights to green/yellow/orange/red depending on a coarse voltage trigger. So the battery sensor gives me +20A, light is green, +15 yellow, -10 red.
The gauge card on lovelace has a define severity feature, which essentially does the same style of thing.
Would there be a way to link the colour of the light with the voltage in a bit more fluid way?

Use the hs_color attribute.

Set the saturation at 100% and vary the hue (0-120° for a red-yellow-green range) depending on your current.

A linear equation based on your red and green values gives:

hue == 4*Amps + 40

This puts yellow at 5A though.

1 Like

That is definatly going the direction I was thinking…

Can I set this via a template ??

Once I get the workings of how to layout the yaml or whatever I can figure the maths

Well using my math above:

- service: light.turn_on
  target:
    entity: light.your_light
  data:
    hs_color: 
      - "{{ 4 * states('sensor.your_current_sensor')|float + 40 }}"
      - 100

Thanks very much

We should limit the hue to allowed values, just in case:

- "{{ ([0, (4 * states('sensor.your_current_sensor')|float + 40), 360]|sort)[1] }}"
2 Likes

Just in case anyone else is interested, I added in the sensor for the charge state and the result is:

"{{ ([0, (4 * states('sensor.victron_battery_current')|float + 40)- 
-8 * (states('sensor.victron_battery_state_of_charge')|float -100), 120]|sort)[1] }}"

Gives a nice smooth transition from red to green with some priority to charge state over current.

Thank you tom.

1 Like