Help with Data template on Scripts

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”

  alias: Script Living Room Lights Level
    - service: switch.turn_on
        entity_id: switch.living_room_recess_switch_1_mss510x_main_channel
    - service: switch.turn_on
        entity_id: switch.living_room_recess_switch_2_mss510x_main_channel
    - service: light.turn_on
        {% 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 %}
        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:

  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
  {% 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).

Related Feature Request: Service data: Allow templates returning a dict in addition to a dict of templates

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

          service: script.theater_lights_level

Here is the link to Light Template Documentation
Light Template

That’s the crux of my question: what are the possible variations of what is passed to the script?

The way you coded it suggests the number of variables passed can range from none to five. Is that correct?

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.

@123 Thanks for all the help :smiley:

Here is the full template

- platform: template
        unique_id: living_room_lights_template
        friendly_name: "Living Room Lights Template"
        temperature_template: "{{states('input_number.temperature_input') | int}}"
        color_template: "({{states('input_number.h_input') | int}}, {{states('input_number.s_input') | int}})"
          service: script.script_living_room_turn_on
          service: script.script_living_room_turn_off
          service: script.script_living_room_lights_level
            brightness: "{{ brightness }}"
          service: input_number.set_value
            value: "{{ color_temp }}"
            entity_id: input_number.temperature_input
          service: input_number.set_value
            value: "{{ white_value }}"
            entity_id: input_number.white_value_input
          - service: input_number.set_value
              value: "{{ h }}"
              entity_id: input_number.h_input
          - service: input_number.set_value
              value: "{{ s }}"
              entity_id: input_number.s_input
          - service: light.turn_on
                - light.light.livingroom_bulbs
              transition: "{{ transition | float }}"
                - "{{ hs[0] }}"
                - "{{ hs[1] }}"
        supports_transition_template: "{{ true }}"

The Template Light calls script.script_living_room_lights_level just once, here:

          service: script.script_living_room_lights_level
            brightness: "{{ brightness }}"

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.

Thanks @123 it worked thanks for your help and patience

1 Like