Collect kWh between 18:00 until 06: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 18:00 and 06:00?

Anyone has any idea?

Create two tariffs for the utility meter. E.g. day and night. This will create a sensor for each tariff.

Switch tariffs using an automation with time triggers.

@tom_l it is not about switching tariffs, but I want to record energy consumption (kWh) between 18:00 and 06:00 o’clock. The actual electricity usage between those times.

I came up with something like this (not yet tested):

  - sensor:
      name: electricity consumption from 18 to 7
      state_class: measurement
      unit_of_measurement: kWh
      device_class: energy
      icon: mdi:flash
      state: >
        {% set ch = now().strftime("%H")|int %}
        {% if ch >= 18 or ch <=7 %}
          {{ states("sensor.helper_electricity_consumption_today") }}
        {% else %}
        {% endif %}

The sensor.helper_electricity_consumption_today is a utility_meter of electricity consumption which is reset daily. So, the new sensor will have it’s value if inside the time measurement, otherwise 0.

Do you think this would work?

Don’t be confused by the name. That is exactly what my suggestion does.

One sensor (tariff) for 18:00 to 6:00, one for the other time that you can ignore.

I guess I need to keep searching because I fail to understand that suggestion / example. I read few times, but yeah, probably I am not advanced user.

  1. Create a utility meter with two tariffs, Day and Night. This will create two sensors that each accumulate energy when their respective tariff is selected. The cycle length is up to you but it must be more than a day or it will reset at midnight. No cycle and it will count up forever.

  2. Create an automation to switch to the Night tariff at 18:00 and to the Day tariff at 06:00.

So between 18:00 and 06:00 the Night sensor will accumulate energy. At other times the Day sensor will accumulate energy. You can ignore this Day sensor as you are only interested in the night energy.

1 Like

After several attempts, I ended up with this different approach.

The P1 meter has Power Consumption data in kW. I created this template sensor, to get the reading between 18:00 to 07:00 o’clock. Convert the data to watt (W), because it is needed for Riemann Sum conversion.

  - sensor:
      name: power_consumption_between_18_and_7
      state_class: measurement
      icon: mdi:transmission-tower
      unit_of_measurement: W
      device_class: power
      state: >
        {% set ch = now().strftime("%H")|int %}
        {% if ch >= 18 or ch <=7 %}
          {{ states("sensor.electricity_meter_power_consumption")|float*1000}}
        {% else %}
        {% endif %}

Then convert that to kWh using Riemann Sum:

  - platform: integration
    source: sensor.power_consumption_between_18_and_7
    name: electricity_consumption_between_18_and_7
    unit_prefix: k
    unit_time: h
    round: 2
    method: left

Then create utility_meter to record and make chart. This works as well.

    source: sensor.electricity_consumption_between_18_and_7
    cycle: daily
    source: sensor.electricity_consumption_between_18_and_7
    cycle: monthly

    source: sensor.electricity_consumption_between_18_and_7
    cycle: yearly