Keeping it simple is key (many separate automations that can be easily changed instead of one complicated thing that turns into a mess of spaghetti logic). The paradigm I use is to (and you can see how it gets more complicated as you refine it as per below with additional fine tuning if you wish to proceed to the end of the list). All the items below are separate automations:
1a. Helper: Set up a timer, 1b. set up a numeric input helper (for timer duration), have it show as a slider on your dashboard (so you can easily change the duration of the timer when it is running).
2. Automation: When the timer is (re)/started, it turns the light on.
3. Automation: When the timer is finished, it turns off the light.
4. Automation: When someone turns on the light, the timer in #1a is (re)started, with the duration from #1b.
5. Automation: When Motion Sensor detects motion (re)start the timer (#1a) with the duration from #1b (that way the lights stay on while people are in the room and the countdown only really starts when there is no more motion). Also, the less expensive motion sensors are not able to tell very well when there is nobody in the room, or there are people in the room but not moving, hence the timer/s.
6. Helper: A drop-down with the text “Enabled” / “Disabled” that I then go-back and put into the above Automation logic (#4 and #5) that the automation is enabled or disabled.
7. Helper: I ran into a problem where people would turn off a light and then when they then walk out of the room the light would go back on because the motion sensor catches the movement! Therefore - an input text datetime helper just for storing the datetime (then see #8-9 that are the final parts to resolve this issue.)
8. Automation: When the switch is only MANUALLY turned off (there are trigger variables that can be used to determine this - but you have to store information about the current datetime into #7 because after this automation is finished, that data is otherwise lost), set the current date and time into #7 -
9. Update Automation: Update #5 to contain logic that will always check #7, to actually skip calling any automation when motion is sensed if the value in #7 is within the last 5 minutes.
10. Automation: Whenever the slider in #1b is changed, only if the related timer (1a) is active, then (re)start that timer with the duration listed in #1b.
11. Automation: When HA is restarted, check the above that if the automation is enabled and the light is on, that the timer is (re)started if it is not active.
12. Automation: When HA is restarted, check the above that if the automation is enabled and the timer is active but the light is not on, that either the timer is cancelled or the light is turned on.
13. Automation: Update the automations that turn on the light, that sense the amount of the light outside from a weather station or the like, and if the room has alot of windows, (and the weather station does not see total darkenss in the middle of the day because it might be covered with snow for example) you can set the light to be dimmer, or if it is very late at night, you can have the lights come on much dimmer, etc.
14. If there is only one person in the home, you can just cancel the timers for the other rooms when motion is sensed in any one room because there is nobody in the other rooms (and that would turn those lights off, etc.)
15. Automation: If the dropdown for enabled/disabled is changed to disabled, then if you are controlling a smart outlet into which a lamp is plugged, then stop the timer and turn the outlet ON so people will not be annoyed that they cannot turn the lamp on - and a different paradigm should be used if it is for a wall switch, that if the dropdown for enabled/disabled is changed to disabled, to just stop the timer and do nothing else - leave the switch as it is (then change the logic in #3 to only turn the light off if the automation is Enabled).
16. Automation: If an automation is then changed to Enabled, if the related light is on then just start the related timer (1a) with the duration (1b).
The above 16 are somewhat detailed but once you have it working it is pretty bulletproof - and you don’t have to do all 16 but you get the idea. Then, you no longer have to reinvent the wheel as you add more lights into your automations, just duplicate trhe automations above and helpers for each new light you add into the mix. Also, when you do this, look at all the new automations (that were duplicated) under the list of entities, and you will see the entity name does not change properly, so right in that screen you can change the names of those entities so they are consistent.
And doing your automations this way (in many simple pieces) makes maintenance etc. MUCH simpler.
Then for the code that is reapeated everywhere, move it into a script to which you can pass the objects (timer, light, enabled/disabled helper, duration helper etc.) and all that logic can be in one place. Therefore also set all automations to be able to run in parallel (check the mode I make mine all parallel and put a limit of 1,000 or the like).
The above has worked beautifully for me - (then I set up alerts for sensors when their batteries are low, as well as one UI panel the just shows all the battery levels for all the sensors at a glance, etc.)