Script: Pass null or nil value to brightness in script

Hey everyone :slight_smile:
I would like to be able to manually disable automatic brightness setting on a light, via an input boolean on the front end.

I have the following setup:

An input_boolean called dining_room_auto_lux.
An input_number called default_brightness

When dining_room_auto_lux is true and the script is called, the light should be turned on, and the brightness set to the value of default_brightness.
When dining_room_auto_lux is false and the script is called, the light should be turned on, but the brightness not set.

I have a script for turning on a light currently, but it doesn’t work when dining_room_auto_lux is off

lights_on_dining_room:
  alias: Turn on lights in Dining Room
  sequence:
    - service: light.turn_on
      data_template:
        entity_id: light.dining_room_light
        brightness: >
          {% if is_state('input_boolean.dining_room_auto_lux', 'on') %}
            {{ states('input_number.default_brightness') | int }}
          {%-else %}
            <What do I put here??>
          {% endif %}

I essentially need to either be able to pass a null value to brightness, or remove the brightness key when the value is false.

Any ideas?

Ok I’ve come up with a solution. Rather than use if is_state(), I turn on the light first (as I’ll be doing this regardless of the state of dining_room_auto_lux) then use a condition in the script to early return if dining_room_auto_lux is false. If the script passes this condition, I call light.turn_on again, this time passing the brightness.

I was concerned sending 2 calls to turn it on would cause problems, but it seems to be fine.

I’d still be interested if anyone has a better solution, or if its possible to move the keys into the template and have then conditionally injected, if needed.

This is my script now:

lights_on_dining_room:
  alias: Turn on lights in Dining Room
  sequence:
    - service: light.turn_on
      entity_id: light.dining_room_cube

    - condition: state
      entity_id: input_boolean.dining_room_auto_lux
      state: 'on'

    - service: light.turn_on
      data_template:
        entity_id: light.dining_room_cube
        brightness: "{{ states('input_number.default_brightness') | int }}"

If you don’t mind first turning it on without a brightness (which typically turns it on to the brightness it was last time it was on), then what you came up with is probably the best solution.

The problems are, first you can’t use a template to conditionally include a key/value pair because a template essentially returns a single string (even if it looks like something else, like a dictionary.) Second, HA YAML scripting does not have an if/else mechanism. The best you can do is conditionally abort the rest of the script or not. What would be really nice would be at least a feature that could conditionally execute the next step or not, but so far there is no such feature.

If you really need to do an if/else, then what I’ve done is to call two scripts - one that runs if a particular condition is true, and another that runs if the same condition is false. (What I mean is, they both start with a condition step, where in one for the given condition it evaluates to true whereas the other would evaluate to false.) It does get a bit unwieldy in that it takes at least three scripts, but it does work.

Thanks for the quick response! :slight_smile:
I think the best approach for me is the double turn on one. It doesn’t seem to have any adverse effects or any light flickering or similar. I can keep it all within one script too, which is tidier.