Synchronising ESP (Tasmota) RGB LEDs with Hue Lights & creating working RGB Lists in HA

Background: I needed a way to synchronise my long standing Philips Hue / Living Color lights with some new TASMOTA driven RGBW bulbs, whenever the Philips Hue / Living Color remotes are used to change the light and color levels, instead of using the native HA interface.

This is mainly due to the reason that we (particularly my wife) still like to use these Philips remotes & have several distributed through the house, and even custom wall mounted a few of them.

To accomplish this, I use HA to watch for changes in the Hue bulbs which trigger automations to read the RGB values from the Hue Bulbs, and push those values to the TASMOTA driven RGBW bulbs over MQTT.

While setting up the mechanisms in HA to do this, I stumbled across the issue I saw others also had - in that we all seemed to be making the same mistake: assuming that a template expression would return the list of values as a list. It doesn’t, templates always return values as strings, not lists.

So while you can feed a RGB value to lights like this: rgb_color: [232, 187, 44] statically and it will work, if you do the same directly outputted from a template in what visually appears to be the same format, it will fail. (this was crazy frustrating before I figured out what was going on.)

This took me a little while to figure out my mistake, and then eventually how to build the proper solution. Seeing others also had the same challenge & difficulty finding solutions, I decided to share this for the benefit of others that struggle with it in the future - I hope it helps you.

I also welcome any improvements / better ways of doing this, if anyone knows of any.

Solution: First, the way to construct the sensor to monitor RGB & brightness levels of the Hue bulbs which I will sync too. After much trial and error, I figured the best way finally was to break up the RGB values right here as individual R, G, and B sensor functions and having that separate from the values presented as a whole string. The whole RGB values as a string will be used for a single trigger, where as the individual R, G, B values will be reconstructed as a list in the automation.

- platform: template
  sensors:
# used for displaying of RGB values on HA dashboard (useful for debugging/testing/info) and used by the automation trigger if changes occur
   dining_room_spot_1_rgb:
     value_template: "{{ state_attr('light.dining_room_spot_1', 'rgb_color')|replace('(','[')|replace(')',']')|replace(' ','') }}"
     friendly_name: Dining Room Light 1 RGB
# brightness level values
   dining_room_spot_1_bright:
     value_template: "{{ state_attr('light.dining_room_spot_1', 'brightness') }}"
     friendly_name: Dining Room Light 1 Brightness
# Specifically Red values
   dining_room_spot_1_red:
     value_template: "{{ state_attr('light.dining_room_spot_1', 'rgb_color') |replace('(','')|replace(')','')|replace(' ','')| regex_findall_index(find='(.*),.*,.*', index=0) }}"
     friendly_name: Dining Room Light 1 Red
# Specifically Blue values
   dining_room_spot_1_blue:
     value_template: "{{ state_attr('light.dining_room_spot_1', 'rgb_color') |replace('(','')|replace(')','')|replace(' ','')| regex_findall_index(find='.*,(.*),.*', index=0) }}"
     friendly_name: Dining Room Light 1 Blue
# Specifically Green values
   dining_room_spot_1_green:
     value_template: "{{ state_attr('light.dining_room_spot_1', 'rgb_color') |replace('(','')|replace(')','')|replace(' ','')| regex_findall_index(find='.*,.*,(.*)', index=0) }}"
     friendly_name: Dining Room Light 1 Green

Next is the automation side of this solution.

This specific automation triggers if the light is turned ON and/or if the RGB / brightness levels are changed using the Philips Hue Remote.

Note: There is a condition statement that’s commented out. You can ignore this, but if you run something like my Sunset Simulation & Alarm ( just posted here: How to win Spouse Support with a Sunset (or Sunrise) Simulation & Alarm ) you will need to enable this commented out section to avoid conflicts.

- alias: Downstairs Turn Tasmota1 on via Hue Diningroom Spot 1
  initial_state: true
  trigger:
    - platform: state
      entity_id: light.dining_room_spot_1
      from: 'off'
      to: 'on'
    - platform: state
      entity_id: sensor.dining_room_spot_1_rgb
    - platform: state
      entity_id: sensor.dining_room_spot_1_bright
  condition:
    condition: and
    conditions:
# This is remarked out, because this is only relevant to my Sunset script that runs, and unless you are running that, you won't need this.   
# I'll document my sunset script in another post, and link back to it here.
#      - condition: state
#        entity_id: script.sunset_warning_downstairs
#        state: 'off'
#
# This condition ensures the Hue Light is ON.
      - condition: template
        value_template: "{{ states('sensor.dining_room_spot_1_rgb') != 'None' }}"
  action:
    - service: light.turn_on
      data_template:
        entity_id: light.tasmota1_light
        brightness: "{{ states('sensor.dining_room_spot_1_bright') }}"
# This is the magic of correctly building a list and presenting it to the light itself.  
        rgb_color: ['{{ states.sensor.dining_room_spot_1_red.state | int }}','{{ states.sensor.dining_room_spot_1_blue.state | int }}','{{ states.sensor.dining_room_spot_1_green.state | int }}']

And of course, the automation to turn off the TASMOTA bulbs when the HUE bulbs are turned off via the Philips Remote

- alias: Downstairs Turn Tasmota1 off via Hue Diningroom Spot 1
  initial_state: true
  trigger:
    - platform: state
      entity_id: light.dining_room_spot_1
      from: 'on'
      to: 'off'
  action:
    - service: light.turn_off
      entity_id: light.tasmota1_light

Some caveats about this solution are worth mentioning.

HA updates with Hue / Living Color values at the rate the Philips Hub polls all lights on the system. We have about 40 lights downstairs and it seems that it takes about 60 seconds approx to poll all the lights. That means when we turn the lights on or off or change the color, it can take up to 60 seconds for the Tasmota lights to sync up. Interestingly, when turning off the lights, it is kind of an accidentally convenience courtesy timed-off delay… So you don’t have to exit a dark room.

However, the fewer lights you have on a Philips hub, the faster it polls - so in my wife’s office, we replicated this as well, and the updates are pushed to her TASMOTA based RGBW Desktop within just 10 seconds (as there’s only 3 Hue Bulbs in her office, with it’s own dedicated Hub)

If you run other scripts / automations to control the lights (like my sunset script) you’ll want to setup some conditions so two automations don’t end up fighting for control.

We’ve been using this setup for a week now, and it’s working really well. Let us know if this solution helps you. :slight_smile: