Welcome to Hubitat and the community. I will let others speak to your coding question, but I did want to point out that @bangali has made a great application called Rooms Manager. It does basically everything you are asking about. He might be a good person to talk with about your project.
[Rooms Manager: Smarter Rooms: Personalized home automation with Occupancy]