Daily energy monitoring

Ok I finally found them here. Nice looking product but they are not cheap…I guess you get what you pay for eh?
Cheers!

Hi @tom_l
I got a question for you :slight_smile:
What data is the Shelly EM returning in kWh?
A total cumulative, a daily cumulative or the usage from last request?

I am asking because I cannot make it wok with my template sensors (I don’t have a Shelly but some Smart Plugs I am using to test those things out).

What happen to me, is that at every restart HA re-calculate the total and basically used kWH literally double up.

Do you have any clue/insight? :slight_smile:

Thanks

Andrea

The Shelly EM is specifically designed to monitor two energy sources via current clamps.

It has two mqtt sensors that measure in Wminutes but they only report instantaneous energy (over one minute) so they reset to zero as soon as power stops flowing.

There are cumulative Wh meters that survive restarts but they are not available via mqtt. I had to use restful sensors to read them:

- 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

You can read about my trials getting this going here:

Thanks for that.
Unfortunately my setup is slightly different, I do use Tasmota on those Meter and they send a SENSOR status on the tele topic with the informatimons,

In this message I got a “Total” value which is a cumulative kWh used since it started to work the first time, it also survive restarts so it should technically work for me.

Unfortunately I don’t understand why HA treat it as a “total” value on each restart, so basically it takes that value and add it up to the previous stored value.

I have the feeling that this is cause by the mqtt sensor to be set to 0 on startup, then as soon as the tele message arrives it update to the value, so the utility_meter first see a “0”, than a “108”, and it adds 108 (sample numbers).

I am now checking how to retain tele messages in Tasmota since the retainsensor option I found doesn’t seems to be working as expected.

In your setup on another thread I saw you have per-device today and yesteday values, which to me looked like the Tasmota output, so I figure that maybe you had some ideas about that :smiley:

Thanks

The individual devices are not using the utility meter. They are direct readings from the device. E.g.

- platform: mqtt
  name: "Dishwasher Energy Today"
  state_topic: "tele/sonoff_dishwasher/SENSOR"
  value_template: '{{ value_json["ENERGY"]["Today"] }}'
  unit_of_measurement: "kWh"

I am testing the Emporia Vue. Overall it is a really nice device and super cheap at only $50. I reached out to their support and they said they have API capabilities on their road map, but no specific timeline.

Their app is nice, a little dated looking, but has some nice data in it. It updates every 2 seconds for current usage.

I am hoping they add something soon so that I could leverage it in Home Assistant. If not, I may send it back as I don’t want to view my data only in their App. I asked them to just make current watts in text available from a URL that I could access locally. That would be enough to be able to achieve what I want.

1 Like

I really appreciate this post. I’m pretty new to Home Assistant and energy monitoring was one of my primary goals. I’m running a couple of Shelly EMs now and basing my configuration around this post. Thanks !

1 Like

Nice work :slight_smile: @tom_l

Whitch Lovelace card you sused for this:
pic

Can you post configuration?

Mini-graph card.

cards:
  - color_thresholds:
      - color: '#039BE5'
        value: 0
      - color: '#0da035'
        value: 600
      - color: '#e0b400'
        value: 1200
      - color: '#e45e65'
        value: 2400
    color_thresholds_transition: hard
    entities:
      - sensor.tariff_31_power
      - color: 'rgba(0,0,255,1)'
        entity: binary_sensor.dark_outside
        show_line: false
        y_axis: secondary
    group: false
    hour24: true
    hours_to_show: 24
    line_width: 4
    name: Lights and Power
    points_per_hour: 4
    show:
      extrema: true
      fill: fade
      icon: true
      labels: false
      name: true
      state: true
    state_map:
      - label: Day
        value: 'off'
      - label: Night
        value: 'on'
    style: |
      ha-card {
        border: solid 2px var(--primary-color);
      }
    type: 'custom:mini-graph-card'
  - color_thresholds:
      - color: '#039BE5'
        value: 0
      - color: '#0da035'
        value: 600
      - color: '#e0b400'
        value: 1200
      - color: '#e45e65'
        value: 2400
    color_thresholds_transition: hard
    entities:
      - sensor.tariff_41_power
      - color: 'rgba(0,0,255,1)'
        entity: binary_sensor.dark_outside
        show_line: false
        y_axis: secondary
    group: false
    hour24: true
    hours_to_show: 24
    line_width: 4
    name: Heating and Hot Water
    points_per_hour: 4
    show:
      extrema: true
      fill: fade
      icon: true
      labels: false
      name: true
      state: true
    state_map:
      - label: Day
        value: 'off'
      - label: Night
        value: 'on'
    style: |
      ha-card {
        border: solid 2px var(--primary-color);
      }
    type: 'custom:mini-graph-card'
  - color_thresholds:
      - color: '#039BE5'
        value: 0
      - color: '#0da035'
        value: 600
      - color: '#e0b400'
        value: 1200
      - color: '#e45e65'
        value: 2400
    color_thresholds_transition: hard
    entities:
      - sensor.total_power
      - color: 'rgba(0,0,255,1)'
        entity: binary_sensor.dark_outside
        show_line: false
        y_axis: secondary
    group: false
    hour24: true
    hours_to_show: 24
    line_width: 4
    points_per_hour: 4
    show:
      extrema: true
      fill: fade
      icon: true
      labels: false
      name: true
      state: true
    state_map:
      - label: Day
        value: 'off'
      - label: Night
        value: 'on'
    style: |
      ha-card {
        border: solid 2px var(--primary-color);
      }
    type: 'custom:mini-graph-card'
