Collect data between 19:00 until 05:00

I want to collect electricity usage in kWh between 18:00 until 06:00 o’clock. Reason because I want to have historical data how much electricity we use during that time, to determine the home battery size we might need.

So, I can create a utility meter for electricity usage with daily reset. But how can I get only the data between 19:00 and 5:00?

Anyone has any idea?

or could someone provide me the yaml code?

Template sensor, created via the UI. Presumably you are integrating a power sensor to energy? If so, just “gate” that to the times you want.

State template like this:

{{ 0 if 5 <= now().hour < 19 else states('sensor.YOUR_POWER_SENSOR') }}

then feed that new template sensor to the energy integration sensor.

1 Like

If you are already measuring your power consumption and are familiar with SQL, you can also query the power consumption values for certain periods directly from the Home Assistant database and / or create an SQL sensor for this :wink:
The long term statistics data is sampled once per hour, to save storage.

1 Like

hi thanks for the help, I created the template but I had to set Kwh and device class: energy, I hope I did well.
As indicated below, my sensor measures the discharge in kWh so, let’s see if I did it right…


That won’t work then. It’ll report 0 during the day then at 19:00 it’ll jump back up to where it would have been anyway.

You’ll need a bit more of a complicated solution if you only have a cumulative energy sensor.

  • 00:00 to 05:00 — straight copy of the source
  • 05:00 — record the energy level using a trigger-based template sensor (call that value x)
  • 05:00 to 19:00 — output x
  • 19:00 — record the energy level from the source sensor using another trigger-based template sensor (call that value y)
  • 19:00 to 23:59 — output the source sensor minus (y-x).

I realized that I also have the sensor that measures the active power in watt, the problem is that it is negative when it is charging and positive when it is discharging, I need to know the consumption in kWh of the battery from 7pm to 5am

sensore name: