Any experience with Utility Meter functions?

Hi all

I’m thinking about adding this to HA.
I have so many smart plugs by now, and getting more, that reports the powerusage.
I would very much like to collect this data.
I’ve found some doing the illustration in Grafana. I’ve never used Grafana, so I was thinking that I would like to first collect it in HA, and for this, use utility meter.

I can see that some of my sensors are in W, and not kW, so I’ve divided them with 1000 and changed the denomination to kW.
So I need to reset the counter.

Looking in the docs, I can see a service:

Service utility_meter.reset

But I can’t see that service in the developer tools or in automations until I add a tariff?

But there is no explanation of how a tariff is given, I tried this:

energy:
  tariff:
    - peak: 2.3
  dryer_daily_energy:
    source:  sensor.dryer_power
    cycle: daily
    unit_of_measurement: kWh
  dryer_monthly_energy:
    source:  sensor.dryer_power
    cycle: monthly
    unit_of_measurement: kWh
  dryer_yearly_energy:
    source:  sensor.dryer_power
    cycle: yearly
    unit_of_measurement: kWh
  washer_daily_energy:
    source:  sensor.washer_power
    cycle: daily
    unit_of_measurement: kWh

But nooo.

Now, the next question, showing kW is not correct, it should show kWh, so is that what it actually shows, and just not display the unit correctly (I’ve added the unit myself)?

I guess that when I have this in place, I could go further and have grafana use these data for nice displays?

1 Like

If you use 1kW of power for one hour, that is 1 kWh of energy.

You need to supply the utility meter with an energy sensor (kWh) not a power sensor (kW). Changing the unit is not enough.

Most smart plugs have a daily energy meter as well as instantaneous power sensors. You don’t need the utility meter for these. You already have the “daily energy consumed” in these sensors.

If your smart plugs have a continuous energy meter you can convert them to daily/monthly/weekly meters with the utility meter.

Example of two daily utility meters with two tariffs each made from my Shelly EM’s yearly energy meters:

utility_meter:
  energy_t31_light_and_power:
    source: sensor.shelly_t31_total_energy
    cycle: daily
    tariffs:
      - day
      - night
  energy_t41_heating:
    source: sensor.shelly_t41_total_energy
    cycle: daily
    tariffs:
      - day
      - night

At sunset and sunrise I change the tariffs to night and day respectively. I’m saving these daily values (along with a lot of other data) in a text file just before midnight. After a year I hope to have enough data to accurately size a solar power and battery (for night use) system.

#################################################################
## Theme Automation and energy tarrif setting
#################################################################

- id: set_dark_theme_at_sunset
  alias: 'Set Dark Theme At Sunset'
  initial_state: true
  trigger:
    platform: sun
    event: sunset
  action:
  - service: input_select.select_option
    data:
      entity_id: input_select.select_theme
      option: Night
  - service: utility_meter.select_tariff
    data:
      entity_id:
      - utility_meter.energy_t31_light_and_power
      - utility_meter.energy_t41_heating
      tariff: night

- id: set_light_theme_at_sunrise
  alias: 'Set Light Theme At Sunrise'
  initial_state: true
  trigger:
    platform: sun
    event: sunrise
  action:
  - service: input_select.select_option
    data:
      entity_id: input_select.select_theme
      option: Day
  - service: utility_meter.select_tariff
    data:
      entity_id:
      - utility_meter.energy_t31_light_and_power
      - utility_meter.energy_t41_heating
      tariff: day
  - service: light.lifx_set_state # reset master bed lamp
    entity_id: light.lifx_bedside_lamp
    data:
      brightness: 255
      transition: 5
      kelvin: 2700
#################################################################
## Mains Power Alerts
#################################################################

