Error in data_template turning on lights

I have a script to turn on lights based on sensor and switch values, but i always this error:

Fallita chiamata a servizio light/turn_on . expected int for dictionary value @ data['color_temp']

This is the script:

attiva_circadian_light:
  alias: Attiva Luce Circadiana
  sequence:
    - service: switch.turn_on
      entity_id: switch.circadian_lighting_circadian_lighting
    - service: light.turn_on
      data_template:
        entity_id: light.yeelight_6, light.gateway_light_7811dcb25b7c
        rgb_color: "{{ state_attr('sensor.circadian_values', 'rgb_color') | int }}"
        brightness_pct: "{{ state_attr('switch.circadian_lighting_circadian_lighting', 'brightness') | int }}"

And these are the sensor and the switch values:

Where the hell is the error?
P.S. Also if i change the attributes i get the same error and if i put the absolute values the script works ok.

Why are you using brightness and brightness_pct in the same line without conversion ?

If you see the values of the switch, the brightness_pct value attribute is called brightness.
Or maybe i don’t understand what you mean…

In the last line your are issuing a command to set “brightness_pct” but you are filling it with a “brightness” value. The two only have a small area of overlap

RGB color is a list of 3 integers, however you are converting the whole list to an integer, this can’t work. With the new templatinh engine you’re out of luck,because it’ll convert a list that separated with commas and containing only integers to a string regardless.

Also take a lool at the adaptive lighting custom component (which is based on the circadian lighting custom component, but has way more feature ans is better integrated). I assume that this component will already solve your issue.

1 Like

Yeah, I saw that too, but I’ve never copied RGB values so have no experience of it
:rofl:

Ok, but the error appears also if i use color_temp in place of rgb_color and color_temp has only one integer. I tried also adaptive lighting component but it is more difficult for me to use that as i see so poor documentation about it and the helps i received about it did not solved my troubles…

I am using that script after looking at the examples in the repository of the component

action:
  service: light.turn_on
  data_template:
    entity_id: light.yeelight_6
    kelvin: "{{ state_attr('sensor.circadian_values', 'colortemp') | int }}"
    brightness_pct: "{{ state_attr('switch.circadian_lighting_circadian_lighting', 'brightness') | int }}"

But also this give me same error…

I think the script you posted in the OP and the error you posted there aren’t related.

the error says “expected int for dictionary value @ data[‘color_temp’]” but the script doesn’t contain any date entry that is called “color_temp:”.

I think this happens in the background, it tries to convert the rgb vlaue to color temp and then sends the color temp to the light, but not sure on this one.

Did you see this? I wouldn’t call this poor docs…

Which version of HA are you using?

If that’s the case then that error is really confusing. How would one ever know that or how to check it if so?

1 Like

99% sure you’re right.

It was only an example… i tried both of the scripts but with the same error…

was the error the example or the script?

do you get the exact same error referencing color_temp with both scripts (using either rgb_color: or color_temp)?

Yes same error in both scripts… but, i repeat, if i use absolute values and not templates it works, so i think the error is in the script syntax…

Which version of HA are you using?

Latest, HA 0.117.6

That’s not the latest.

Aside from what burningstone said above about trying to force a list into an integer try adding “legacy_templates: false” into your “homeassistant:” section in your configuration.yaml.

That should make you start using the new template engine and once you remove that “int” it might keep it as a list and allow it to work.

as far as using color_temp: in the script it’s hard to tell why that would fail since we haven’t seen that script syntax or the data it relies on.

No, I don’t think so. That’s been reported multiple times now, that the new template engine converts list with only ints, separated by a comma to a string, no matter what you do.

See this post from Taras and the ones below it.