Easily create daily, weekly and monthly meters for multiple instant power sensors

Despite the new awesome energy panel, I believe people (like myself) will typically have a single power meter to add as a source. While this yield accurate readings for the total energy consumption of an installation, it might also be the case that users would like to measure individual consumptions around the house, where they will install individual meters (on power sockets, on light switches, PoE ports, etc).

These power meters usually display instant power consumption in watts, and I didn’t find a straightforward way of visualising the consumption of what they are measuring on a tumbling window of say a day/week/month, etc[1].

I’ve come up with a template that will generate the configuration entries necessary to create, per power meter:

1 - The Riemann sum for the sensor
2 - Three meters for the Rienmann sum sensor: one with a daily cycle, one with a weekly cycle, one with a monthly cycle

The goal is to produce a dashboard equivalent to the energy dashboard, but for each (or a set of) individual power sensors, rather than a single one for the entire house.

It could be useful/interesting/a kink to be able to spot that a given PoE port increases its consumption when someone is watching 4k content on netflix, or that the reason why there’s higher consumption on a rainy day is because more cups of tea are brewed.

To use this, simply paste the following snippet on the templating utility on dev tools:

{## Set your sensors here ##}
{% set base_sensors = {
  "instant_consumption_sensors_in_w": 
  [
     "sensor.kettle_socket",
     "sensor.christmas_tree_socket",
     "sensor.living_room_ap_poe_port",
     "sensor.immersion_heater",
     "sensor.bathroom_lights",
     "sensor.bedroom_heater"
  ]
} %}

This section generates the sensor configurations for the integral calculation of each one of the instance power consumption sensors configured in the variable 'base_sensors'
Paste this in the "sensor:" section of your configuration.yaml

------------>cut from next line
{%- for base_sensor in base_sensors.instant_consumption_sensors_in_w %}
- platform: integration
  source: {{ base_sensor }} 
  unique_id: {{ base_sensor.split(".")[1] }}_energy_spent
  name: {{ base_sensor.split(".")[1].replace("_"," ").title() }} Energy Spent
  unit_prefix: k
  unit_time: h
  method: left
{% endfor -%}
------------>cut until previous line

This section generates the daily, weekly and monthly meters for each of the integrations created above
Paste this in the into your "utility_meter" section of your configuration.yaml

------------>cut from next line
{%- for base_sensor in base_sensors.instant_consumption_sensors_in_w %}
#Meters for {{base_sensor}}
{{ base_sensor.split(".")[1] }}_daily_energy_spent:
  unique_id: {{ base_sensor.split(".")[1] }}_daily_energy_spent
  source: {{ base_sensor }}_energy_spent
  name: {{ base_sensor.split(".")[1].replace("_"," ").title() }} Energy Spent Daily
  cycle: daily
{{ base_sensor.split(".")[1] }}_weekly_energy_spent:
  unique_id: {{ base_sensor.split(".")[1] }}_weekly_energy_spent
  source: {{ base_sensor }}_energy_spent
  name: {{ base_sensor.split(".")[1].replace("_"," ").title() }} Energy Spent Weekly 
  cycle: weekly
{{ base_sensor.split(".")[1] }}_monthly_energy_spent:
  unique_id: {{ base_sensor.split(".")[1] }}_monthly_energy_spent
  source: {{ base_sensor }}_energy_spent
  name: {{ base_sensor.split(".")[1].replace("_"," ").title() }} Energy Spent Monthly
  cycle: monthly
{% endfor -%}
------------>cut until previous line

On the result pane on the right, two segments of configuration will be generated. One for the integrals:

- platform: integration
  source: sensor.kettle_socket 
  unique_id: kettle_socket_energy_spent
  name: Kettle Socket Energy Spent
  unit_prefix: k
  unit_time: h
  method: left

- platform: integration
  source: sensor.christmas_tree_socket 
  unique_id: christmas_tree_socket_energy_spent
  name: Christmas Tree Socket Energy Spent
  unit_prefix: k
  unit_time: h
  method: left

- platform: integration
  source: sensor.living_room_ap_poe_port 
  unique_id: living_room_ap_poe_port_energy_spent
  name: Living Room Ap Poe Port Energy Spent
  unit_prefix: k
  unit_time: h
  method: left

- platform: integration
  source: sensor.immersion_heater 
  unique_id: immersion_heater_energy_spent
  name: Immersion Heater Energy Spent
  unit_prefix: k
  unit_time: h
  method: left

- platform: integration
  source: sensor.bathroom_lights 
  unique_id: bathroom_lights_energy_spent
  name: Bathroom Lights Energy Spent
  unit_prefix: k
  unit_time: h
  method: left

- platform: integration
  source: sensor.bedroom_heater 
  unique_id: bedroom_heater_energy_spent
  name: Bedroom Heater Energy Spent
  unit_prefix: k
  unit_time: h
  method: left

And one for the meters:

#Meters for sensor.kettle_socket
kettle_socket_daily_energy_spent:
  unique_id: kettle_socket_daily_energy_spent
  source: sensor.kettle_socket_energy_spent
  name: Kettle Socket Energy Spent Daily
  cycle: daily
kettle_socket_weekly_energy_spent:
  unique_id: kettle_socket_weekly_energy_spent
  source: sensor.kettle_socket_energy_spent
  name: Kettle Socket Energy Spent Weekly 
  cycle: weekly
kettle_socket_monthly_energy_spent:
  unique_id: kettle_socket_monthly_energy_spent
  source: sensor.kettle_socket_energy_spent
  name: Kettle Socket Energy Spent Monthly
  cycle: monthly

#Meters for sensor.christmas_tree_socket
christmas_tree_socket_daily_energy_spent:
  unique_id: christmas_tree_socket_daily_energy_spent
  source: sensor.christmas_tree_socket_energy_spent
  name: Christmas Tree Socket Energy Spent Daily
  cycle: daily
christmas_tree_socket_weekly_energy_spent:
  unique_id: christmas_tree_socket_weekly_energy_spent
  source: sensor.christmas_tree_socket_energy_spent
  name: Christmas Tree Socket Energy Spent Weekly 
  cycle: weekly
christmas_tree_socket_monthly_energy_spent:
  unique_id: christmas_tree_socket_monthly_energy_spent
  source: sensor.christmas_tree_socket_energy_spent
  name: Christmas Tree Socket Energy Spent Monthly
  cycle: monthly

#Meters for sensor.living_room_ap_poe_port
living_room_ap_poe_port_daily_energy_spent:
  unique_id: living_room_ap_poe_port_daily_energy_spent
  source: sensor.living_room_ap_poe_port_energy_spent
  name: Living Room Ap Poe Port Energy Spent Daily
  cycle: daily
living_room_ap_poe_port_weekly_energy_spent:
  unique_id: living_room_ap_poe_port_weekly_energy_spent
  source: sensor.living_room_ap_poe_port_energy_spent
  name: Living Room Ap Poe Port Energy Spent Weekly 
  cycle: weekly
living_room_ap_poe_port_monthly_energy_spent:
  unique_id: living_room_ap_poe_port_monthly_energy_spent
  source: sensor.living_room_ap_poe_port_energy_spent
  name: Living Room Ap Poe Port Energy Spent Monthly
  cycle: monthly

#Meters for sensor.immersion_heater
immersion_heater_daily_energy_spent:
  unique_id: immersion_heater_daily_energy_spent
  source: sensor.immersion_heater_energy_spent
  name: Immersion Heater Energy Spent Daily
  cycle: daily
immersion_heater_weekly_energy_spent:
  unique_id: immersion_heater_weekly_energy_spent
  source: sensor.immersion_heater_energy_spent
  name: Immersion Heater Energy Spent Weekly 
  cycle: weekly
immersion_heater_monthly_energy_spent:
  unique_id: immersion_heater_monthly_energy_spent
  source: sensor.immersion_heater_energy_spent
  name: Immersion Heater Energy Spent Monthly
  cycle: monthly

#Meters for sensor.bathroom_lights
bathroom_lights_daily_energy_spent:
  unique_id: bathroom_lights_daily_energy_spent
  source: sensor.bathroom_lights_energy_spent
  name: Bathroom Lights Energy Spent Daily
  cycle: daily
bathroom_lights_weekly_energy_spent:
  unique_id: bathroom_lights_weekly_energy_spent
  source: sensor.bathroom_lights_energy_spent
  name: Bathroom Lights Energy Spent Weekly 
  cycle: weekly
bathroom_lights_monthly_energy_spent:
  unique_id: bathroom_lights_monthly_energy_spent
  source: sensor.bathroom_lights_energy_spent
  name: Bathroom Lights Energy Spent Monthly
  cycle: monthly

#Meters for sensor.bedroom_heater
bedroom_heater_daily_energy_spent:
  unique_id: bedroom_heater_daily_energy_spent
  source: sensor.bedroom_heater_energy_spent
  name: Bedroom Heater Energy Spent Daily
  cycle: daily
bedroom_heater_weekly_energy_spent:
  unique_id: bedroom_heater_weekly_energy_spent
  source: sensor.bedroom_heater_energy_spent
  name: Bedroom Heater Energy Spent Weekly 
  cycle: weekly
bedroom_heater_monthly_energy_spent:
  unique_id: bedroom_heater_monthly_energy_spent
  source: sensor.bedroom_heater_energy_spent
  name: Bedroom Heater Energy Spent Monthly
  cycle: monthly

After pasting the segments in the correct sections of the configuration.yaml file (create them if they don’t exist), and restarting HA, a bunch of new sensors and meters will be created, than can be used freely on cards to display or plot the consumptions in the desired periods.

Not sure if this is useful for anyone else. If it is, let me know what you think. If it isn’t, at least now my template code is stored somewhere I won’t lose it.

[1] I can easily have overlooked some way of doing this, so if there is one, I’d love to use it instead of what I’m sharing here.

2 Likes

I like the idea of you using the dev tools template editor to create the yaml config. Very creative.

:+1:

2 Likes

It’s also possible to add those sensors resulting from Riemann Sum Integration directly in the Energy Dashboard (Settings > Dashboards > Energy).
I actually use my devices in two sections:

  • at the “Electricity grid” section (where you can also add a tariff sensor/input which would calculate the money you spent), showing you a nice sum of the spent kWh and cash, and allowing you to see a nice daily/weekly/monthly graph
  • at Individual devices, where you can compare in a horizontal bar the consumption for the same period selected (not sure how else this would be useful, but… that would be the correct section lol)

I know, those graphs are contained to that section and won’t help much showing information elsewhere, but if you’re only looking for the actual consumption graphs, that’s enough, easier and needs less maintenance :slight_smile:

And indeed, nice job with automating the list creation with the templating tools! haha

BTW, I think you mistyped “monthly” instead of “daily” on the title :sweat_smile:

I think the powercalc integration does all this very easily.

Ineed!
Fixed.

I think powercalc (didn’t know about it, so thanks) is aimed to estimate power consumptions for devices that do not have energy meters. In other words, it aims to create power sensors where there isn’t one.

My scripts starts from an existing power sensor, so I don’t think there’s much overlap here.

It adds period sensors to my Kasa power monitoring plugs too

Just read the docs.

Yes, it appears that powercalc with the following configuration:

powercalc:
  create_energy_sensors: true

and

powercalc:
  create_utility_meters: true
  utility_meter_types:
    - daily
    - weekly 
    - monthly

Will produce a very similar result !

1 Like

My goal is to have specialised energy dashboards for a couple of rooms of the installation where I display the values of these sensors but also gauge cards with the instant consumption.

1 Like

Cool! Let us know the end results of your experiments.

Even with actual details coming from the right places in HA, I’m still having trouble with some parts of the Energy Dashboard (trouble as in “bugs”, not as in “can’t configure”). If powercalc is really helpful in your scenarios, let us know!

Was trying to do this, then found its a yaml only option, not in the UI:

https://homeassistant-powercalc.readthedocs.io/en/latest/real-power-sensor.html

I’m fighting graphic/plotting more than anything else. Either through the sensors my scripts generates or through powercalc I get the data alright, it’s how to plot it properly that is making me pulling my hair off.

The closest I got to what I want was with apex, but the bar charts has huge gaps on either side of the graph:

I’m sure there will be some way to get it to work, but I haven’t found it yet.