How can I sum energy usage for a specified time period and use it in an automation?

I’m looking to take an existing energy sensor I have and sum the historical data it’s logged in Home Assistant for a specified time period, and then use the resulting value as part of an automation I’m tinkering with. For example, given an energy sensor which has collected data from Sunday to Saturday and logged consumption over that time in kWh, how can I get the total kWh that was used from Monday to Tuesday?

I can see a few ways this might be done via graphs and other UI tools, but I’m struggling to figure out how to accomplish this in an automation. I’ve explored template sensors, helper sensors, the history_stats and statistics integrations, and just about everything else I’ve been able to find while prodding around online for the last couple hours, but nothing seems to fit my specific use case.

Anyone have any ideas on how I can accomplish this? I’m not opposed to creating new helper or template sensors, or using Node-Red, or whatever else may make sense to do this in a “best practice” way. I’m just at a bit of a loss as I’d have thought something as powerful as HA should be able to do this fairly easily.

1 Like