Setting light RGB values based on MQTT payload data

I have some Yeelight RGB bulbs which I would like to set the colour of via MQTT.

Does anyone know if it is possible to create an automation that will extract the RGB and brightness values from a MQTT message and pass them to a service command to update the bulb?

I think it should be possible using an automation something like this:

- id: light-mqtt-set-color
  alias: Lounge Main Set Color
  - platform: mqtt
    topic: /home/light
    service: yeelight.set_color_scene
      entity_id: light.lounge_main_1
      rgb_color: '{{ trigger.payload }}'
      brightness: 100

I submit a MQTT message with the rgb value in the body of the message like: [10,10,10]

But this results in the error:
“Invalid data for call_service at pos 1: None for dictionary value @ data[‘rgb_color’]”

Can anyone suggest what i’m doing it wrong or if this is even possible?

Using a template to set rgb_color is complicated by the fact the value must be presented as a python list.

     rgb_color: [254, 124, 213]

The challenge to create the list is that a template produces a string. So even if the payload contains [10, 10, 10] which looks like a list, this template {{ trigger.payload }} will render it as a string. The result will be the error message you received.

I haven’t tried this but there’s a new Jina2 filter in the latest release of Home Assistant called from_json. Just for fun, try this:

      rgb_color: '{{ trigger.payload | from_json }}'

If it fails to work then there’s another (tried and true) way to fix this issue.

1 Like

This would be so much easier if my PR was accepted, or even looked at! (Yes, it needs to be rebased, but I did that twice and it never even got a comment. Doesn’t want to make me go through the effort again, only to be ignored again.)

1 Like

Clearly your PR is not a case of ‘no news is good news’. With zero commentary, one is left wondering why it’s being overlooked. It’s far from being a disruptive PR so it should have been an easy-in. :man_shrugging:

I just installed version 0.101.3 and tested the new from_json filter.

The following example shows:

  • a valid list
  • a string that has the appearance of a list
  • a string converted to a list using from_json

Only the first and third templates correctly extract the desired item (145). The second one fails because the variable y is a string, not a list.

Based on those encouraging results, this ought to work:

      rgb_color: '{{ trigger.payload | from_json }}'