"Next Level" logic - Dynamically Grouped Objects to turn on/off depending on situation?

Senario:

  • I have automated almost everything in my home now and spent many hours tweaking it so everything is now working as expected.
  • This I believe is primarily because I have kept everything simple and easy to debug - numerous automations instead of monolithic complicated ones etc.
  • My wife and I have two kids but are now empty nesters (they are older) - and a dog.
  • Rather than more expensive motion detectors that can detect accurately when someone has left a room, my paradigm is that whenever motion is detected to (re)start a timer - when the timer is started it turns on any lights nearby that were off, and when it is finished (or cancelled) that triggers turning those lights off.

Use case:

  • If there are only two of us in the home, and for example the motion sensor in the den detects motion while at the same time or nearly the same time, another motion sensor say in the master bedroom detects motion - to then use that information to turn off the lights in all other other rooms that might be on at that time. (Remember take into account the paradigm I am using in the last bullet aboive.)

So here is the ask:

How do I dynamically group all of the devices that need to be turned off when the two motion sensors detect motion - so they can just be turned off all at once? I think of this dynamic grouping (maybe I could use the hard coded “room” grouping) - as the next ‘layer’ of logic that sits on top of all the other logic… If not done in this manner - with grouping - you can see how it would be a nightmare of numerous automations to do this any other way (even with just rooms - A, B, C, D, E, F - if motion is detected in A and C then turn off B, D, E, F - even in that kind of scenario the possible number of combinations is a real hassle. Possible a binary input ‘presence’ helper sensor for each room that whenever motion is detected by both motion sensors within 30 seconds of each other, an automation would loop through all the other binary room sensors to turn off lights grouped by room, or ? How do I write such a loop?

2 “Nice to have”'s:

  • Rather than a manual control on a dashboard to say how many people are in the home, automated detection for an input number to track the number of people (and dog or not) in the home, so the rules can be changed (in case there are 5 in the home or whatever in which case the above use case would be ignored. My problem with implementing this is, I can only discern how many people are in the home (other than myself and my wife) when other people that might not even have any wifi connection or phone on them - would stop by to visit…
  • With these inexpensive motion sensors, how to ignore the dog (other than placing them so they will not be able to ‘view’ any areas where the dog would be?

Thoughts folks? Thank you in advance for all of your help!