Help with RGB light automation

Why this

   - service: light.turn_on
     data:
       entity_id: light.led_strip_rgb
       rgb_color: [255, 0, 0]
       brightness: 255
       transition: 50        

… work, and the following didn’t?

  - service: light.turn_on
    data_template:
      entity_id: light.led_strip_rgb
      brightness: 255
      transition: 50            
      rgb_color: >-
        [{{ states('input_number.led_strip_r') | int }},{{ states('input_number.led_strip_g') | int }},{{ states('input_number.led_strip_b') | int }}]

The result of the rgb_color template is correct if passed with the template editor.

Based on this post, try this:

      rgb_color: >-
        [{{ "states('input_number.led_strip_r') | int }}", "{{ states('input_number.led_strip_g') | int }}", "{{ states('input_number.led_strip_b') | int }}"]

You may not even need to use the int filter.

Looking at the log i found the following error:

Invalid data for call_service at pos 2: None for dictionary value @ data['rgb_color']

But as specified here Lights rgb_color is a valid attribut of light.turn_on service

I believe the complaint is that the value for rgb_color is None.

None for dictionary value @ data['rgb_color']

For testing the value of the template I just created this quivalent:

  - service: mqtt.publish
    data_template:
      topic: "DIODER/CMD/RGB"
      payload: >-
        [{{ states('input_number.led_strip_r') | int }},{{ states('input_number.led_strip_g') | int }},{{ states('input_number.led_strip_b') | int }}]

… well, the content is perfect formed.

1 Like

I believe the issue was what I had originally suspected. This is understood to be a python list:

       rgb_color: [255, 0, 0]

The output of a template is always a string. Therefore the product of this template may look like a list but it is actually a string:

      rgb_color: >-
        [{{ states('input_number.led_strip_r') | int }},{{ states('input_number.led_strip_g') | int }},{{ states('input_number.led_strip_b') | int }}]

So what’s happening is that a string is being supplied to the rgb_color option when it is expecting a list.

In your MQTT publishing example, the content may indeed be perfectly formed but it’s a string which is precisely what the payload option accepts.

@123, okay!. this could be the problem. Now… how to “convert” the output to a list?

Working on it …

I’m having trouble emulating the actual light, to test the template, so maybe I’ll just let you perform the test. Try this:

      rgb_color:
        - "{{ states('input_number.led_strip_r') | int }}"
        - "{{ states('input_number.led_strip_g') | int }}"
        - "{{ states('input_number.led_strip_b') | int }}"
1 Like

its as @123 says,

this works fine:

        rgb_color: ['{{ (range(0, 255)|random) }}',
                    '{{ (range(0, 255)|random) }}',
                    '{{ (range(0, 255)|random) }}']
        brightness: '{{ (range(50, 250)|random) }}'
        transition: '{{ (range(1, 3)|random) }}'

not sure if the rgb template I use is yaml identical to the list @123 provides, there are many variations boiling down to the same.

Of course mine is random, so set it to your specific needs, per color_template.
Hope this helps

Solutions I’ve seen include what Marius provided:

rgb_color: ['{{ bla }}', '{{ bla }}', '{{ bla }}']

or this one:

rgb_color:
  - '{{ bla }}'
  - '{{ bla }'
  - '{{ bla }}'

The moment you start doing this with rgb_color it fails:

rgb_color: >
  ['{{ bla }}', .... etc

The last example is no longer handled as a list but as a string.

2 Likes

I found a solution that each of you had actually already found …

  - service: light.turn_on
    data_template:
      entity_id: light.led_strip_rgb
      rgb_color: 
        - "{{ states('input_number.led_strip_r') | int }}"
        - "{{ states('input_number.led_strip_g') | int }}"
        - "{{ states('input_number.led_strip_b') | int }}"

Many thanks! guys!

When I format it this way and trigger my automation, it causes my esp32 running esphome with the sk6812 leds to crash. I have to pull power and restart. This behavior is consistent. I’d just rather save problems like this for my 40 hour a week job. :rofl:

EDIT: Wait…it’s crashing all together - just ignore me

Thanks. Even after 5 years, this helps me.

Only that nowadays, the “date_template” is not needed anymore. It’s simply “data”.

Sorry to reawaken this super helpful thread.
I’m trying to create a similar flow - looking at the above I think I had the same issue - I’m ending up with “None for dictionary value”

I’ve posted my node red flow below - to try and tackle the issue I’ve split out my R G B values and trying to pull them into the data field. But still getting the error

I’m wondering if it’s as simple as the way I’m trying to grab the payload values is incorrect?
{"rgb_color": ["{{msg.payloadr}}", "{{msg.payloadg}}", "{{msg.payloadb}}"]}

I’ve tried with the quotes and curly braces none seem to work :confused:

Anyone able to help?

[{"id":"405c45f5.97d48c","type":"function","z":"3aa4bb1d045b93c0","name":"change rgb","func":"var i = flow.get(\"i\");\nvar red   = Math.round(Math.sin(0.3*i + 0) * 127 + 128);\nvar green = Math.round(Math.sin(0.3*i + 2) * 127 + 128);\nvar blue  = Math.round(Math.sin(0.3*i + 4) * 127 + 128);\n\ni++;\nif (i>=32) {\n    i = 0;\n}\n\nflow.set(\"i\", i);\n\nmsg.payload = {};\nmsg.payload.data = {};\nmsg.payload.data.rgb_color = \"[\" + red + \", \" + green + \", \" + blue + \"]\";\nmsg.payload.data.r = red;\nmsg.payload.data.g = green;\nmsg.payload.data.b = blue;\n\nreturn msg;","outputs":1,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":[],"x":690,"y":320,"wires":[["93f6beb4.23847","6401153b187820c9"]]},{"id":"c0a79053.feb99","type":"inject","z":"3aa4bb1d045b93c0","name":"","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":360,"y":320,"wires":[["af891aa6.6452f8"]]},{"id":"af891aa6.6452f8","type":"function","z":"3aa4bb1d045b93c0","name":"set var","func":"flow.set(\"i\", 0);\n\nreturn msg;","outputs":1,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":[],"x":530,"y":320,"wires":[["405c45f5.97d48c"]]},{"id":"93f6beb4.23847","type":"delay","z":"3aa4bb1d045b93c0","name":"","pauseType":"delay","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":700,"y":400,"wires":[["405c45f5.97d48c"]]},{"id":"6a933fa0.e02d5","type":"api-call-service","z":"3aa4bb1d045b93c0","name":"Cycle","server":"3aa13fed.bf745","version":7,"debugenabled":false,"action":"light.turn_on","floorId":[],"areaId":[],"deviceId":[],"entityId":["light.0xa4c138710fc49807"],"labelId":[],"data":"{\"rgb_color\": [msg.payloadr, msg.payloadg, msg.payloadb]}","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","blockInputOverrides":false,"domain":"light","service":"turn_on","x":1110,"y":340,"wires":[[]]},{"id":"6401153b187820c9","type":"change","z":"3aa4bb1d045b93c0","name":"","rules":[{"t":"set","p":"payloadr","pt":"msg","to":"payload.data.r","tot":"msg"},{"t":"set","p":"payloadg","pt":"msg","to":"payload.data.g","tot":"msg"},{"t":"set","p":"payloadb","pt":"msg","to":"payload.data.b","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":900,"y":320,"wires":[["6a933fa0.e02d5","8a58af81d8c0283e"]]},{"id":"8a58af81d8c0283e","type":"debug","z":"3aa4bb1d045b93c0","name":"debug 21","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payloadr","targetType":"msg","statusVal":"","statusType":"auto","x":1080,"y":220,"wires":[]},{"id":"201e0e17a4b81d6f","type":"inject","z":"3aa4bb1d045b93c0","name":"","props":[{"p":"rese","v":"","vt":"date"},{"p":"reset","v":"reset","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":470,"y":400,"wires":[["93f6beb4.23847"]]},{"id":"3aa13fed.bf745","type":"server","name":"Home Assistant","version":5,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":30,"areaSelector":"friendlyName","deviceSelector":"friendlyName","entitySelector":"friendlyName","statusSeparator":"at: ","statusYear":"hidden","statusMonth":"short","statusDay":"numeric","statusHourCycle":"h23","statusTimeFormat":"h:m","enableGlobalContextStore":true}]