Cant get RGB to set via an automation triggered by MQTT

- id: ledvance_a19_rgbw_00060a5e_1_light_color
  alias: ledvance_a19_rgbw_00060a5e_1_light_color
  initial_state: 'off'
  trigger:
  - platform: mqtt
    topic: "/HAwirelesslink/return/light/ledvance_a19_rgbw_00060a5e_1/rgb_color"
  action:
  - data_template: 
      entity_id: >
        light.ledvance_a19_rgbw_00060a5e_1
      rgb_color: >
        {{ trigger.payload | list }}
    service:
      light.turn_on

I am trying to get an RGB bulb to set its color based on an MQTT message formatted 255, 255, 255 and I cannot for the life of me get it to accept it. Above is what I ended with before deciding I needed some help, the automation triggers fine, but it throws the error below. I think there is something I am just missing in the formatting of it all, but this is my first go at lists and RGB so I am a little in the weeds. Thanks for any help!

Error while executing automation automation.ledvance_a19_rgbw_00061a28_1_light_color. Invalid data for call_service at pos 1: None for dictionary value @ data['rgb_color']

Put this into the Template Editor and see if it produces the result you were expecting:

{{ '255, 255, 255' | list }}
{% set rgb = 255,255,255 %}

{{ rgb | list }}

but it looks like with a string, it is not what I want. Any thoughts on how to get the trigger.payload formatted correctly?

Try this:

- id: ledvance_a19_rgbw_00060a5e_1_light_color
  alias: ledvance_a19_rgbw_00060a5e_1_light_color
  initial_state: 'off'
  trigger:
  - platform: mqtt
    topic: "/HAwirelesslink/return/light/ledvance_a19_rgbw_00060a5e_1/rgb_color"
  action:
  - service: light.turn_on
    data_template: 
      entity_id: light.ledvance_a19_rgbw_00060a5e_1
      rgb_color: >
        {% set rgb = trigger.payload.split(',') %}
        - {{rgb[0]}}
        - {{rgb[1]}}
        - {{rgb[2]}}

I’m pretty sure that won’t work either. The problem is the value for rgb_color is a template (one, big, multi-line template), and a template will output one string. The string will contain end of line characters and dashes, but that won’t make it a list, because that is YAML, and the YAML parsing happened a long time before when the file was originally parsed.

See PR #25065. Unfortunately it was ignored.

However, what might work is this:

- id: ledvance_a19_rgbw_00060a5e_1_light_color
  alias: ledvance_a19_rgbw_00060a5e_1_light_color
  initial_state: 'off'
  trigger:
  - platform: mqtt
    topic: "/HAwirelesslink/return/light/ledvance_a19_rgbw_00060a5e_1/rgb_color"
  action:
  - service: light.turn_on
    data_template: 
      entity_id: light.ledvance_a19_rgbw_00060a5e_1
      rgb_color:
        - "{{trigger.payload.split(',')[0]}}"
        - "{{trigger.payload.split(',')[1]}}"
        - "{{trigger.payload.split(',')[2]}}"

Nutz! I think I now understand the principle correctly. You create the list’s “framework” in YAML:

rgb_color:
  -
  -
  -

and restrict Jinja2’s templates to generating the content of each item in the list:

rgb_color:
  - "{{ }}"
  - "{{ }}"
  - "{{ }}"

So the scope of the templating is restricted to creating strings for each list item. Any expansion of this scope and then the whole result becomes a string (with only the appearance of being a list).


EDIT
So what’s the accepted etiquette for ‘bumping’ a long-overlooked PR? Because that one would definitely help alleviate this very common struggle with setting rgb_color.

I don’t really know. I have seen people ask on discord to have their PR’s looked at, but I try to avoid doing that myself. Besides, this one has sat around long enough that it has to be re-done to be merge-able again. Same goes for the doc PR that goes with it. And I didn’t want to do the work, only for it to sit and be ignored again (which, I think, I’ve already done once, so I should have said I don’t want to do the work again.)

Thanks for all the help, I got it working but needed to change the RGB string to hex on the MQTT side, and ended up with the code below. Input is now in the form “RRGGBB”

- id: ledvance_a19_rgbw_00060a5e_1_light_color
  alias: ledvance_a19_rgbw_00060a5e_1_light_color
  initial_state: 'off'
  trigger:
  - platform: mqtt
    topic: "/HAwirelesslink/return/light/ledvance_a19_rgbw_00060a5e_1/rgb_color"
  action:
  - data_template:
      entity_id:  light.ledvance_a19_rgbw_00060a5e_1
      rgb_color:
        - "{{ trigger.payload[:2] | int(0,16) }}"
        - "{{ trigger.payload[2:2] | int(0,16) }}"
        - "{{ trigger.payload[-2:] | int(0,16) }}"
    service:
      light.turn_on
1 Like