How to calculate daily power consumption from DSMR sensors?

Hi All,
I’m quite new at HASS, but got my configuration to work. Much easier with HASS than with other Domotic’s! Compliments for the developers! :+1:
Part of my configuration is the DSMR component which sensors actual power consumption, and the total power consumption.
I am looking for a way to keep track of daily power consumption:
Subtract the values of total power consumption at midnight and the value at the midnight before. But I could not find a way to access the historic value. Perhaps there are easier ways to achieve this? How to do this? Any help will be appreciated!

3 Likes

Hi, I’m try to achive the same… did you ever figure out how to do this?

Hi!
No, I did try some things with templates and also combined with MQTT, but did not succeed in this topic. Do you have any suggestions?

Nope, I’m also new to HA, so my knowledge is limited. Is it maybe possible to update a custom sensor at the beginning of the day capture the meter begin usage and update another custom sensor at the end of the day to capture the end usage of that day. And than calculate to the usage for that day?

Should get you values over a period of time.

1 Like

Hi Pedro, I’ve looked at this… maybe I don’t get it but I think what I want to achieve isn’t possible with this sensor.

we have a sensor called “sensor.power_consumption_normal” it’s value at the beginning of the day is lets say “1916.385” then at the end of the day, it’s value is let say “2016.385”.

Total energy use for this day would be 100. (end value 2016.385 minus begin value 1916.385)

I would like to have a new sensor that’s called “senor.power_consumption_today” with that value…

Is that possible? if so… how?

All help is greatly appreciated!

1 Like

I think you can do it by creating 3 automations + input_numbers that get the value of your real sensor at a specific time:

  1. to get the value at the beginning of the day (get the value of sensor when time is 00:00:01)
  2. to get the value at the end of the day (get the value of the sensor when time is 23:59:59)
  3. to calculate the value for a day (2-1)

If someone has other way or idea, please share it.

For example, this is how i get the correct date when my MiRobot starts cleaning:

  - alias: MiRobot last runtime
    hide_entity: True
    trigger:
      platform: template
      value_template: "{{ (states.vacuum.mirobot.attributes.status) == 'Cleaning' }}"
    action:
      - service: input_datetime.set_datetime
        data_template:
          entity_id: input_datetime.mirobot_last_runtime
          time: '{{ (as_timestamp(now()) | timestamp_custom("%H:%M:%S", true)) }}'
          date: '{{ (as_timestamp(now()) | timestamp_custom("%Y-%m-%d", true)) }}' 

I think you can use something similar, but using input_number instead of datetime.

something like this (didn’t test it):

input_number:
  consumption_morning:
    name: Consumption morning
    min: 9
    max: 999999999
	
  consumption_evening:
    name: Consumption evening
    min: 9
    max: 999999999

  daily_consumption:
    name: Consumption evening
    min: 9
    max: 999999999	

	
automation:
  - alias: Start of the day value
    trigger:
      platform: time
      at: '00:00:01'
    action:
      service: input_number.set_value
      data_template:
        entity_id: input_number.consumption_morning
        value: "{{ states.sensor.your_sensor_name.state }}"
		
  - alias: End of the day value
    trigger:
      platform: time
      at: '23:59:58'
    action:
      service: input_number.set_value
      data_template:
        entity_id: input_number.consumption_evening
        value: "{{ states.sensor.your_sensor_name.state }}"
		
  - alias: Calculate daily consumption
    trigger:
      platform: time
      at: '23:59:59'
    action:
      service: input_number.set_value
      data_template:
        entity_id: input_number.daily_consumption
        value: "{{ (states.input_number.consumption_evening.state) - (states.input_number.consumption_morning.state) }}"

Great, thanks alot! going to try if I can get this to work!

I don’t think this will work because it will not give you the last 24 hours. If you ever want to know your current power consumption, it will always be behind in time and not current.

Just my 2 cents.

Piggy backing off @an20dei’s idea, this could work with a template sensor and an input boolean to give you todays power consumption up til now. His method will always be 24 hours behind and won’t show you the current power consumption for today.

input_number:
  midnight_consumption:
    name: Midnight Power Level
    min: 0
    max: 999999999

automation.

there’s no need to start it at 1 second, just do it at midnight. It’s the start of the night.

  - alias: Start of the day value
    trigger:
      platform: time
      at: '00:00:00'
    action:
      service: input_number.set_value
      data_template:
        entity_id: input_number.midnight_consumption
        value: "{{ states.sensor.your_sensor_name.state }}"

and your sensor to always display the current state which will get updated every time your sensor updates:

sensor:
  platform: template
  sensors:
    current_power_consumption:
      value_template: >
        {{ states.sensor.your_sensor_name.state - states.input_number.midnight_consumption.state }}
      unit_of_measurement: 'W'

