Setting rule based on illuminance, motion, and physical switches

Would love some help accomplishing the following.

Plain English:

If it's bright in the room (illuminance < 300) and lights are on, keep them on for 30 mins past the last detected motion. If the lights turn off and motion is "redetected" within 30 seconds, turn the lights back on to whatever combination/setting/etc. they were before the 30 min auto-off.

If it's not bright outside and light(s) were physically turned on, leave them as long as there is motion plus 30 minutes longer. If the motion is redetected withing 30 seconds of them being auto turned off, turn the light(s) back on to the same settings/configuration they were operating before they were auto shut off.

Slowly getting better with Rule Machine logic but the above is beyond my ability. Would sure appreciate being pointed in the right direction.

Thanks,

Hey Marty,
Here is a rule that might use similar logic, but doesn't quite do everything you want. Pay attention to the cancel delayed actions. That's important. It will cancel a delay that you set something to turn off and reset the timer. Otherwise, it will be persistent and turn off no matter the status. Also, this rule doesn't take processing in mind, which is something that you should pay attention to. This rule was written with the old habits. :grin: Maybe you can take something from the logic in my rule and form a rule that works for you to post here. Sometimes it's easier to see what you're trying to do when you get the rough start of a rule and put it out thee.

Mine is a bit like that. Basically i have a scenario where lights come on automatically if there is presence detected, but only if we're at home and only if the lux level is below a certain level or it is between dusk and dawn.

I then have a separate virtual switch that can turn the lights on (linked to google home voice control); when the switch is on it then adjusts the timeout for the lights so that they stay on for 20 minutes after the last motion rather than the usual 3 minutes. useful for when you're sitting very still in the dining room :-). if the lights turn off after 20 minutes, it then turns off the virtual switch so that the next time they come on they only stay on for 3 minutes. Screenshot below