Light.turn_on multiple brightness question

Is it possible to set different a brightness for multiple lights with a single light.turn_on command, or does each individual brightness require its own command?

In other words, can this be simplified?

alias: Script Outside Lights
sequence:
- service_template: '{% if states.sun.sun.attributes.elevation|int > 0|int %}light.turn_off{% else %}light.turn_on{% endif %}'
  entity_id:
  - light.front_porch
  data_template:
    brightness_pct: '{% if now().hour >= 22 or now().hour < 12 %}25{% else %}0{% endif %}'
- service_template: '{% if states.sun.sun.attributes.elevation|int > 0|int %}light.turn_off{% else %}light.turn_on{% endif %}'
  entity_id:
  - light.back_porch
  data_template:
    brightness_pct: '{% if now().hour >= 22 or now().hour < 12 %}50{% else %}100{% endif %}'
- service_template: '{% if now().hour >= 22 or now().hour < 12 %}light.turn_off{% else %}light.turn_on{% endif %}'
  entity_id:
  - light.christmas_lights

Into something like this which uses a single call?

alias: Script Outside Lights
sequence:
- service_template: '{% if states.sun.sun.attributes.elevation|int > 0|int %}light.turn_off{% else %}light.turn_on{% endif %}'
  entity_id:
    light.front_porch:
      data_template:
        brightness_pct: '{% if now().hour >= 22 or now().hour < 12 %}25{% else %}0{% endif %}'
    light.back_porch:
      data_template:
        brightness_pct: '{% if now().hour >= 22 or now().hour < 12 %}50{% else %}100{% endif %}'
    light.christmas_lights:

Thats just the thing, I’m trying to get away from scenes. It over complicates the code.

Automation -> Script -> Scene

I would rather just have automations that call scripts, or possibly even just put everything in the automation actions.

Skip the scripts and put the scenes in the action of the automation. If you want multiple lights at differing brightness/color/temp the most efficient way is to use a scene.

Well let me ask this then…
Is it possible to define the scene settings inside the automation action rather than creating a separate scene entity and then just calling it?

In other words, at “entity_id” instead of calling scene.soandso, can you just define that scene right there in the action of the automation?

My end goal here is to have a single automation for my entities which can have different actions according to various things. I don’t want a separate automation for turning on a light, turning off a light, setting a light to 75, setting a light to 80 and so on.
I’ve mostly got that working as shown above with a single automation which calls a script where the determination is made on what needs to happen.

No, but if you’re already happy with the scripts then just move them to the action block of the automation instead of calling them from the automation.

Okay thanks. I’ll play around with these ideas and see what I end up liking best.

1 Like

Did you get your issue figured out? My vote is to put the script into the automation action:. But you will have to call a separate service: for each entity_id with a different brightness. Also the lights will not be turned off by your action because service: light.turn_off will not execute because brightness_pct: is not a service data attribute of light.turn_off. To get around this use light.turn_on and set brightness_pct: equal to 0. See //Light for more information.

This is how I do a similar thing.

- alias: Motion for 2 Lights
  trigger:
  - platform: state
    entity_id: binary_sensor.m_bath_motion_24a
    from: 'off'
    to: 'on'
  - platform: state
    entity_id: binary_sensor.m_bath_motion_24a
    from: 'on' 
    to: 'off'
  condition: []
  action:
  - service: light.turn_on  
    data_template:
      entity_id:  light.m_bath_sconce_06
      brightness_pct: >
        {% if is_state('light.m_bath_sconce_06', 'on') or is_state('light.m_bath_tub_07', 'on') %}
        0
        {% elif is_state('sun.sun', 'below_horizon') %}
        25
        {% else %}
        75
        {% endif %}
  - service: light.turn_on
    data_template:
      entity_id: light.m_bath_tub_07
      brightness_pct: >
        {% if is_state('light.m_bath_tub_07', 'on') or is_state('light.m_bath_sconce_06', 'on') %}
        0
        {% elif is_state('sun.sun', 'below_horizon') %}
        25
        {% else %}
        100
        {% endif %}

I ended up with a combination of methods. I nixed the script and just use a single automation with conditions.
That automation will call a different scene depending on the conditions.

So I get one automation which is great, but multiple scenes. This actually turns out to be better in the long run since i can also turn off things in scenes and the code is cleaner.

1 Like

First time post, so, please bear with me if I’m clueless…
Sorry to dig up such an old question. I tried to create a similar automation to what was being done, above, with the bathroom lights being triggered by a motion sensor, which does work, and with the brightness as a function of the sun’s elevation, which is not working. When I wrote the code to get a brightness value in the Developer Tools’ template, it worked just fine. When my script is called, I’m getting the ‘else’ value.

In scripts.yaml:

set_bathroom_brightness:
  alias: Set Bathroom Brightness
  mode: single
  sequence:
  - brightness: >
      {% set sun_elevation = state_attr('sun.sun', 'elevation') | int %}
      {% if sun_elevation > 5 %} 192
      {% elif sun_elevation > -5 %} 102
      {% else %} 12
      {% endif %}
    device_id: 37c964d708e84c7da48a233ab15a91ec
    domain: light
    entity_id: light.bathroom
    type: turn_on

In automations.yaml:

- id: '1593891497986'
  alias: turn on the master bathroom light when motion is detected
  description: Master Bath motion triggered lights
  trigger:
  - device_id: bd6e444010fb4955bb7cf4431eaae483
    domain: binary_sensor
    entity_id: binary_sensor.tradfri_motion_sensor
    for:
      hours: 0
      minutes: 0
      seconds: 0
    platform: device
    type: motion
  condition:
  - condition: device
    device_id: 37c964d708e84c7da48a233ab15a91ec
    domain: light
    entity_id: light.bathroom
    type: is_off
  action:
  - data: {}
    service: script.set_bathroom_brightness

It’s nice that the light turns on, but it would be great if I could set the brightness by the sun’s elevation.