- id: daily_energy_use_message
  alias: 'Daily Energy Use Message'
  trigger:
    platform: time
    at: '23:59:50'
  action:
  - service: notify.telegram_general
    data_template:
      title: '*Information*'
      message: "Today's energy use was {{ states('sensor.total_energy') }} kWh, which cost ${{states('sensor.total_cost_today')|float }}. Total energy cost since last reset on {{ states('input_datetime.last_reset') }} is ${{ (states('input_number.cumulative_energy_cost')|float + states('sensor.total_cost_today')|float)|round(2) }}"
  - service: input_number.set_value
    data_template:
      entity_id: input_number.cumulative_energy_cost
      value: "{{ states('input_number.cumulative_energy_cost')|float + states('sensor.total_cost_today')|float }}"
  - service: notify.energy_log
    data_template:
      message: "{{ states('sensor.date') }},{{ states('sensor.energy_t31_light_and_power_day') }},{{ states('sensor.energy_t31_light_and_power_night') }},{{ states('sensor.energy_t31_light_and_power_total') }},{{ states('sensor.energy_t41_heating_day') }},{{ states('sensor.energy_t41_heating_night') }},{{ states('sensor.energy_t41_heating_total') }},{{ states('sensor.total_energy_day') }},{{ states('sensor.total_energy_night') }},{{ states('sensor.total_energy') }},{{ state_attr('sensor.power_stats','min_value') }},{{ state_attr('sensor.power_stats','max_value') }},{{ states('sensor.power_stats') }},{{ states('sensor.total_cost_today') }}"

Cumulative energy cost reset script:

reset_cumulative_cost:
  sequence:
  - service: input_number.set_value
    data:
      entity_id: input_number.cumulative_energy_cost
      value: 0
  - service: input_datetime.set_datetime
    entity_id: input_datetime.last_reset
    data_template:
      date: "{{ as_timestamp(now())|timestamp_custom('%Y-%m-%d') }}"

Result:


The daily energy cost graph should be a mini-gragh-card bar graph but it has caching issues at the moment. So the line graph is there until it is fixed. The downward spikes are due to many recent Home Assistant restarts.

Individual smart meters give some data too but I don’t accumulate this. It is just for interest to see what is using the most energy and graphs of power throughout the day.

There are a whole bunch of template sensors used for the first view too:

#################################################################
## Shelly and power sensors
#################################################################

- platform: rest
  name: Shelly EM Wifi
  resource: http://10.1.1.24/status
  username: !secret shelly_usr
  password: !secret shelly_pwd
  authentication: basic
  value_template: '{{ value_json.wifi_sta.rssi }}'
  scan_interval: 180
  unit_of_measurement: dB

- platform: rest
  name: Shelly T31 Total Energy # lights and power
  resource: http://10.1.1.24/status
  username: !secret shelly_usr
  password: !secret shelly_pwd
  authentication: basic
  value_template: '{{ (value_json.emeters.0.total | float /1000)|round(3) }}' # Wh to kWh
  scan_interval: 300 # no point updating faster than this. The Shelly only updates every 5 minutes
  unit_of_measurement: kWh

- platform: rest
  name: Shelly T41 Total Energy # heating and hot water
  resource: http://10.1.1.24/status
  username: !secret shelly_usr
  password: !secret shelly_pwd
  authentication: basic
  value_template: '{{ (value_json.emeters.1.total | float /1000)|round(3) }}'
  scan_interval: 300
  unit_of_measurement: kWh

- platform: mqtt
  name: "Tariff 31 Power"
  state_topic: "shellies/shellyem-B9E940/emeter/0/power"
  value_template: '{{ value|round(1) }}'
  qos: 1
  unit_of_measurement: "W"
  icon: mdi:gauge

- platform: mqtt
  name: "Tariff 41 Power"
  state_topic: "shellies/shellyem-B9E940/emeter/1/power"
  value_template: '{{ value|round(1) }}'
  qos: 1
  unit_of_measurement: "W"
  icon: mdi:gauge

- platform: mqtt
  name: "Mains Voltage"
  state_topic: "shellies/shellyem-B9E940/emeter/0/voltage"
  qos: 1
  unit_of_measurement: "V"
  icon: mdi:flash

- platform: template
  sensors:
    total_power:
      friendly_name: 'Total Power'
      entity_id:
        - sensor.tariff_31_power
        - sensor.tariff_41_power
      value_template: "{{ (states('sensor.tariff_31_power')|float + states('sensor.tariff_41_power')|float)|round(1) }}"
      unit_of_measurement: "W"

- platform: statistics
  name: 'Power Stats'
  entity_id: sensor.total_power
  sampling_size: 50000
  max_age:
    hours: 24
  precision: 1

