I am new to HA template, I want to set an automation to increase/decrease light brightness and temperature on trigger. However, when I test the below action in development/service page, the code is not working.
I can only update the light by put a static value to the brightness/color_temp attribute, but not a template. Any advise on how to inc/dec light brightness/temperature?
I am using HA version 2022.5. Thanks!
service: light.turn_on
data_template:
transition: 1
# the templates statement below is not working
brightness: {{ state_attr('light.livingroom_tv_lightbulbs','brightness')|int + 10 }}
color_temp: {{ state_attr('light.livingroom_tv_lightbulbs','color_temp')|int + 10 }}
# replace to below code will work
# brightness: 50
# color_temp: 100
target:
entity_id: light.livingroom_tv_lightbulbs
You should also supply default values for your |int filters in case the entities return a non numeric value. In this case I have chosen 50% brightness (128/255) and cool white for the color temp. Feel free to change them to whatever you want.
You also need to guard against the brightness value exceeding 255, e.g.
Thanks for your kind description about the default value and min operation. It is very useful for me to enhance my scripts.
However, I found the templates for lighting control is still not working, as the service call on template attributes will not do anything.
I can show the template values in Development page as attached, But whenever I put it into data part of the light_on service call, it will do nothing. Seems the light-on service call just ignored the template attributes.