Automation taking past States into Account?

Hi guys,

my Dreame Vacuum cleaner runs fine so far with the custom integration. My lights are controlled by Hue and HomeAssistant.

The original app from Dreame can schedule cleaning jobs based on time (which is fair enough). But especially in winter it might be dark when the job starts. So I want to switch the lights on for a room where the vacuum is cleaning.

My idea:
Automation triggers when the state of “vacuum_current_room” changes for at least 1 minute (my rooms are not so big, the vacuum needs max a minute to cross one to get to the target room).
When triggered, automation switches light on for this room with 100%.
But only if the state of the vacuum is “cleaning”.

My first problem is now how to match between the rooms the Dreame offers and the rooms configured in Hue. Do I need to have a single automation for each room or can I do some sort of matching by script like “if room=5 then hue_room=kitchen” and then switch the matching Hue light on?

My second problem is to switch the lights off. In an ideal world I could use the previous state of the the above Dreame sensor and switch it off within the same automation like an additional action: “Switch light off on the previous state”.

Unfortunately, I have to take into account that there might be some people in the room so it is not a good idea to switch it off while we are at dinner… so only switch it off if it was switched on by this automation. I guess I can use some sort of helper to mitigate this. Create additional action for switching on and switch the helper on. Only switch off if the helper is on.

Any further ideas?

Thanks a lot!

/KNEBB

So far this should work.