- platform: template
  sensors:
    energy_t31_light_and_power_total:
      friendly_name: 'Light and Power Total'
      entity_id:
        - sensor.energy_t31_light_and_power_day
        - sensor.energy_t31_light_and_power_night
      value_template: "{{ (states('sensor.energy_t31_light_and_power_day')|float + states('sensor.energy_t31_light_and_power_night')|float)|round(3) }}"
      unit_of_measurement: "kWh"

- platform: template
  sensors:
    energy_t41_heating_total:
      friendly_name: 'Heating and Hot Water Total'
      entity_id:
        - sensor.energy_t41_heating_day
        - sensor.energy_t41_heating_night
      value_template: "{{ (states('sensor.energy_t41_heating_day')|float + states('sensor.energy_t41_heating_night')|float)|round(3) }}"
      unit_of_measurement: "kWh"

- platform: template
  sensors:
    total_energy_day:
      friendly_name: 'Total Energy Day'
      entity_id:
        - sensor.energy_t31_light_and_power_day
        - sensor.energy_t41_heating_day
      value_template: "{{ (states('sensor.energy_t31_light_and_power_day')|float + states('sensor.energy_t41_heating_day')|float)|round(3) }}"
      unit_of_measurement: "kWh"

- platform: template
  sensors:
    total_energy_night:
      friendly_name: 'Total Energy Night'
      entity_id:
        - sensor.energy_t31_light_and_power_night
        - sensor.energy_t41_heating_night
      value_template: "{{ (states('sensor.energy_t31_light_and_power_night')|float + states('sensor.energy_t41_heating_night')|float)|round(3) }}"
      unit_of_measurement: "kWh"

- platform: template
  sensors:
    total_energy:
      friendly_name: 'Total Energy Used Today'
      entity_id:
        - sensor.total_energy_day
        - sensor.total_energy_night
      value_template: "{{ (states('sensor.total_energy_day')|float + states('sensor.total_energy_night')|float)|round(3) }}"
      unit_of_measurement: "kWh"

- platform: template
  sensors:
    t31_cost_today:
      friendly_name: 'T31 Cost Today'
      entity_id:
        - sensor.energy_t31_light_and_power_total
        - input_number.t31_daily_cost
        - input_number.t31_energy_cost
      value_template: "{{ (states('sensor.energy_t31_light_and_power_total')|float * states('input_number.t31_energy_cost')|float + states('input_number.t31_daily_cost')|float)|round(2) }}"
      unit_of_measurement: "$"

- platform: template
  sensors:
    t41_cost_today:
      friendly_name: 'T41 Cost Today'
      entity_id:
        - sensor.energy_t41_heating_total
        - input_number.t41_daily_cost
        - input_number.t41_energy_cost
      value_template: "{{ (states('sensor.energy_t41_heating_total')|float * states('input_number.t41_energy_cost')|float + states('input_number.t41_daily_cost')|float)|round(2) }}"
      unit_of_measurement: "$"

- platform: template
  sensors:
    total_cost_today:
      friendly_name: 'Total Cost Today'
      entity_id:
        - sensor.t31_cost_today
        - sensor.t41_cost_today
      value_template: "{{ ( states('sensor.t31_cost_today')|float + states('sensor.t41_cost_today')|float )|round(2) }}"
      unit_of_measurement: "$"

- platform: template
  sensors:
    cumulative_energy_cost:
      friendly_name: 'Cumulative Energy Cost'
      entity_id:
        - input_number.cumulative_energy_cost
      value_template: "{{ states('input_number.cumulative_energy_cost')|float|round(2) }}"
      unit_of_measurement: "$"

- platform: template
  sensors:
    last_reset:
      friendly_name: 'Last Reset'
      entity_id:
        - input_datetime.last_reset
      value_template: "{{ states('input_datetime.last_reset') }}"

6 Likes

Being there… But the answer is simple. IF you are using the “instant” power reported by the power meter, you have to also use the integration sensor. Let’s say you have a sonoff pow reporting power in watts, then:

sensor:
  - platform: integration
    source: sensor.pow_power
    name: pow_power_in
    round: 2
    unit_prefix: k
utility_meter:
    consumo_mensal:
      source: sensor.pow_power_in
      cycle: monthly
4 Likes

So for a Sonoff POW in ESPhome that has a total daily energy value, that is not a correct value for utility meter?

Ok so I need to use integration on the power sensor, and forget about the total daily energy sensor?

Not for a daily meter (original question) because the energy used already resets daily. There is no need for a utility meter.

The integration sensor is definitely a way around this if you want more than a daily utility meter (weekly / monthly etc…) and only have a daily energy accumulator in the smart switch.

1 Like

Hi @tom_l and @clyra This is super info, thankyou very much, it is much clearer to me now.
I have ESPHome on a lot of my items, so I can use the internal ‘daily’ in that, and then I can use the integral function for my Fritz!Box sensors, that only supports instantaneus and ‘total consumption’.
I still don’t have a firm grasp on tariff.
For me a tariff is a cost per kWh, but that is not set up?
And with our current company we only have a flat-rate, so I guess I only have on tariff?

I have ESPHome on a lot of my items, so I can use the internal ‘daily’ in that, and then I can use the integral function for my Fritz!Box sensors, that only supports instantaneus and ‘total consumption’.

So you have sensor.xyz_daily_energy meters already. Just add them all up. No need for a utility meter (unless you want more than daily cycles, like monthly - then use the power integration method posed above by @clyra).

I still don’t have a firm grasp on tariff.
For me a tariff is a cost per kWh, but that is not set up?

Yeah, ‘tariff’ defined in the utility meter just splits the energy consumption into seperate ‘meters’.

So I have a day and night tariff I can measure how much energy I use during the day or during the night. My template sensors (see post above) multiply these values by my power company’s tariff rates to calculate the costs. This could equally be peak and off peak rather than day or night.

And with our current company we only have a flat-rate, so I guess I only have on tariff?

Correct.

My power company has separate charges for ‘light and power’ (T31) and ‘heating and hot water’ (T41). But I have separate meters for these so I don’t need to use tariffs.

1 Like

Ok, gotcha

Unfortunately due to the tbh shitty state of ESPHome 1.14.3, now all the sensors are offline in HA because it recompiled it to 1.14.3 (it was rocksolid on 1.13.6), but because I had to add the extra sensors in it, I had to recompile. :frowning:
Geez, this version is crappy, it’s happy for an hour or two, and then it f… up.

I did read about your in wall sonoff issues and was a bit worried but I flashed an ESP32 board two days ago with the latest version and it has been running rock solid. I wonder if it is just an ESP8266 issue?

esphome:
  name: bt_gw_ensuite
  platform: ESP32
  board: mhetesp32minikit

wifi:
  ssid: 'WAPLO'
  password: !secret wifi_pwd
  manual_ip:
    static_ip: 10.1.1.67
    gateway: 10.1.1.1
    subnet: 255.255.255.0

api:
  password: !secret api_password

logger:
  level: WARN

ota:
  password: !secret esp_pwd

esp32_ble_tracker:

binary_sensor:
  - platform: status
    name: "Ensuite BT GW Status"

sensor:
  - platform: wifi_signal
    name: "Ensuite BT GW WiFi Signal"
    update_interval: 15s
    filters:
      - sliding_window_moving_average:
          window_size: 15
          send_every: 15
          send_first_at: 15

  - platform: ble_rssi
    mac_address: E7:2E:00:D2:E0:BF
    name: "Ensuite BT RSSI"
    icon: mdi:bluetooth
    filters:
      - sliding_window_moving_average:
          window_size: 15
          send_every: 15


  # - platform: dht
  #   pin: D5
  #   model: DHT22
  #   update_interval: 15s
  #   temperature:
  #     name: "Ensuite Temperature"
  #     filters:
  #       - sliding_window_moving_average:
  #           window_size: 15
  #           send_every: 15
  #           send_first_at: 15
  #   humidity:
  #     name: "Ensuite Humidity"
  #     filters:
  #       - sliding_window_moving_average:
  #           window_size: 15
  #           send_every: 15
  #           send_first_at: 15

  - platform: xiaomi_lywsd02
    mac_address: E7:2E:00:D2:E0:BF
    temperature:
      name: "Ensuite BT Temperature"
      filters:
        - sliding_window_moving_average:
            window_size: 6
            send_every: 6
            send_first_at: 1
    humidity:
      name: "Ensuite BT Humidity"
      filters:
        - sliding_window_moving_average:
            window_size: 6
            send_every: 6
            send_first_at: 1

