How do I: calculate absolute humidity (indoor/outdoor), compare & trigger a switch?

Goal: vent the attic when outdoor air holds less moisture (g/m3).
Have: 4 aqara temp- & humidity sensors (2 in attic, 2 outdoor), 2 fans powered through a (single) zigbee smart plug.

Want:

  • from RH & temp measurements (plural, 2 sensors in both locations) calculate absolute humidity (g/m3) outdoors, and in the attic
  • if AH outdoor is < AH in attic, switch plug on to start fans.
  • Would be great if (hourly?) AH is logged “forever” for further analysis in the future.
  • Stretch goal: graph the changes in (outdoor & attic) AH when venting, so I can evaluate the frequency and effects of venting.
  • Strech goal 2: a card to visualize both outdoor sensors’ RH, temp & calculated AH, as well as both attic sensors’ RH, temp & AH

I can sort out the math myself so feel free to use a placeholder expression. I mostly need help with how to write and structure the automation to make it robust and maintenable. How do I best average the sensor readings (avoiding bugs if one goes offline)? How do I log hourly values?

Grateful for any help and advice!