New user here, so apologies in advance. My goal is to change the color of a Sengled RGBW light based on PurpleAir’s pm2.5 measurement. I’m oh so close. Basically, what I want to do is map the pm2.5 values to an appropriate HS_color value…
I believe my problem is that templates aren’t evaluated when I think they are, and I’m guilty of a classic “binding problem.”
What I’ve come up with so far (clever in so far as it goes as it maps pm2.5 values from green through yellow to red for 0…150, and purple for anything higher)
entity_id: light.sengled_e11_n1ea_50e10800_level_light_color_on_off
hs_color:
{% set pm25=states("sensor.purpleair_pm25") | int -%}
{%- if pm25 <= 50 -%}
{%- set h=120-4*pm25 -%}
{%- elif pm25 <= 150 -%}
{%- set h=[60-0.6*(pm25-50)]|max -%}
{%- else -%}
{%- set h=500 -%}
{%- endif -%}
{{ h | int }}
100
When I run this in the template tab of developer tools I get the right thing:
Of course this works perfectly when I type the above four lines into the Services section of Developer Tools, but typing in the version with the templates doesn’t get past the syntax checker:
Also, your h variable won’t last beyond the {% if %} / {% endif%}. Once that scope is done, h is no longer valid. It works in the template editor because the entire thing is one giant scope. It can really throw you off.
Inside of sequences, you can get around this with the variables action.
For a template light (which is seems you have), you’ll have to use a namespace. This will allow the variable to live beyond the scope of the template node.
hs_color:
{% set pm25=states("sensor.purpleair_pm25") | int -%}
{% set ns = namespace() %}
{%- if pm25 <= 50 -%}
{%- set ns.h=120-4*pm25 -%}
{%- elif pm25 <= 150 -%}
{%- set ns.h=[60-0.6*(pm25-50)]|max -%}
{%- else -%}
{%- set ns.h=500 -%}
{%- endif -%}
[{{ ns.h | int }}, 100]
And finally, this will only work in 0.118 (or 0.117 if you turn on beta templates in the config). Before these changes, your template would return a string, not a list. No matter how hard you try.
(Note - I changed your hs_color output to a list by adding the braces. The way you had it was just 2 numbers with a \n between them)
Right now I’m stuck on something a lot sillier and more basic… even though in a service call I can change a light’s color with hs_color, in an automation I can’t…
Part of me wants to figure it out, and part of me is just going to do the math to convert from hs to xy! But regardless, a big thanks for the harder part of the job (templates and namespaces).