Yes, I beleive so, I also have a some ESP32’s here, and I have seen no issues with them.
Now I’ve restored my 1.13.6 back on the Hass.io and I have recompiled all those with issues, and now everything is working again.

Ok, so now I have all my Sonoff Pow R2 and BlitzWolf SHP6 showing with daily usage.
I’m not sure when they reset, is that settable in ESPHome?
I’m still trying to tweak my Fritz!Box switches, so far I would like to keep them as they have an ‘outdoor’ units. I have a few extra SHP6 units that I just need to flash, but I would like to get my flashing ‘jig’ first, so I don’t have to take them all apart.

So I created this in my ‘sensors’:

  - platform: template
    sensors:
      serverwattage:
        friendly_name: "Server forbrug"
        unit_of_measurement: "kW"
        device_class: power
        value_template: "{{ state_attr('switch.server', 'current_power_w') | multiply(0.001) | float }}"
  - platform: integration
    source: sensor.serverwattage
    name: server_total_usage

That will just continuesly sum up as I can see it.
Looking in the developer console, it shows:

sensor.serverwattage:
unit_of_measurement: kW
friendly_name: Server forbrug
device_class: power
icon: mdi:flash
sensor.server_total_usage:
source: sensor.serverwattage
unit_of_measurement: kWh
friendly_name: server_total_usage
icon: mdi:chart-histogram

Which I think looks very correct.
Now I create the utility sensor:

  server_daily_energy:
    source: sensor.server_total_usage
    cycle: daily

Looking in the developer console, it get’s weird:

source: sensor.server_total_usage
status: collecting
last_period: 0
last_reset: 2019-11-22T07:11:10.596359+01:00
meter_period: daily
unit_of_measurement: W
friendly_name: server_daily_energy
icon: mdi:counter

This shows W, not Wh, and not even kWh?

Hi @tom_l
I’m trying to set it up like yours, but I seem to have some problems in getting the ‘midnight’ automation set up. I’ve made this so far:

- id: '1577539305551'
  alias: Rapportér dagligt forbrug
  description: ''
  trigger:
  - at: '23:59:50'
    platform: time
  condition: []
  action:
  - data:
      data_template:
        message: 'Samlet forbrug for dagen: {{ states(''sensor.total_daily_power'')
          }} {{ state_attr(''sensor.total_daily_power'',''unit_of_measurement'') }}'
        title: Strømforbrug
    service: persistent_notification.create
  - data:
      data_template:
        entity_id: input_number.yesterday_powerusage
        value: '{{ states(''sensor.total_daily_power'')|float }}'
    service: input_number.set_value

But for some reason I don’t get a notification, and I can’t get it to set the old variable.
Furthermore when I make an entity card, the input_number is showed with a slider, I just want it to show a number:
image

You have extra ’ around the sensors, should only be one set of ’

Example:

message: "Samlet forbrug for dagen: {{ states('sensor.total_daily_power') }} {{ state_attr('sensor.total_daily_power','unit_of_measurement') }}"
1 Like

Two things,

  1. Don’t use the UI to generate automations with templates. Compare the quoting in mine to that generated for yours.

  2. Look at the mode option, https://www.home-assistant.io/integrations/input_number/

1 Like

Darned, you are right, the editor made the quotes all odd.
I’ve changed them to what I had set them to, and now the automation works.
I can see that changing the mode didn’t help me that much, it’s not shown as a slider, but it is still shown as an input from lovelace.
So maybe the only way to make it a ‘read only’ entity, is to make an extra template sensor that shows the value.

Thanks, yes, it must be the editor doing that for me.

Yes the key is in the integration name, input_number.

Yes this is what you will have to do.

1 Like

Hi there!
I have a Shelly EM. It properly works in home assistant but I have some problem for the utility meter functions.

This is my configuration. Could you help me to find the error?
Thank you so much.

#############
# SHELLY EM #
#############
switch:
  - platform: mqtt
    name: "Output"
    state_topic: "shellies/shellyem-XXXXXX/relay/0"
    command_topic: "shellies/shellyem-XXXXXX/relay/0/command"
    qos: 1
    payload_on: "on"
    payload_off: "off"
    retain: false
 
