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.

https://www.home-assistant.io/integrations/utility_meter/#advanced-configuration

@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 %}
          0
        {% 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.

2 Likes

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 %}
          0
        {% endif %}

Then convert that to kWh using Riemann Sum:

sensor:
  - 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.

utility_meter:
  electricity_consumption_between_18_and_7_today:
    source: sensor.electricity_consumption_between_18_and_7
    cycle: daily
    
  electricity_consumption_between_18_and_7_this_month:
    source: sensor.electricity_consumption_between_18_and_7
    cycle: monthly

  electricity_consumption_between_18_and_7_this_year:
    source: sensor.electricity_consumption_between_18_and_7
    cycle: yearly
2 Likes

I like this approach.
Only strugling with what to put where!
Tried the first bit in configuration.yaml but that does not work.
Maybe also the syntax and commands have changed since Feb 23.
Could you give some guidance?

Yeah don’t do that, it is needlessly complicated.

All you need is one utility meter and one automation to switch it from measuring day and night energy.

configuration.yaml

utility_meter:
  energy_from_grid:
    source: sensor.your_energy_sensor_here
    cron: "0 6 * * *" # resets at 6am
    tariffs:
      - day
      - night

automation (you can use the automation editor for this if you wish, or YAML):

- id: 841ae395-4646-4c95-8991-14cdd7e9c834
  alias: 'Set day/night tariff'
  triggers:
    - trigger: time
      at: "18:00:00"
      id: night
    - trigger: time
      at: "06:00:00"
      id: day
  actions:
    - action: select.select_option
      target:
        entity_id:
          - select.energy_from_grid
        data:
          option: "{{ trigger.id }}"

This creates two sensors:

sensor.energy_from_grid_day # 6am to 6pm
sensor.energy_from_grid_night # 6pm to 6am

A new count starts at 6am each day. If you don’t want that and you want them to count up forever (or reset monthly or yearly or whatever) change the cron: option in the utility meter. e.g. Delete cron: the option and it will count up day and night energy forever. Replace it with cycle: monthly to count the day and night energy for a month.