This way, your power consumption is always up to date and doesn’t rely on a automation to populate. Also, you can assign a unit_of_measurement and it will appear as a sensor.

Finally got this working! Thanks for the help and hints!
Here is my config:

In input_number.yaml:

  midnight_power_consumption_low:
    name: Midnight Power Level Low
    min: 0
    max: 999999999
  midnight_power_consumption_normal:
    name: Midnight Power Level Normal
    min: 0
    max: 999999999

In sensors.yaml:

- platform: template
  sensors:
    todays_power_consumption_low:
      value_template: >
        {{ (float(states.sensor.power_consumption_low.state) - float(states.input_number.midnight_power_consumption_low.state)) | round(3) }}
      unit_of_measurement: 'kWh'
      friendly_name: "Today's Power Consumption Low"

- platform: template
  sensors:
    todays_power_consumption_normal:
      value_template: >
        {{ (float(states.sensor.power_consumption_normal.state) - float(states.input_number.midnight_power_consumption_normal.state)) | round(3) }}
      unit_of_measurement: 'kWh'
      friendly_name: "Today's Power Consumption Normal"
      
- platform: template
  sensors:
    todays_power_consumption:
      value_template: >
        {{ ((float(states.sensor.power_consumption_normal.state) - float(states.input_number.midnight_power_consumption_normal.state)) + 
           (float(states.sensor.power_consumption_low.state) - float(states.input_number.midnight_power_consumption_low.state))) | round(3) }}
      unit_of_measurement: 'kWh'
      friendly_name: "Today's Power Consumption"

In automations.yaml:

- alias: Power Consumption Low at start of the day
  trigger:
    platform: time
    at: '00:00:00'
  action:
    service: input_number.set_value
    data_template:
      entity_id: input_number.midnight_power_consumption_low
      value: "{{ states.sensor.power_consumption_low.state }}"

- alias: Power Consumption High at start of the day
  trigger:
    platform: time
    at: '00:00:00'
  action:
    service: input_number.set_value
    data_template:
      entity_id: input_number.midnight_power_consumption_normal
      value: "{{ states.sensor.power_consumption_normal.state }}"

You can add to groups if you like of course.
Any optimizations are welcome!
Thanks!
Gerben

2 Likes

Yes me too! Was still figuring out how to get the decimals working, but I see you already did. :wink:

Thanks alot everyone!

1 Like

Looks good. Only optimization I can see is combining your automation into 1 automation:

- alias: Power Consumption start of the day
  trigger:
    platform: time
    at: '00:00:00'
  action:
    - service: input_number.set_value
      data_template:
        entity_id: input_number.midnight_power_consumption_normal
        value: "{{ states.sensor.power_consumption_normal.state }}"
    - service: input_number.set_value
      data_template:
        entity_id: input_number.midnight_power_consumption_low
        value: "{{ states.sensor.power_consumption_low.state }}"
1 Like

Also, for calculating the total power usage today, I just added together the _low and _normal sensor (instead of using the values of the input_numbers):

 - platform: template
    sensors:
      power_consumption_today_total:
        value_template: >
          {{ (float(states.sensor.power_consumption_today_normal.state) + float(states.sensor.power_consumption_today_low.state)) | round(3) }}
        unit_of_measurement: 'kWh'

And since I have solar panels, I’ve also added those in the calculations. My current configuration in the frontpage looks like this (still need to add some friendly names):

1 Like

i try to implement that myself. i have a power meter that reports actual wattage and got a sensor that takes this value every 30s and sums up the kWh.
i just don’t get this “normal” and “low” sensor implemented in this example. what are those two for? i use only one sensor value for yesterday’s power usage?!

Well this can probably done using the statistics component now.

Just use a max age of 24 hours. And the value you would want would be the ‘total’ attribute.

i couldn’t acchieve with statistics sensor what i wanted to do, so i tried to adopt the code shown above.
for testing i set the interval to 5min. but something seems to go wrong.

- alias: Power Consumption at start of the hour
  trigger:
    platform: time
    minutes: '/5'
  action:
    service: input_number.set_value
    data_template:
      entity_id: input_number.hourly_power_consumption
      value: "{{ states.sensor.today_kwh.state.state }}"

the automation is triggered multiple times every 5min. and i don’t know why?!?

can’t say how many times exactly it’s called, but i guess it’s about 20x.

image

got that solved myself: it need the

seconds: 00

as well, or it will do this from 05:00 -05:59

The normal and low are power tariffs. From 23:00u at night until 6:00u in the morning, electricity is cheaper (low tariff). Almost every household in The Netherlands has two power tariffs and two meters (a normal an low meter).

:wink:

Thanks guys for scripts, I was able to implement it, however below line did not worked for me. Maybe
power_consumption_today_normal and power_consumption_today_low are not yet available in the state. So I did all today’s calculations again like gdschut