You thought about it, do IT !

Home Assistant – Automation based on the color of an RGB light

Home Assistant – Automation based on the color of an RGB light

It’s difficult finding documentation about making an Home Assistant automation based on the color of a RGB light.

I finaly found a way to make it work. You will have to use a template (You can test it on developpement tools)

My goal was to have a RGB led with red color when electricity comes from electricity provider, with progressive intensity based on the consumption.

Color had to be green when self-consumption, and blue when sending electricity to my provider (Always with progressive intensity)

In home asssistant i had to make this automation for red :

  • when –> each second of each hour of each day
  • if –> imported electricity > 0 (Use your sensor) and not (the intensity i need and the color i need, value of 0 to 255 base on a maximum intensity at 2300W)
  • do –> set good color and intensity.

Here is the yaml code :

alias: RED INDICATOR - Active red light with progressive intensity when using provider electricity
description: ""
trigger:
  - platform: time_pattern
    seconds: /1
condition:
  - condition: numeric_state
    entity_id: sensor.imported_power
    above: 0
  - condition: or
    conditions:
      - condition: not
        conditions:
          - condition: state
            entity_id: light.multisensor1_led_rgb
            attribute: brightness
            state: >
              {% set consumption = states('sensor.imported_power') |
              int %} {% set intensity = (consumption / 2300) * 255 %} {% if
              intensity > 255 %}
                255
              {% else %}
                {{ intensity | int }}
              {% endif %}
      - condition: not
        conditions:
          - condition: template
            value_template: >-
              {%- if state_attr('light.multisensor1_led_rgb', 'rgb_color')[0]==
              255 and state_attr('light.multisensor1_led_rgb', 'rgb_color')[1]==
              0 and state_attr('light.multisensor1_led_rgb', 'rgb_color')[2]== 0
              -%} True {% else %} False {% endif %}
        enabled: true
action:
  - service: light.turn_on
    metadata: {}
    data:
      color_name: red
      brightness: >
        {% set consumption = states('sensor.imported_power') | int %}
        {% set intensity = (consumption / 2300) * 255 %} {% if intensity > 255 %}
          255
        {% else %}
          {{ intensity | int }}
        {% endif %}
    target:
      entity_id: light.multisensor1_led_rgb
mode: single

Use the same syntax for the green and blue automation with adequates sensors.

do-it.dev Avatar
if you liked it, please contribute !

Leave a Reply

Your email address will not be published. Required fields are marked *

Aurélien Bordinat

I am a french computer engineer, adept of DIY, home automation and new technologies. When i’m not busy trail running, i spend some time prototyping.