type: vertical-stack
1 Like

Thanks. @tom_l

Can you also post config for this sensor: binary_sensor.dark_outside

It’s a bit convoluted and probably won’t be much use to you.

“Dark Outside” depends on my calculated_light_brightness which is the brightness I automatically set some of my lights to. That brightness depends on a linear equation related to a lux sensor

brightness = -0.1785*lux + 298.35

This was an equation I developed using a spreadsheet and observation of outside light level vs light required indoors (manually tweaked during a week or so of testing).

It is a highly subjective equation (I tried logarithmic and exponential equations but found linear the best) and depends on the sensor type (but is closely related to the 30-70 lux switching of street lights). It won’t be any use to you unless you have the same sensor (BH1750) and house windows.

I think the bit concerning the sun elevation and default values was to prevent some sort of glitching on restarts. I really don’t remember. Comment your code kids! It looks like if the lux sensor is unavailable the light brightness is set to 0 during the day and 128 at night. Other variables actually determine if the lights are on or off. Different lights have different dependencies (movement, house or bed occupancy etc…).

The min/max filters ensure the value is always between 0 and 255.

Finally, if my lights are set to a brightness of greater than 24, I call it dark outside (more like a light twilight just requiring lights).

- platform: template
  sensors:
    calculated_light_brightness:
      friendly_name: 'Calculated Light Brightness'
      entity_id:
        - sensor.outside_light_level
      value_template: >
        {% if states('sensor.solar_elevation') | float(default=1) > 0 %}
          {{ [ [ (-0.1785 * states('sensor.outside_light_level') | float(default=2000) + 298.35) | int, 255 ] | min, 0 ] | max }}
        {% else %}
          {{ [ [ (-0.1785 * states('sensor.outside_light_level') | float(default=954) + 298.35) | int, 255 ] | min, 0 ] | max }}
        {% endif %}
      unit_of_measurement: "Int"

- platform: template
  sensors:
    dark_outside:
      friendly_name: Dark Outside
      entity_id: sensor.calculated_light_brightness 
      value_template: "{{ states('sensor.calculated_light_brightness') | int >= 24 }}"

Surprisingly this mess works very well for me.

EDIT: also the graph above does not actually plot dark outside. Must have been something I developed later:

A much simpler solution you could implement uses the time of day binary sensor:

binary_sensor:
  - platform: tod
    name: Dark Outside
    after: sunset
    before: sunrise

This can also have offsets applied if required.

Thanks.

I still have problem showing cards.

I put in one sensor, just for the test. I get blank card no errors:

blank_card

