Hi All,
I am new to home assistant. I am trying to setup a light template. In this light template I wanted to use a script to set the levels of the light group, I am following steps from the light template Documentation however having issues in setting this script.
Can someone tell me what am I doing wrong in the “data_template” section?
I wanted the brightness to be set only if the brightness value is send, same for others , Primary idea is to call this script from a light template’s “set_level”
script_living_room_lights_level:
alias: Script Living Room Lights Level
sequence:
- service: switch.turn_on
target:
entity_id: switch.living_room_recess_switch_1_mss510x_main_channel
- service: switch.turn_on
target:
entity_id: switch.living_room_recess_switch_2_mss510x_main_channel
- service: light.turn_on
data_template:
{% if brightness is defined and brightness|length %}
brightness: {{brightness}}
{% endif %}
{% if h is defined and h|length %}
hs_color: {{ h }}/{{ s }}
{% endif %}
{% if white_value is defined and white_value|length %}
white: {{ white_value }}
{% endif %}
{% if color_temp is defined and color_temp|length %}
color_temp: {{ color_temp }}
{% endif %}
target:
entity_id: light.livingroom_bulbs
mode: single
icon: mdi:lightbulb-on
You have attempted to use a template to create options. That’s not a valid operation.
You can only template values for options. For example:
data:
brightness: "{{ template can go here to compute a value for the brightness option }}"
You can’t use templates to decide if options and other YAML statements should or should not be used (like what you did in your posted example). For example, this is not valid:
# You cannot use templates to generate YAML statements
data:
{% if some test is true then use this option %}
brightness: 125
{% else use these options %}
brightness: 75
transition: 5
{% endif %}
You will need to redesign it and use choose (maybe). I say maybe because I don’t know how you are using this script (i.e. what are the possible variations of what is passed to the script).
Thanks a lot for the explanation, these values will be sent from the light switch card, as I mentioned my idea is to create a set level script for the light template
set_level:
service: script.theater_lights_level
Here is the link to Light Template Documentation Light Template
Yes that is correct I believe, however I am new to HA Scripting.
Can you please tell me does the light card sends all the 5 parameters in one dict or sends the individual commands the options are selected in this bulb card ? If you don’t have the answer for this, can you please suggest how can I find what parameters are passed?
OK, so what’s your plan to handle every valid combination of five variables?
It can’t be done the way you did it because, as explained, you can’t use a template to include/exclude YAML statements. You might want to rethink the whole thing (or limit the number of possible combinations of variables passed to the script).
What makes it exceptionally challenging to help someone is when they don’t reveal all the details at once but choose to introduce them one at a time. It’s like solving a puzzle where the individual pieces are supplied randomly and there’s no picture of the goal.
For example, you never explained how your script is used and now you introduce a new piece to this puzzle in the form of a Light card. I assume this is a custom card, because the standard Light card doesn’t look like that, and this card is calling the script you have created?
I believe this was precise enough to give you a clear picture what I am doing, however please understand there is steep learning curve in understanding the whole home assistant echo system and I am still navigating it. so my request to all the experts here is please be patient with newbies
The information you provided is far from “precise enough”. For example, you overlooked to explain which option within the Template Light is calling the script. In fact, you didn’t post the Template Light’s configuration.
Are their any other suggestions you have for how unpaid volunteers should spend their free time helping others?
If there are no other suggestions, perhaps we can get back to solving the problem and you can supply your Template Light’s configuration.
The only variable it passes to the script is brightness and nothing else. You can’t refer to white_value, color_temp, etc within the script unless you pass those variables to it.
it all depends on what you want to do if the variable wasn’t passed to the script
You can test for the variable’s existence with is defined .
BTW, it’s generally discouraged to re-open a topic that has been dead for over a year. Your series of questions are only vaguely related to this topic and it would have been better to start a new topic, clearly explaining what you were trying to achieve.