Template lights: how to handle brightness being or not being set?

Hi,

I’m sorry if it’s been asked before, it sounds like such a basic use-case, but I can’t find anything.

I’m trying to define a basic template light group that would turn on or off some lights based on some condition. Basically trying to filter out the events without changing their parameters.
I’m running into trouble with actions like turn_on, which may or may not receive the parameter brightness, and I am not sure how to deal with that.
So far I have used something like

action: light.turn_on
metadata: {}
target:
  entity_id: light.ceiling_light_light
data:
  brightness: {{ int(brightness, default=255) if brightness is not none }}

but if brightness is not specified, it will set the brightness to 255, whereas I would like to send the light.turn_on event without brightness (so that e.g. adaptive lights can intercept the call).

I guess I could use an if block with two different events depending on whether the value is set or not, and do the same with transition and end up with 4 ifs, but before I go there and do that over every light, I have to ask: is there a better/cleaner way?

Thanks!

Try this example. It uses a template to generate the appropriate dict value for data.

- action: light.turn_on
  target:
    entity_id: light.ceiling_light_light
  data: >
    {%- if brightness is defined and brightness is not none %}
      { 'brightness': {{brightness | int(255)}} }
    {%- else %}
      {}
    {%- endif %}
1 Like

Very neat, thanks!

1 Like

To set brightness you use the actions under set_level. The action(s) set there are also used when the light is turned on by using the brightness slider.

1 Like

Sorry for the late reply, I just saw your comment now. Thanks for the clarification! My understanding is that I would still need to handle brightness in light.turn_on for calls (e.g. in automations) that would set both brightness and transition, is that correct?

As far as I know you don’t need to do that. Also not when using transition in combination with brightness

1 Like