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?
@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.
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.
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.
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.
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?