Shelly EM Energy Management Setup MQTT

This is my configuration for setting up two Shelly EM’s. One is measuring utility in and out (+/-) and another is measuring solar production.
For the new energy management dashboard you would use the Grid Import/Export and Total Solar Energy

sensor.yaml (fyi i’m not using any of the energy measured by the shelly only the power)

- platform: mqtt
  name: "Utility Power 1"
  state_topic: "shellies/Utility/emeter/0/power"
  qos: 1
  unit_of_measurement: "W"
  icon: mdi:gauge
- platform: mqtt
  name: "Utility Power 2"
  state_topic: "shellies/Utility/emeter/1/power"
  qos: 1
  unit_of_measurement: "W"
  icon: mdi:gauge        
- platform: mqtt
  name: "Utility Voltage"
  state_topic: "shellies/Utility/emeter/1/voltage"
  qos: 1
  unit_of_measurement: "V"
  icon: mdi:gauge   
- platform: mqtt
  name: "Utility Energy 1"
  state_topic: "shellies/Utility/emeter/0/energy"
  qos: 1
  unit_of_measurement: "Wm"
- platform: mqtt
  name: "Utility Energy 2"
  state_topic: "shellies/Utility/emeter/1/energy"
  qos: 1
  unit_of_measurement: "Wm"

- platform: mqtt
  name: "Solar Power 1 Raw"
  state_topic: "shellies/Solar/emeter/0/power"
  qos: 1
  unit_of_measurement: "W"
  icon: mdi:gauge
- platform: mqtt
  name: "Solar Power 2 Raw"
  state_topic: "shellies/Solar/emeter/1/power"
  qos: 1
  unit_of_measurement: "W"
  icon: mdi:gauge        
- platform: mqtt
  name: "Solar Voltage"
  state_topic: "shellies/Solar/emeter/1/voltage"
  qos: 1
  unit_of_measurement: "V"
  icon: mdi:gauge   
- platform: mqtt
  name: "Solar Energy 1"
  state_topic: "shellies/Solar/emeter/0/energy"
  qos: 1
  unit_of_measurement: "Wm"
- platform: mqtt
  name: "Solar Energy 2"
  state_topic: "shellies/Solar/emeter/1/energy"
  qos: 1
  unit_of_measurement: "Wm"


  
- platform: template
  sensors:
    solar_power_1:
      value_template: >
        {{ (states('sensor.solar_power_1_raw')|float - 3) |round(2) }}
      friendly_name: 'Solar Power 1'
      unit_of_measurement: 'W'   
    solar_power_2:
      value_template: >
        {{ (states('sensor.solar_power_2_raw')|float - 3) |round(2) }}
      friendly_name: 'Solar Power 2'
      unit_of_measurement: 'W'       
    consumed_power_1:
      value_template: >
        {{ (states('sensor.solar_power_1')|float 
        + states('sensor.utility_power_1')|float) |round(2) }}
      friendly_name: 'Consumed Power 1'
      unit_of_measurement: 'W'       
    consumed_power_2:
      value_template: >
        {{ (states('sensor.solar_power_2')|float 
        + states('sensor.utility_power_2')|float) |round(2) }}
      friendly_name: 'Consumed Power 2'
      unit_of_measurement: 'W'          
    total_consumed_power:
      value_template: >
        {{ (states('sensor.consumed_power_1')|float 
        + states('sensor.consumed_power_2')|float)| round(2) }}
      friendly_name: 'Total Consumed Power'
      unit_of_measurement: 'W'
    total_solar_power:
      value_template: >
        {{ (states('sensor.solar_power_1')|float 
        + states('sensor.solar_power_2')|float)| round(2) }}
      friendly_name: 'Total Solar Power'
      unit_of_measurement: 'W'
    total_utility_power:
      value_template: >
        {{ (states('sensor.utility_power_1')|float 
        + states('sensor.utility_power_2')|float)| round(2) }}
      friendly_name: 'Total Utility Power'
      unit_of_measurement: 'W'
    total_returned_power:
      value_template: >
        {{ (states('sensor.total_utility_power')|float 
        * (-1)|float)| round(2) }}
      friendly_name: 'Total Returned Power'
      unit_of_measurement: 'W'
      