cards:
  - color_thresholds:
      - color: '#039BE5'
        value: 0
      - color: '#0da035'
        value: 600
      - color: '#e0b400'
        value: 1200
      - color: '#e45e65'
        value: 2400
    color_thresholds_transition: hard
    entities:
      - sensor.mqtt_pow_hallway_climate_meter_total
      - color: 'rgba(0,0,255,1)'
        entity: binary_sensor.dark_outside
        show_line: false
        y_axis: secondary
    group: false
    hour24: true
    hours_to_show: 24
    line_width: 4
    name: Lights and Power
    points_per_hour: 4
    show:
      extrema: true
      fill: fade
      icon: true
      labels: false
      name: true
      state: true
    state_map:
      - label: Day
        value: 'off'
      - label: Night
        value: 'on'
    style: |
      ha-card {
        border: solid 2px var(--primary-color);
      }
    type: 'custom:mini-graph-card'
  - color_thresholds:
      - color: '#039BE5'
        value: 0
      - color: '#0da035'
        value: 600
      - color: '#e0b400'
        value: 1200
      - color: '#e45e65'
        value: 2400
    color_thresholds_transition: hard
    entities:
      - sensor.mqtt_pow_hallway_climate_meter_total
      - color: 'rgba(0,0,255,1)'
        entity: binary_sensor.dark_outside
        show_line: false
        y_axis: secondary
    group: false
    hour24: true
    hours_to_show: 24
    line_width: 4
    name: Heating and Hot Water
    points_per_hour: 4
    show:
      extrema: true
      fill: fade
      icon: true
      labels: false
      name: true
      state: true
    state_map:
      - label: Day
        value: 'off'
      - label: Night
        value: 'on'
    style: |
      ha-card {
        border: solid 2px var(--primary-color);
      }
    type: 'custom:mini-graph-card'
  - color_thresholds:
      - color: '#039BE5'
        value: 0
      - color: '#0da035'
        value: 600
      - color: '#e0b400'
        value: 1200
      - color: '#e45e65'
        value: 2400
    color_thresholds_transition: hard
    entities:
      - sensor.mqtt_pow_hallway_climate_meter_total
      - color: 'rgba(0,0,255,1)'
        entity: binary_sensor.dark_outside
        show_line: false
        y_axis: secondary
    group: false
    hour24: true
    hours_to_show: 24
    line_width: 4
    points_per_hour: 4
    show:
      extrema: true
      fill: fade
      icon: true
      labels: false
      name: true
      state: true
    state_map:
      - label: Day
        value: 'off'
      - label: Night
        value: 'on'
    style: |
      ha-card {
        border: solid 2px var(--primary-color);
      }
    type: 'custom:mini-graph-card'
type: vertical-stack
1 Like

Now is working binary_sensor.dark_outside was missing. :slight_smile:

Also the style: options require card-mod.

Thanks.

I installed card-mod, but don’t see difference:)

My not look so fancy like yours:) Why?
card_look

Here my config:

cards:
  - color_thresholds:
      - color: '#039BE5'
        value: 0
      - color: '#0da035'
        value: 600
      - color: '#e0b400'
        value: 1200
      - color: '#e45e65'
        value: 2400
    color_thresholds_transition: hard
    entities:
      - color: blue
        entity: sensor.bathroom_washer_and_dryer_electrical_energy_today
        name: EM
      - color: red
        entity: sensor.bathroom_washer_and_dryer_electrical_energy_tariff_vt
        name: VT
      - color: green
        entity: sensor.bathroom_washer_and_dryer_electrical_energy_tariff_mt
        name: MT
      - color: grey
        entity: binary_sensor.dark_outside
        name: TARIFA
        show_line: false
        y_axis: secondary
    group: false
    hour24: true
    hours_to_show: 24
    icon: 'mdi:counter'
    line_width: 4
    name: PRALNI IN SUŠILNI STROJ
    points_per_hour: 4
    show:
      extrema: true
      fill: fade
      icon: true
      labels: false
      name: true
      state: true
    state_map:
      - label: VT
        value: 'off'
      - label: MT
        value: 'on'
    style: |
      ha-card {
        border: solid 2px var(--primary-color);
      }
    type: 'custom:mini-graph-card'
type: vertical-stack

The majority of the styling is due to my theme(s). This is what it was: Day and Night

I’ve updated it a bit since then. Will post the new config and background images in that topic when I get home tonight.

Thanks.

If I chage names and color I lose multicolor line. How do I chage colors and names without loosing multicolor line?

graf_prej

        entities:
          - entity: sensor.bathroom_washer_and_dryer_rest_1pm_servis_energy_power
          - color: 'rgba(0,0,255,1)'
            entity: binary_sensor.dark_outside
            show_line: false
            y_axis: secondary

graf_potem

    entities:
      - color: 'rgba(0,0,255,1)'
        entity: sensor.bathroom_washer_and_dryer_rest_1pm_servis_energy_power
        name: trenutna moč
      - color: grey
        entity: binary_sensor.dark_outside
        name: ' '
        show_line: false
        y_axis: secondary

Use color_thresholds instead of color.

Colors and names is working now. Thanks :slight_smile:

Can you post this?

I did yesterday. Follow the link.