Setting color with rgb_color doesn't work with data_template?

I want to use one of my standing lamps as a notification/status device for some “system” related stuff.
lamp is equipped with 3 yeelight rgb bulbs lined vertically [bulbs named: dwn (down), mid (middle), btm (bottom)]. main idea is to utilize xiaomi’s wireless button, that has an ability to register long clicks - I want to assign a 15-second long click to “home reset” - when I’ll press & hold the button, every 5 seconds one bulb is turned on with red colour, so I know how long should I hold and when to release to reset the house.

I made a script, that you can feed with bulb choice & color, and it should light up what you want in a way you want, and every 5 seconds I call this script with red color and different bulb. color in web HEX format [it is converted to rgb decimal] and bulb number [don’t want to use names in case when I’ll use other bulbs too - so numbers are translated to bulb name in the script]. it looks like this:

script:
  custom_light:
    sequence:
    - service: light.turn_on
      data_template:
        entity_id: >
          {% set temp = 'light.yeelight_' %}
          {% set bulbs = {'1':'dwn','2':'mid','3':'btm'} %}
          {% if what_bulb|string in bulbs.keys() %}{{ (temp,bulbs[what_bulb|string])|join }}{% endif %}
        rgb_color: >-
          '[{{ (what_color[0],what_color[1])|join|int(0,16) }},{{ (what_color[2],what_color[3])|join|int(0,16) }},{{ (what_color[4],what_color[5])|join|int(0,16) }}]'
        brightness_pct: '75'
        transition: '1'

I’m calling this script with for example:

  - service: script.custom_light
    data:
      what_bulb: 2
      what_color: "FF0000"

and this is what I get in the log:

2018-10-27 16:54:07 ERROR (MainThread) [homeassistant.core] Invalid service data for light.turn_on: None for dictionary value @ data['rgb_color']. Got "'[250,0,14]'"
2018-10-27 16:54:07 ERROR (MainThread) [homeassistant.helpers.service] Template rendered invalid service: 

templates when tested - worked fine, so I really don’t know what’s going on… as always - I am here to ask the community for hints :slight_smile:

1 Like

A template can only return a single string. What your rgb_color template does is return a string that starts with a square bracket, has some numerals, a comma, … and ends with a square bracket. It looks like a list of numbers, but it’s really a single string. You should use color_name instead, or if you want or need to use rgb, then try this instead:

        rgb_color:
          - '{{ (what_color[0],what_color[1])|join|int(0,16) }}'
          - '{{ (what_color[2],what_color[3])|join|int(0,16) }}'
          - '{{ (what_color[4],what_color[5])|join|int(0,16) }}'

Or even:

        rgb_color:
          - '{{ what_color[0:2]|int(0,16) }}'
          - '{{ what_color[2:4]|int(0,16) }}'
          - '{{ what_color[4:6]|int(0,16) }}'
1 Like

great! working as promised :slight_smile:
I didn’t know that HA sees it as just a string. thank you for explaining that!

No problem. Glad to help.

That’s what Got "'[250,0,14]'" basically said. :wink: Since a lot of people get bit by this, it would really be nice if the light.turn_on service was enhanced to accept a string like this that could be parsed to extract the three numbers.

TBH I was testing with converting the whole load to/from string, to/from integer and in the end I just got lost and believed that if dev-template shows “what I want”, that it should be good for Home Assistant :wink:

I really hope, that even if last version of hassio introduced some configuration options possible only via frontend, they won’t kill yaml files and we still will be able to play with the code. sure it sometimes gets buggy, doesn’t work like I think it should, but all in all - it’s the best part of the whole fun, when something that came from “under your fingers” starts to work, even if it’s not entirely mine, because most of the code was found or corrected on the community pages :slight_smile:

2 Likes