Shelly 3EM 3-phases Energy sensor

Shelly 3EM has integrated relay output which can be used for contactor control.

However I wouldn’t recommend a contactor for switching EV charging, as the load can be quite large.

A better approach would be to control the charging rate either directly in the car (Tesla Custom Integration - Charging Amps adjust via LoveLace or control an OCPP Server Plugin for linking electric car chargers!

Yes BUT, probably best to control it in the car. Most of not ALL cars have this feature - what vehicle do you have? Manu chargers have the feature too.

OpenEVSE is good for the hacker type.

Staring the charger is OK with the contactor but disconnecting will arc and spark the contractor.

Hey @modernhistorian ,
So this code is working

This is the read-out on devtools, which is what i was hoping for as an outcome

My next question is how do i convert this from a template to an entity?

Ideally, I will be adding this to the energy dashboard to replace the initial 3 phases for hourly snapshots. But, I also seek live data for my dashboard

I am sure this is fairly basic now there is a template. I just don’t understand, can you please help?

Thanks for responsing. I will have BMW i4. It is possible to do it in the car manually, but with HA I could do it automatically based on current price. Did I understood correctly that you suspect it could damage something? Car, charger or something else?

Turning on: Fine.
Turning off: BAD.

Connecting the car will be fine. The charger (in the car) will detect the return of power, the EVSE will do all the GFI tests, etc, and charging will begin as though you plugged the regular handle in to the car, and then plugged the EVSE in to the wall.

Disconnecting the charger power won’t (shouldn’t) damage the car, but the vehicle will experience the equivalent of a power-outage, and that might make it grumpy. but more importantly, the current will probably be cut when it’s flowing at a full 50 amps, and that’s going to make a pretty nasty spark / arc in your contactor. They’re pretty robust devices, but this is abusive.

I’d really consider a different EVSE. If you’re going to be wiring in Shellys and buying contactors, why not just make yourself up an OpenEVSE with the wifi? I have one and it’s been fantastic over the years. Just put a good contactor and a good cord on it and you’re set. You can MQTT all day long. Or, de-brain your current EVSE and put an OpenEVSE in to it. Infinitely hackable and you’re already doing the bulk of the same work with your plan anyways.

I have two-phase service here in the States, so I have a clamp around each phase and the third clamp around one leg of the solar inverter output. That is why channel c is doubled in my yaml, to get the total solar output. Like so:

  - name: "All Energy Total"
    unique_id: all_energy_total
    state: >-
      {{ [ states('sensor.em3_channel_a_energy'), 
           states('sensor.em3_channel_b_energy'),
           states('sensor.em3_channel_c_energy'),
           states('sensor.em3_channel_c_energy'),
         ] | map('float') | sum }}
    unit_of_measurement: kWh
    icon: mdi:counter
    device_class: energy
    state_class: total_increasing
    availability: >-
          {{ [ states('sensor.em3_channel_a_energy'), 
               states('sensor.em3_channel_b_energy'),
               states('sensor.em3_channel_c_energy'),
             ] | map('is_number') | min }}

My figures agree with the utility company to within one percent, so that works. But I think you will want to remove one of the channel c entries in your “all energy total”, to sum each of the three phases. Check your output with figures from your supplier.

I’m not too familiar with three-phase service. Are these the circuits for each phase? I also wonder why two phases are negative and one positive> Do you have solar feeding into phases A and B?

Hello, everyone.
I would like to use the Shelly 3em to optimize the excess feed-in of the photovoltaic system.

So if I have from -600 watts on all 3 phases, I would like the pool to switch on, for example. Unfortunately I only found this automation for 1 phase each. How can I do it for all 3 phases together too?

1 Like

Create an “all power total” sensor as shown above and use this in your automation.

2 Likes

Hello all,

I have a comprehension question of my configuration or it is just a thinking mistake on my part. I have now had the time to look into the Energy Dashboard for a long time - hence the question.
I have summarized all 3 phases of my Shelly as follows:

    - 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: total_increasing
      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) |round(1)) }}
      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') ] }}

I am running a solar panel connected to L2 return and also metered via a Shelly Plug S.
image

Thus, for my understanding, with the above code, I get a sum of the consumptions including the solar feed through L2 as consumption.

However, if I use the Shelly Plus S as a feed, then for my understanding in the Energy Dashboard insofar a wrong value would be displayed, because there graphically the consumption including the solar feed is displayed against the solar energy production.

So how can I tell how much energy was fed back into the grid if I specify the Shelly Plug S as a “return to grid” device?
Basically, it should be a value that gives the negative value (surplus) from the sum of the solar feed and the sum of the consumption?

I would be grateful for any feedback.

A random idea on the moment - define 2 template sensors, one that gets values over 0 and one that gets value under 0 (only negative ones) based on power/energy sensor. That would give you a power out and power in sensor

Thanks for getting back to me.

So if I use “sensor.power_total” which already totals the consumption of L1, L2, L3 with the L2 return (solar feed), then I would only need to use a filter which in case of a negative value, then displays a value in the Energy Panel “back to grid”.

Could you possibly help me with a filter?

Take it with a grain of salt - i’m just learning these things.
If you create a sensor with this state

{{ 
      [ min(states('sensor.energy_meter_channel_a_energy')|float,0), 
        min(states('sensor.energy_meter_channel_b_energy')|float,0),
        min(states('sensor.energy_meter_channel_c_energy')|float,0),
      ] | map('float') | sum
    }}

you will get only 0 or negative values

If you replace min with max you will get only positive values.

1 Like

Thanks for your reply.
I am still waiting for some more sun to get a negative value in the consumption :wink:

My current code is as follows:

- 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: total_increasing
      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) |round(1)) }}
      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') ] }}
    - name: "Return Energy"
      unique_id: return_energy
      state: >-
          {{ 
             [ min(states('sensor.phase_1_energy')|float,0), 
               min(states('sensor.phase_2_energy')|float,0),
               min(states('sensor.phase_3_energy')|float,0),
             ] | map('float') | sum
          }}
      unit_of_measurement: kWh
      device_class: energy
      state_class: total_increasing
      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') ] }}

All 3 phases are summed (Shelly 3M) and displayed as consumption in the Energy Dashboard.

The solar production is fed in via the Shelly Plug S.
Phase L2 return, on which the Shelly Plug S feeds in, I cannot use as “return to grid”, because this reads BEFORE the balancing electricity meter and the feed-in values are therefore incorrect.

Therefore I have to use the sensor “return_energy”. I’ll let you know if it works as soon as the sun shines a bit more again :slight_smile:

1 Like

This helped eliminate my wrong energy returned values in a similar setup. Can you elaborate on the logic more? What does the “min” here do to the values?

Honestly I just try and error to find a code or a “sensor”, that filters me just every value, which is below “0” (zero) to get the returned energy to the grid.
Currently I sum L1, L2 and L3. So with energy_total and power_total I have the consumption which sometimes has a negative value, because of the solar feed. That is, how it should be.

But instead of just using the physical measuring L2 return of the Shelly 3M, I just want to have the negative value of the sum mentioned above. So I could use this as “return to grid” sensor.

In my case, “min” should filter just the negative values, as mentioned above by @quasar66, which unfortunately doesn´t work for me in that code i pasted. :expressionless:

1 Like

Ah, understood. So, does your code above not do what you are looking for? If not, hope somebody can help here. Would be appreciated.

I think this should work:

  - name: "Return Energy"
    unique_id: return_energy
    state: >
      {% set energy_value = (states ('sensor.a_energy')|float(2) + states ('sensor.b_energy')|float(2) + states ('sensor.c_energy')|float(2)) %}
      {% set return_value = 0 - energy_value %}
      {% if return_value > 0 %}
        {{ return_value }}
      {% else %}
        0
      {% endif %}
    unit_of_measurement: kWh
    device_class: energy
    state_class: total_increasing
    attributes:
      last_reset: "1970-01-01T00:00:00+00:00"
    availability: >-
        {{ 
          [ states('sensor.a_energy'), 
            states('sensor.b_energy'),
            states('sensor.c_energy'),
          ] | map('is_number') | min
        }}
1 Like

Hi @teropop,

thanks for your feedback.
Unfortunately, I do not get any values displayed.
You can see the current solar energy output from the Shelly Plug S here:

The current sum consumption is as follows:

The returned energy should be there, but it seems not working:

The current template *.yaml looks like that:

    - 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: total_increasing
      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) |round(1)) }}
      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') ] }}
    - name: "Return Energy"
      unique_id: return_energy
      state: >
            {% set energy_value = (states ('sensor.phase_1_energy')|float(2) + states ('sensor.phase_2_energy')|float(2) + states ('sensor.phase_3_energy')|float(2)) %}
            {% set return_value = 0 - energy_value %}
            {% if return_value > 0 %}
              {{ return_value }}
            {% else %}
              0
            {% endif %}
      unit_of_measurement: kWh
      device_class: energy
      state_class: total_increasing
      attributes:
        last_reset: "1970-01-01T00:00:00+00:00"
      availability: >-
          {{ 
             [ states('sensor.phase_1_energy'), 
               states('sensor.phase_2_energy'),
               states('sensor.phase_3_energy'),
             ] | map('is_number') | min
          }} 

1 Like

Yeah, just got some sun in (higher than all consumed energy), and unfortunately not getting any value either.