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”

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

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


set_level:
          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
    lights:
      living_room_lights_s:
        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}})"
        turn_on:
          service: script.script_living_room_turn_on
        turn_off:
          service: script.script_living_room_turn_off
        set_level:
          service: script.script_living_room_lights_level
          data:
            brightness: "{{ brightness }}"
        set_temperature:
          service: input_number.set_value
          data:
            value: "{{ color_temp }}"
            entity_id: input_number.temperature_input
        set_white_value:
          service: input_number.set_value
          data:
            value: "{{ white_value }}"
            entity_id: input_number.white_value_input
        set_color:
          - service: input_number.set_value
            data:
              value: "{{ h }}"
              entity_id: input_number.h_input
          - service: input_number.set_value
            data:
              value: "{{ s }}"
              entity_id: input_number.s_input
          - service: light.turn_on
            data_template:
              entity_id:
                - light.light.livingroom_bulbs
              transition: "{{ transition | float }}"
              hs_color:
                - "{{ hs[0] }}"
                - "{{ hs[1] }}"
        supports_transition_template: "{{ true }}"

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

        set_level:
          service: script.script_living_room_lights_level
          data:
            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

How do i pass more variables and how do i use them in the script?

Best regards,

Stefan

Passing variables to scripts

Thank for the quick reply.

How can i check within a script if the parameter is passed to that script?

There are several ways to do it and it all depends on what you want to do if the variable wasn’t passed to the script.

The simplest is to supply a default value if the variable was not passed to the script.

            brightness: "{{ brightness | default(125) }}"

Thank you for your quick answer.

I would like to run different

light.turn_on depending on which parameters are given.

If only brightness is given, just run the light.turn_on with brightness, if both brightness and transition are given, use those two etc.

That’s why a default value should not work here.

Thanks again for your help.

That’s why I said:

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.