- platform: integration
  source: sensor.total_utility_power
  name: total_utility_energy
  unit_prefix: k
  unit_time: h
  method: left    

- platform: integration
  source: sensor.total_consumed_power
  name: total_consumed_energy
  unit_prefix: k
  unit_time: h
  method: left    

- platform: integration
  source: sensor.total_returned_power
  name: total_returned_energy
  unit_prefix: k
  unit_time: h
  method: left 
  
- platform: integration
  source: sensor.solar_power
  name: total_solar_energy
  unit_prefix: k
  unit_time: h
  method: left  
  
- platform: integration
  name: Grid Import Energy
  source: sensor.grid_import_power
  unit_prefix: k
  unit_time: h
  method: left
    
- platform: integration
  name: Grid Export Energy
  source: sensor.grid_export_power
  unit_prefix: k
  unit_time: h
  method: left

template.yaml

  - sensor:
        name: Grid Import Power
        state_class: measurement
        icon: mdi:transmission-tower
        unit_of_measurement: W
        device_class: power
        state: >
            {{ [0, states('sensor.total_consumed_power') | int - states('sensor.total_solar_power') | int ] | max }}
  - sensor:
        name: Grid Export Power
        state_class: measurement
        icon: mdi:transmission-tower
        unit_of_measurement: W
        device_class: power
        state: >
            {{ [0, states('sensor.total_solar_power') | int - states('sensor.total_consumed_power') | int ] | max }}            

  - sensor:
        name: Solar Power
        state_class: measurement
        icon: mdi:transmission-tower
        unit_of_measurement: W
        device_class: power
        state: >
            {{ [0, states('sensor.total_solar_power') | int ] | max }}                  

And some utility meters for fun
utiliy_meter.yaml

  utility_yearly_energy:
    source: sensor.total_utility_energy
    cycle: yearly
    tariffs: 
      - peak
    net_consumption: false


  utility_monthly_energy:
    source: sensor.total_utility_energy
    cycle: monthly
    net_consumption: false
    tariffs: 
      - peak
    
  utility_daily_energy:
    source: sensor.total_utility_energy
    cycle: daily  
    net_consumption: false
    tariffs: 
      - peak
    
  solar_daily_energy:
    source: sensor.total_solar_energy
    cycle: daily  
    net_consumption: false
    tariffs: 
      - peak
    
  solar_monthly_energy:
    source: sensor.total_solar_energy
    cycle: monthly  
    net_consumption: false
    tariffs: 
      - peak    

  solar_yearly_energy:
    source: sensor.total_solar_energy
    cycle: yearly  
    net_consumption: false
    tariffs: 
      - peak
      
  consumed_daily_energy:
    source: sensor.total_consumed_energy
    cycle: daily  
    net_consumption: false
    tariffs: 
      - peak    
      
  consumed_monthly_energy:
    source: sensor.total_consumed_energy
    cycle: monthly  
    net_consumption: false
    tariffs: 
      - peak    
      
  consumed_yearly_energy:
    source: sensor.total_consumed_energy
    cycle: yearly  
    net_consumption: false
    tariffs: 
      - peak    
 
  returned_daily_energy:
    source: sensor.total_returned_energy
    cycle: daily  
    net_consumption: false
    tariffs: 
      - peak    
      
  returned_monthly_energy:
    source: sensor.total_returned_energy
    cycle: monthly  
    net_consumption: false
    tariffs: 
      - peak    
      
  retured_yearly_energy:
    source: sensor.total_returned_energy
    cycle: yearly  
    net_consumption: false
    tariffs: 
      - peak    
            
1 Like

I switched from mqtt to the core integration recently. The only noticeable difference is that I don’t need any configuration.

That’s using the Shelly integration? Guess maybe I should give that a go.

Yep. I held off and used mqtt for ages. The Shelly integration seems quite mature now. I have no issues with it at all.

I’m researching the Shelly EM to monitor production/consumption in real-time.

Do you know if you can configure Shelly EM to send mqtt updates every second? I also see you changed to core integration recently - can that be configured for 1 second updates?

I’m also a bit unsure why you need two Shelly EMs - do you have two phase supply for utility?

Thanks

OP probably lives in the US with 240V split phase which requires 2 clamps since you need to measure each leg.

I just ordered a Shelly EM with 2x 120A clamps (I live in the US with 240 split phase). Shelly sent me a survey followed by a 15% off code for answering the survey. I had been considering the Shelly EM for months, and with the new Energy integration and the 15% of discount code, I decided it was time to purchase.

Can anyone speak to how accurate the Shelly EM is for split phase? Normally the best practice is to draw power from the same phase you are using a clamp to measure because W = V*A and you can only accurately measure V on the leg powering the Shelly EM. I am wondering if I will need/want a second Shelly EM that I can power from the second leg and use to measure the second leg.

1 Like

Sounds like you need the Shelly 3EM which allows 3 Phase measurement with each phase having a voltage input. You don’t need to use all 3 phases - it would work on split phases as well. Shelly 3EM | Shelly Cloud

I don’t think the shelly has any ability to change the update speed but it seems to be almost instant or at least every second. I’ve been very happy them.
I have two so that I can measure what is coming in from my solar panels and what is coming in or going out to the utility. My solar panels do have an api but it needs a cloud connection where as the shelly doesn’t.

I can’t see the difference between the 120V legs being anything significant.
You could also wire the shelly to 240V.

It is pretty normal for the voltage in US split phase to vary between ~115 and ~124 volts, and generally leg 1 and leg 2 won’t be exactly the same due to things such as voltage sag on one phase from your neighbors’ usage. Let’s say that you have 118V on leg 1 and 122V on leg 2. The Shelly is powered by leg 1 and detects 50A draw on the leg 2 clamp. The calculated W usage will be wrong by 200W. Over time, this can lead to usage numbers that are off significantly. Unfortunately, powering the Shelly EM on 240 from split phase won’t help. First, it won’t be able to accurately know the Watt usage on either leg. Second, the usage will be off by a factor of 2 because it will calculate W as 240V * amp.

1 Like

Maybe it’s a US thing? I wouldn’t think Canada would be too different. I’ve got a seperate voltage monitor using a UPS and both legs are within 0.5V

It depends on how reliable and clean the power in your area/neighborhood is. Certainly some areas will be much better than others as far as voltage difference/sag between phases.

Great suggestion, I didn’t even think of using the Shelly 3EM with US split phase, but they even have a wiring diagram for how to do that: https://shelly.cloud/knowledge-base/devices/shelly-3em/#wiring

Is the measurement of active power right on both legs? I suppose it’s ok because you have 2 phases 180 degrees between them. Are your power factors higher than 0.8 on both legs?

Here, the power is split phase 120 degrees, only the leg that’s directly connected to Shelly return the right values. The other clamp brings wrong power factor and wrong active power, as the voltage reference is 120 degrees from the measured one.

Guys, I live in EU, I have 3 phase here, but also my solar invertor is 3 phase… Does it mean I need 2 x Shelly 3 EM ? So in total 6 clamps, probably?

I haven’t bought it yet, but how do you setup those 2 shellys then so you can set them up as production/consumption ?? Do I need some pairing? Is it possible to display both graphs as overlay ? Or how does it work?

I had before a smappee, with 6 clamps, and I saw easily production/consumption in the graphs

i just stumbled on this one. great write up as i’m wanting to switch from the integration to MQTT since the integration tends to drop regularly. i noticed you’re not really using the shelly energy readings, which is interesting because that’s the route i’m going now; it seems the shelly mutt energy readings are “off”. i’m going to follow your path. thanks