Trigger based on charging / location status of multiple iphones

Hi there!

I’m looking for help with an automation.

Some background. We’re a family of 3, all with iPhones that we tend to put on charge when we go to bed, and take off charge when we get up. I’d like to trigger a ‘go to bed’ routine when the last phone that is at home (eg if I’m away on business then my phone on/off charge status is not relevant / ignored) is put on charge, and a ‘get up’ routine when the last phone that is at home is taken off charge.

I’ve already set up all our phones to trigger ‘update sensors’ via the HA app on the iphone so HA knows when phones are taken off / put on charge, now I need to set up an automation trigger that would cater for the logic of ‘last phone on charge’ / ‘last phone off charge’.

Does anyone have an elegant solution to this? Ideally it would cater for other phones (eg visitors that come to stay or house sit), so I’m thinking that setting up a helper or two is likely to be part of the solution.

Thanks in advance for any helpful suggestions! :pray: