Shelly 3EM 3-phases Energy sensor

Sorry for distrubing again.
The current message, after making that change, is:

"Logger: homeassistant.components.template.template_entity
Source: components/template/template_entity.py:73
Integration: Template (documentation, issues)
First occurred: 13:45:09 (1 occurrences)
Last logged: 13:45:09

TemplateError(‘ZeroDivisionError: float division by zero’) while processing template ‘Template("{{ (((states(‘sensor.phase_1_power’)|float) * (states(‘sensor.phase_1_power_factor’)|float) + (states(‘sensor.phase_2_power’)|float) * (states(‘sensor.phase_2_power_factor’)|float) + (states(‘sensor.phase_3_power’)|float) * (states(‘sensor.phase_3_power_factor’)|float)) / (states(‘sensor.phase_1_power’)|float + states(‘sensor.phase_2_power’)|float + states(‘sensor.phase_3_power’)|float)) |int }}")’ for attribute ‘_attr_native_value’ in entity ‘sensor.power_factor_total’"

@Chickenbreast, you are probably add the “availability:” property as described in this reply.

Hi there,

Not looking to threadjack – but does anyone know where the ‘voltage’ attribute went?

I have had a 3EM since May and some of the sensors I created were voltage for each phase (per below). They working perfectly until in the last week the voltage attribute disappeared (don’t see in developer tools anymore either). These voltages are necessary inputs to automations I have that shut down pumps (i.e. for wells, irrigation etc) if phase imbalance gets above 10% or one of the phases drops (we’re in the boonies here and all sorts of weird stuff happens to power that ends up costing us new pumps more often than we would like…). Anyone noticed this, or perhaps happened as a firmware update recently? Any suggestions on getting it back? Worth switching to MQTT? Does that kill em3 availability in the shelly app? – now where it really matters to me but curious to know.

Thanks,

    - platform: template
      sensors:
        phase_a_voltage:
          value_template: "{{ state_attr('sensor.shelly_shem_3_d8bfc01a7b45_1_current_consumption', 'voltage') }}"
          friendly_name: "Phase A Voltage"
        phase_b_voltage:
          value_template: "{{ state_attr('sensor.shelly_shem_3_d8bfc01a7b45_2_current_consumption', 'voltage') }}"
          friendly_name: "Phase B Voltage"
        phase_c_voltage:
          value_template: "{{ state_attr('sensor.shelly_shem_3_d8bfc01a7b45_3_current_consumption', 'voltage') }}"
          friendly_name: "Phase C Voltage"

**fyi I think this is answer to above question btw

        total_power:
          value_template: >
            {{ ( (states('sensor.shelly_shem_3_d8bfc01a7b45_1_current_consumption') | int) + (states('sensor.shelly_shem_3_d8bfc01a7b45_2_current_consumption') | int)  + (states('sensor.shelly_shem_3_d8bfc01a7b45_3_current_consumption') | int) ) }}
          friendly_name: "Borgo Total Power"
          unit_of_measurement: "W"

rest of my 3em setup

        phase_imbalance_ab:
          value_template: >
            {{ ((((state_attr('sensor.shelly_shem_3_d8bfc01a7b45_1_current_consumption', 'voltage') | replace(" V", "")) | int) - ((state_attr('sensor.shelly_shem_3_d8bfc01a7b45_2_current_consumption', 'voltage') | replace(" V", "")) | int)) | abs) }}
          friendly_name: "Phase Imbalance A/B"
        phase_imbalance_ac:
          value_template: >
            {{ ((((state_attr('sensor.shelly_shem_3_d8bfc01a7b45_1_current_consumption', 'voltage') | replace(" V", "")) | int) - ((state_attr('sensor.shelly_shem_3_d8bfc01a7b45_3_current_consumption', 'voltage') | replace(" V", "")) | int)) | abs) }}
          friendly_name: "Phase Imbalance A/C"
        phase_imbalance_bc:
          value_template: >
            {{ ((((state_attr('sensor.shelly_shem_3_d8bfc01a7b45_2_current_consumption', 'voltage') | replace(" V", "")) | int) - ((state_attr('sensor.shelly_shem_3_d8bfc01a7b45_3_current_consumption', 'voltage') | replace(" V", "")) | int)) | abs) }}
          friendly_name: "Phase Imbalance B/C"

I believe voltage is now reported as separate entity state. In my case it looks like that:

I don’t have the "total_increasing available as state_class (only measurement). If I try to add it in config.yaml it also throws an error. I have 2021.09.

Any idea what could be the issue?

@amitfin: Thanks for your feedback.
In my case for “energy_total” the “measurement” suits better than “total_increasing”.
Since 3 days I didn´t get any log regarding warning or failure. Therefore I want to share it with you guys.

My current settings are:

- sensor:
    - name: "Energy Total"
      unique_id: energy_total
      state: >-
        {{ states('sensor.phase_1_energy')|float(0) + 
           states('sensor.phase_2_energy')|float(0) + 
           states('sensor.phase_3_energy')|float(0) }}
      unit_of_measurement: kWh
      device_class: energy
      state_class: measurement
      attributes:
        last_reset: "1970-01-01T00:00:00+00:00"
      availability: >
          {{ not 'unavailable' in 
             [ states('sensor.phase_1_energy'), 
               states('sensor.phase_2_energy'),
               states('sensor.phase_3_energy') ] }}
    - name: "Power Total"
      unique_id: power_total
      state: >-
        {{ (states('sensor.phase_1_power')|float(0) + 
            states('sensor.phase_2_power')|float(0) + 
            states('sensor.phase_3_power')|float(0)) }}
      unit_of_measurement: W
      device_class: power
      state_class: measurement
      availability: >
          {{ not 'unavailable' in 
             [ states('sensor.phase_1_power'), 
               states('sensor.phase_2_power'),
               states('sensor.phase_3_power') ] }}
1 Like

Can you paste your *.yaml code for that here?

at the end i solved like this:

- sensor:
  - name: "House Energy kWh"
    unique_id: sensor.house_energy_kwh
    unit_of_measurement: "kWh"
    device_class: energy
    state_class: total_increasing
    state: >
      {% set val1 = states('sensor.shelly_3em_meter_total_0') %}
      {% set val2 = states('sensor.shelly_3em_meter_total_1') %}
      {% set val3 = states('sensor.shelly_3em_meter_total_2') %}
      {{ val1 | float(0) + val2 | float(0) + val3 | float(0) 
        if 
          is_number(val1) and is_number(val2) and is_number(val3)
        else 
          None
      }}
    availability: >- 
      {{ 
        is_number(states('sensor.shelly_3em_meter_total_0')) and 
        is_number(states('sensor.shelly_3em_meter_total_1')) and
        is_number(states('sensor.shelly_3em_meter_total_2'))
      }}

deleted out of embarrassment…

Thank you for getting back to me!" This may have been an issue with my HA as I had to restore a backup … I’ve now managed to set the proper state_class and will be monitoring if everything will work OK :slight_smile:

1 Like

hello, I try to give my calculation a positive value with “abs”. everything seems fine, apart from the value being negative. Any Idea why?

      - name: "Haus"
        unique_id: total-auto
        unit_of_measurement: kWh
        device_class: energy
        state_class: total_increasing
        state: >-
          {{ (states('sensor.ebl_stromzahler_total_energy')|float - 
              states('sensor.shelly_shem_3_ecfabcc7ed90_1_total_consumption')|float - 
              states('sensor.shelly_shem_3_ecfabcc7ed90_2_total_consumption')|float - 
              states('sensor.shelly_shem_3_ecfabcc7ed90_3_total_consumption')|float) | abs }}
        attributes:
          last_reset: '1970-01-01T00:00:00+00:00'
        availability: >
          {{ not 'unavailable' in 
            [ states('sensor.ebl_stromzahler_total_energy'), 
              states('sensor.shelly_shem_3_ecfabcc7ed90_1_total_consumption'),
              states('sensor.shelly_shem_3_ecfabcc7ed90_2_total_consumption'),
              states('sensor.shelly_shem_3_ecfabcc7ed90_3_total_consumption') ] }} 

My sensors look like this, but as soon as I add
state_class: measurment to either the total sensor or power sensor I get configuration error. state_class: total:_increasing is not allowed says Visual Code…?
Any suggestions please.

# bergvärme total energy
    bv_total_energy:
      friendly_name: 'BV total energy'
      unique_id: '57831d02-53cb-497e-be9e-624b8d42230e'
      unit_of_measurement: kWh
      device_class: energy
      value_template: "{{(
        (
          states('sensor.bv_energy1') | float + 
          states('sensor.bv_energy2') | float + 
          states('sensor.bv_energy3') | float 
        ) | round(2)
      )}}"
      availability_template: >
        {{ states('sensor.bv_energy1') not in ['unknown', 'unavailable', 'none'] and 
          states('sensor.bv_energy2') not in ['unknown', 'unavailable', 'none'] and 
          states('sensor.bv_energy3') not in ['unknown', 'unavailable', 'none'] }}
# bergvärme total power
    bv_total_power:
      friendly_name: 'BV total power'
      unique_id: '36eedbec-c0f3-4920-88a7-003c0fb543b1'
      unit_of_measurement: W
      device_class: power
      value_template: "{{(
        (
          states('sensor.bv_power1') | float + 
          states('sensor.bv_power2') | float + 
          states('sensor.bv_power3') | float 
        ) | round(0)
      )}}"
      availability_template: >
        {{ states('sensor.bv_power1') not in ['unknown', 'unavailable', 'none'] and 
          states('sensor.bv_power2') not in ['unknown', 'unavailable', 'none'] and 
          states('sensor.bv_power3') not in ['unknown', 'unavailable', 'none'] }}
1 Like

total_increasing without the “:” you currently have written inside.

Ha ha, such a stupid mistake :grin:. But id didn’t help:

Invalid config for [sensor.template]: [state_class] is an invalid option for [sensor.template]. Check: sensor.template->sensors->bv_total_energy->state_class. (See ?, line ?).

I’ll put it in the customize.yaml instead.

The is_number approach to availability is pretty neat!

And thanks for others as well in this topic. I was quite stuck with the false readings and couldn’t find out what was the problem with my calculated sensor.

Anyways, my code is as follows now:

  - platform: template
    sensors:
      total_main_energy:
        friendly_name: 'Total Main Energy'
        device_class: energy
        unit_of_measurement: "kWh"
        entity_id:
          - sensor.phase_1_energy
          - sensor.phase_2_energy
          - sensor.phase_3_energy
        value_template: "{{ (states('sensor.phase_1_energy')|float +
                             states('sensor.phase_2_energy')|float +
                             states('sensor.phase_3_energy')|float )|round(3) }}"
        availability_template: >-
          {{
            is_number(states('sensor.phase_1_energy')) and
            is_number(states('sensor.phase_2_energy')) and
            is_number(states('sensor.phase_2_energy'))
          }}


  - platform: template
    sensors:
      total_main_energy_returned:
        friendly_name: 'Total Main Energy Returned'
        device_class: energy
        unit_of_measurement: "kWh"
        entity_id:
          - sensor.phase_1_energy_returned
          - sensor.phase_2_energy_returned
          - sensor.phase_3_energy_returned
        value_template: "{{ (states('sensor.phase_1_energy_returned')|float +
                             states('sensor.phase_2_energy_returned')|float +
                             states('sensor.phase_3_energy_returned')|float)|round(3) }}"
        availability_template: >-
          {{
            is_number(states('sensor.phase_1_energy_returned')) and
            is_number(states('sensor.phase_2_energy_returned')) and
            is_number(states('sensor.phase_2_energy_returned'))
          }}

But now I have a question, how to correct the data on my energy dashboard?

I would be ok if i can just delete those few “bad” days or at least the peaks.

3 Likes

I would like to know that as well, how to delete just a few bad days/readings.

What the benefit with the “is_number”?
My sensor looks like this:

    hd_total_energy:
      friendly_name: 'HD total energy'
      unique_id: '47962c0d-e871-465f-ab89-b3810d399416'
      unit_of_measurement: 'kWh'
      device_class: energy
      value_template: "{{(
        (
          states('sensor.hd_energy1') | float + 
          states('sensor.hd_energy2') | float + 
          states('sensor.hd_energy3') | float 
        ) | round(2)
      )}}"
      availability_template: >
        {{ states('sensor.hd_energy1') not in ['unknown', 'unavailable', 'none'] and 
          states('sensor.hd_energy2') not in ['unknown', 'unavailable', 'none'] and 
          states('sensor.hd_energy3') not in ['unknown', 'unavailable', 'none'] }}

I found a way to reset/delete faulty values with success! Have a look here.
https://community.home-assistant.io/t/energy-management-in-home-assistant/326854/455?u=thelordvader

Just shorter code, nothig else. “not in [‘unknown’, ‘unavailable’, ‘none’]” vs is_number

1 Like

I have a docker installation of HA, so need to find out how to install SQLite Web…

EDIT: It was pretty easy. Just add sqliteweb to docker compose yaml and voila.

  sqliteweb:
    image: tomdesinto/sqliteweb
    ports:
      - 8089:8080
    volumes:
    - ${USERDIR}/docker/homeassistant/home-assistant_v2.db:/db/home-assistant_v2.db
    command: home-assistant_v2.db    
1 Like

Thanks for the is_number recommendation!
I’ve updated the original post at the top of this thread to include it.
Here is how the sensor looks now (IMHO, quite clean):

template:
  - sensor:
      - name: "Energy Total"
        unique_id: energy_total
        state: >-
          {{ 
            [ states('sensor.phase_a_energy'), 
              states('sensor.phase_b_energy'),
              states('sensor.phase_c_energy'),
            ] | map('float') | sum
          }}
        availability: >-
          {{ 
            [ states('sensor.phase_a_energy'), 
              states('sensor.phase_b_energy'),
              states('sensor.phase_c_energy'),
            ] | map('is_number') | min
          }}
        unit_of_measurement: kWh
        device_class: energy
        state_class: total_increasing

(Note: the “min” filter returns False if one the elements in the list is False. If all elements are True, it returns True.)

3 Likes