Proper light brightness code

I have the below config, based on the how-to documentation… Turn on a light when motion detected, wait a period of time, then turn it off. This works, except for the brightness setting.

I would like the brightness increased to 100% with a three-second transition, and reduce the brightness to 50% before turning it off, with a five-second transition.

First, do I have the brightness coded correctly? Secondly, I understand “light.turn_off” has a problem with setting brightness level; is there another to do this?

- alias: Office Motion Light On
  trigger:
    platform: state
    entity_id: binary_sensor.office_motion_detector_motion
    to: 'on'
  condition:
    condition: time
    after: '19:00'
    before: '08:00'
  action:
    service: homeassistant.turn_on
    entity_id: light.office_floor_lamp
    data:
      brightness_pct: '100'
      transition: 3
- alias: Office Motion Light Off
  trigger:
    - platform: state
      entity_id: binary_sensor.office_motion_detector_motion
      to: 'off'
      for:
        minutes: 3
  action:
    - service: homeassistant.turn_off
      entity_id: light.office_floor_lamp
      data:
        brightness_pct: '50'
        transition: 5

In the second automation, change it to turn_on with the brightness at 50 percent and a 5 second transition, then call the turn_off service. I think you’ll also need a delay equal to the transition time between the turn_on and turn_off service calls.

If I dismiss the transition for a moment, is the below what I need?

- alias: Office Motion Light On
  trigger:
    platform: state
    entity_id: binary_sensor.office_motion_detector_motion
    to: 'on'
  condition:
    condition: time
    after: '19:00'
    before: '08:00'
  action:
    service: homeassistant.turn_on
    entity_id: light.office_floor_lamp
    data:
      brightness_pct: '100'
      transition: 3
- alias: Office Motion Light Off
  trigger:
    - platform: state
      entity_id: binary_sensor.office_motion_detector_motion
      to: 'off'
      for:
        minutes: 3
  action:
    - service: homeassistant.turn_on
      entity_id: light.office_floor_lamp
      data:
        brightness_pct: '50'
    - service: homeassistant.turn_off
      entity_id: light.office_floor_lamp

Yes, but that will basically immediately turn the light off. That’s why I suspect you’d need a delay.

I’m unfamiliar with the ‘delay’ action… How would I add that?

Ok. I’ll give this a go and report back herein. Thanks for the help.

This is what I ended up with, minus lowering brightness on exit. I’ll get to scripting later on.

- alias: "Office Motion Light On"
  trigger:
    - platform: state
      entity_id: binary_sensor.office_motion_detector_motion
      to: 'on'
  condition:
    - condition: state
      entity_id: sun.sun
      state: below_horizon
  action:
    - service: homeassistant.turn_on
      entity_id: light.office_floor_lamp
      data:
        brightness_pct: '90'
        transition: 3
#    - service: notify.notify
#      data:
#        message: Office Motion Light On
        
- alias: "Office Motion Light Off"
  trigger:
    - platform: state
      entity_id: binary_sensor.office_motion_detector_motion
      to: 'off'
      for:
        minutes: 3
  action:
    - service: homeassistant.turn_off
      entity_id: light.office_floor_lamp
      data:
        transition: 3
#    - service: notify.notify
#      data:
#        message: Office Motion Light Off