How to Template Automations over Areas?

I have done automations before that do some dynamic stuff like reading out area names and such, but I have one idea I am not getting how to properly automate.

I have one area for each room, and I have a device of type A and a device of type B in each room. I need to do some calculations from different attributes of the devices, and set one attribute on one of these devices. Since all devices are properly assigned to areas, I can easily find a pattern to find the devices, but I am wondering how to iterate over the areas for this inside an automation… I don’t seem to find an easy way of doing this.

I would like something like:

  • 1x per hour iterate over all areas, and for each area where both these devices are found, do the calculations and set a state.