Turn on and of a light in 1 automation but with brightness setup

Hi all
Thanks in advance !

What I need is to have an Automation that , based on a sensor state (On / off ) turns on and off the light ( and this is simple ) but in the meantime i need to set the brightness to a certain percentage …

the only automation to turn on and off works perfectly :

 Accendi Balcone (on+off) (Duplicate)
description: ''
trigger:
  - platform: state
    entity_id: binary_sensor.balcone_sensore_motion
condition:
  - condition: state
    entity_id: binary_sensor.notte
    state: 'off'
action:
  - service: |
      {% if trigger.to_state.state == "on" %}
        light.turn_on
      {% else %}
        light.turn_off
      {% endif %} 
    target:
      entity_id: light.balcone
mode: single

but if i set also the brightness the turn off part does not works :

alias: Accendi Balcone (on+off) (Duplicate)
description: ''
trigger:
  - platform: state
    entity_id: binary_sensor.balcone_sensore_motion
condition:
  - condition: state
    entity_id: binary_sensor.notte
    state: 'off'
action:
  - data:
      brightness_pct: 50
    service: |
      {% if trigger.to_state.state == "on" %}
        light.turn_on
      {% else %}
        light.turn_off
      {% endif %} 
    target:
      entity_id: light.balcone
mode: single

Of course the problem is that I cannot set brightness while i’m turning off but is there a way to do that instead of using 2 automation ( one to turn on and one to off ) ?

many thanks .

I think you should be able to do this with a choose/condition under action rather than via a service call.

The following is an example of an action from my config but unfortunately I don’t have one that changes the brightness:

  action:
  - choose:
    - conditions:
      - condition: template
        value_template: '{{ trigger.event.data.command in [''left_single''] }}'
      sequence:
      - device_id: 5320c28c7c1e45c580aa9a5ad9bc4432
        domain: light
        entity_id: light.kitchen_lights
        type: toggle
    - conditions:
      - condition: template
        value_template: '{{ trigger.event.data.command in [''right_single''] }}'
      sequence:
      - service: light.toggle
        data: {}
        entity_id: light.lounge_lamps

thanks Jonah1970,

I will try in the next days

thanks.

Hi,
I finally solved my problem using 2 scripts: one to turn on and one to turn off.
for who is interestested following the code I used.

in my script.yaml file :
To turn lights on with brightness percentage :

alias: Light On Dim
sequence:
  - service: light.turn_on
    target:
      entity_id: '{{ entita }}'
    data_template:
      brightness_pct: '{{ luminosita_pct }}'
mode: single

to turn off lights:

alias: Light off Dim
sequence:
  - service: light.turn_off
    target:
      entity_id: '{{ entita }}'
mode: single

and the automation :

alias: Accendi Balcone (on+off)
description: ''
trigger:
  - platform: state
    entity_id: binary_sensor.balcone_sensore_motion
condition:
  - condition: state
    entity_id: binary_sensor.notte
    state: 'on'
action:
  - service_template: |
      {% if trigger.to_state.state == "on" %}
        script.light_on_dim
      {% else %}
        script.light_off_dim
      {% endif %} 
    data_template:
      entita: light.balcone
      luminosita_pct: >
        {% if is_state('input_boolean.luci_bassa_intensita', 'on')
        %}                     
          20
        {% else %}
          100
        {% endif %}
mode: single

as you can see I also set the brightness depending on a sensor state.
Thanks for the help.

Marco A.