sensor:
  - platform: mqtt
    name: "Power 1"
    state_topic: "shellies/shellyem-XXXXXX/emeter/0/power"
    unit_of_measurement: "W"
  - platform: mqtt
    name: "Reactive power 1"
    state_topic: "shellies/shellyem-XXXXXX/emeter/0/reactive_power"
    unit_of_measurement: "PR"
  - platform: mqtt
    name: "Voltage 1"
    state_topic: "shellies/shellyem-XXXXXX/emeter/0/voltage"
    unit_of_measurement: "V"
 
  - platform: mqtt
    name: "Power 2"
    state_topic: "shellies/shellyem-XXXXXX/emeter/1/power"
    unit_of_measurement: "W"
  - platform: mqtt
    name: "Reactive power 2"
    state_topic: "shellies/shellyem-XXXXXX/emeter/1/reactive_power"
    unit_of_measurement: "PR"
  - platform: mqtt
    name: "Voltage 2"
    state_topic: "shellies/shellyem-XXXXXX/emeter/1/voltage"
    unit_of_measurement: "V"

##############################
## Shelly and power sensors ##
##############################

  - platform: rest
    name: Shelly EM Wifi
    resource: http://192.168.1.47/status
    value_template: '{{ value_json.wifi_sta.rssi }}'
    scan_interval: 180
    unit_of_measurement: dB

  - platform: rest
    name: Shelly Casa
    resource: http://xxx.xxx.xxx.xx/status #Shelly's IP
    value_template: '{{ (value_json.emeters.0.total | float /1000)|round(3) }}' # Wh to kWh
    scan_interval: 300 # no point updating faster than this. The Shelly only updates every 5 minutes
    unit_of_measurement: kWh

  - platform: rest
    name: Shelly PdC
    resource: http://xxx.xxx.xxx.xx/status #Shelly's IP
    value_template: '{{ (value_json.emeters.1.total | float /1000)|round(3) }}'
    scan_interval: 300
    unit_of_measurement: kWh

  - platform: mqtt
    name: "Energia Casa"
    state_topic: "shellies/shellyem-XXXXXX/emeter/0/power"
    value_template: '{{ value|round(1) }}'
    qos: 1
    unit_of_measurement: "W"
    icon: mdi:gauge

  - platform: mqtt
    name: "Energia PdC"
    state_topic: "shellies/shellyem-XXXXXX/emeter/1/power"
    value_template: '{{ value|round(1) }}'
    qos: 1
    unit_of_measurement: "W"
    icon: mdi:gauge

  - platform: template
    sensors:
      total_power:
        friendly_name: 'Total Power'
        entity_id:
          - sensor.energia_casa
          - sensor.energia_pdc
        value_template: "{{ (states('sensor.energia_casa')|float + states('sensor.energia_pdc')|float)|round(1) }}"
        unit_of_measurement: "W"

  - platform: statistics
    name: 'Power Stats'
    entity_id: sensor.total_power
    sampling_size: 50000
    max_age:
      hours: 24
    precision: 1

#################
# UTILITY METER # 
#################
utility_meter:
  energy:
    source: sensor.energia_casa
    cycle: monthly
  energy:
    source: sensor.energia_pdc
    cycle: monthly
  daily_energy:
    source: sensor.energia_casa
    cycle: daily
  monthly_energy:
    source: sensor.energia_casa
    cycle: monthly
  daily_energy:
    source: sensor.energia_pdc
    cycle: daily
  monthly_energy:
    source: sensor.energia_pdc
    cycle: monthly

Naming a power as sensor an energy sensor does not make it an energy sensor.

  - platform: mqtt
    name: "Energia Casa"
    state_topic: "shellies/shellyem-XXXXXX/emeter/0/power"
                                                      ^---- see, power, not energy.

You need to use actual energy sensors in your utility meters. This one for example:

  - platform: rest
    name: Shelly Casa
    resource: http://xxx.xxx.xxx.xx/status #Shelly's IP
    value_template: '{{ (value_json.emeters.0.total | float /1000)|round(3) }}' # Wh to kWh
    scan_interval: 300 # no point updating faster than this. The Shelly only updates every 5 minutes
    unit_of_measurement: kWh