If it was me, this is how I would do it -
Automation is triggered by a motion detector, give it an id (eg motion).
Additionally automation is triggered by a:
trigger:
- platform: event
event_type: timer.finished
id: timer_finished
event_data:
entity_id: timer.room_fade_lights <----------(create this)
Then in the automation, I use a choose
the first choose has the condition trigger, and then choose motion.
turn the light on at the brightness you want.
cancel the timer (in case it is running - DO NOT FINISH THE TIMER)
start the timer
Add the next choose option - choose trigger again for the condition and then “timer_finished”
Now add a condition that checks that the current brightness level is above the level you want it to fade down to (or use a master input_number slider to set a global value for all the lights you want to have an automation for).
Call the service to decrease the brightness
Start the timer
Make sure the automation is set to restart, not single.
So if motion is detected, the light turns on, the timer is cancelled, and started. The automation ends.
The timer reaches 0, the automation is triggered, the second choose option is executed, the light level is above the set fade level, so it is decreased, the timer is started again and the automation ends. Repeated until the brightness is below the set level.
If you want to turn the light off after this, I would start the timer again, but this time set a duration for however long you want to wait until the light turns off, and then in the default actions block add a call to turn the light off. (It’s probably best to add a duration to all the start timer calls in this automation if they are going to be different lengths).
EDIT:
You should also have a trigger for the light turning off, which you give an id to, and in the choose options you should react to the light turning off, by simply cancelling the timer. This is in case you have turned the light off in the UI or by voice or some other way while the timer is running, because it will turn the light back on again when it is has reached 0.