Looking to compute hourly stats

Folks,

I’m looking to generate hourly/daily/monthly runtime statistics off my binary sensors (sump pump, furnace fan, clothes dryer, outdoor heater etc. I want to use the values to determine if the device has failed or is faulted (i.e. stuck on) based upon previous and average runtimes.

I’m thinking I will need to write an external script that will parse the event data I’m storing in the MySQL.

Any thoughts or similar code I can use for a start?

Thanks in advance,