Search

Shelly EM3 and Home Assistant

Today we are going to install and setup a Shelly EM3, 3 phase energy monitor based on the ESP8266 module. We'll consider a few installation consideration, and setup some template sensors and utility sensors in home assistant to display some useful information on my dashboard in my kitchen. This installation is done under core-2021.3.2 on a supervised Home Assistant OS system.




It can be hard to fit the device and the current sensing clamps inside an electrical load centre like this one here.


There may be room in a meter box, if not then you will need to get a custom enclosure install for it to be mounted in where the clamps can be install around the incoming mains cables. I happen to have the perfect spot but only because I have my main cable cut and stolen during construction, so I guess it worked out sort of ok.


Whichever location you choose you will be working with live and in some cases unprotected mains cables, so please have an electrician install it for you.


Once the device is installed and powered you can connect to the wireless access point that the Shelly EM3 creates, you can also use the Shelly app on you phone although I am not a fan of the app it's a bit slow and not intuitive. Connect the device to your wifi, and look up its local IP address on your network. In my case my router assigned 192.168.1.123, (Your might be in the 10.1.1.1 range or any of the private IP ranges) I then made it a static reservation so that it will remain the same, all routers have different ways of doing this so you will need to look up the information for your router if you have never done that before.


Next on the EM3 web page go to settings and name the device and the 3 channels, that way once the device is configured in Home Assistant the entity's will have meaningful names, this will make setup easier. I have called my device Main Power Monitor and each channel Red Phase , Blue Phase and White Phase. This results in home assistant entities having: sensor.power_red_phase for each phase and sensor.energy_red_phase for each phase, for a total of 6 entities that we will be using today to create some template sensors. If for some reason the name don't come over into home assistant you can change them in home assistant.


To change the entity names, head to configuration devices and find the shelly EM3 the device info panel will show all the entities attached to that device, if you select an entity you can edit the entity ID, the icon and the name. The entity ID it used in the template sensors we will be work on next, the Name is the default name that will display in the GUI if you use that entity directly on your Home Assistant dashboard.


Usually home assistant will automatically find the Shelly device on your network, (you might need to restart home assistant), and in configuration under integrations and box will pop up asking you if you want to install a Shelly Integration. The Shelly integration is now part of the Home Assistant core so it's very easy to setup the energy monitor, in fact you will most likely only have to press 1 button and it's done. If it doesn't show up automatically, in the bottom right corner select add integration and find Shelly and add it that way and enter the local IP address of your device EG. 192.168.1.123 and that will add your device.


If you have not already you will need to turn on advanced mode in home assistant, to do that click on the bottom left corner the circle with initials for your user and you will enter your user profile, one of the options will be to turn on advanced mode.


Under configuration/devices select your energy monitor device and you will see all of the channels putting out information such as voltage, amps, watts and kWh. Here is where we can get the names of the 6 key sensors that we will need, so remember the entity IDs or write them down, the 3 that end in power and the 3 that end in energy. These are the ones that I have:


sensor.red_phase_power , sensor.white_phase_power, sensor.blue_phase_power
sensor.red_phase_energy, sensor.white_phase_energy, sensor.blue_phase_energy

If your's are the exact same you can copy my template sensors exactly, if your sensors are different then you will need to change out the relevant sensor entity id's that are in the templates.

I'm going to use VS code, it is easy to install, under supervisor/add-on store, it is part of the https://addons.community repository if necessary in the top right click on the 3 dots and select repositories and you can add it if you don't have it already. And then start it so you can use it and add it to your sidebar so you can open it. You will also be able to use it later to install Mini-Graph-Card.




Open configuration.yaml and you will need sensor and utility_meter if one or both are there already you can add to what you have other wise add the following to the configuration.yaml and create 2 new files with the plus file circled on the picture, and call them sensor.yaml and utility_meter.yaml


# Add the below to configuration.yaml if you don't already have 
# have it
utility_meter:!include utility_meter.yaml
sensor:!include sensor.yaml

Then open sensor.yaml so we can make 3 x template sensors, to check correct formatting please see GitHub template_sensors and remember to use the entities that are correct for your system.

- platform: template
  sensors:
    energy_total: # This is the entity id sensor.energy_total
      friendly_name: 'Total Energy'
      value_template: "{{ 
        (states('sensor.red_phase_energy')|float + 
        states('sensor.white_phase_energy')|float + 
        states('sensor.blue_phase_energy')|float)|round(3) }}"
      unit_of_measurement: "kWh"
      availability_template: "{{ 
        states('sensor.red_phase_energy') not in 
        ['unknown','unavailable'] and 
        states('sensor.white_phase_energy') not in 
        ['unknown','unavailable'] and 
        states('sensor.blue_phase_energy') not in 
        ['unknown','unavailable'] }}"
    power_total:
      friendly_name: 'Total Power'
      value_template: "{{ (states('sensor.red_phase_power')|float 
        + states('sensor.white_phase_power')|float + 
        states('sensor.blue_phase_power')|float)|round(3) }}"
      unit_of_measurement: "W"
    daily_power_cost:
      friendly_name: 'Daily Power Cost'
      value_template: "{{ (states('sensor.daily_energy')|float * 
        0.262026 + 0.9393)|round(2) }}"

Now we are going to edit utility_meter.yaml and create a daily and bi monthy sensor starting when my bill starts, once again correct format is here utility_sensor



daily_energy:
  source: sensor.energy_total
  cycle: daily
bimonthly_energy:
  source: sensor.energy_total
  cycle: bimonthly
  offset:
    days: 9
    hours: 7
    minutes: 30
  tariffs:
    - peak
    - offpeak

The first daily utility sensor uses the template sensor we created that has an availability template so that the utility meter doesn't glitch out, it only has 1 tariff and will reset every day a midnight. It will in turn make a sensor called sensor.daily_energy . The second is an example of 2 tariffs with an offset all these features could be used with the daily one as well, but I only include it as an example as I only have 1 tariff at my place. This one will create two sensors sensor.bimonthly_energy_peak and sensor.bimonthly_energy_offpeak

For the peak and off peak to work you will also need and automation to switch the tariff times and example of this is found at he Home Assistant web site and the automation would look like this with peak time being 9am - 21pm and off-peak 21pm to 9am:


automation:
  trigger:
    - platform: time       
      at:"09:00:00"
    - platform: time       
      at:"21:00:00"
  action:
    - service: utility_meter.next_tariff       
      target:
        entity_id: utility_meter.bimonthly_energy

This could be entered in configuration/automations via the GUI


Next we are going to display some information that I have found to be most relevant on a HA dashboard in my kitchen. To do that we will need a custom add on called Mini Graph Card (https://github.com/kalkih/mini-graph-card)

There are 3 ways to install it, I used HACS which is the easiest way but you will first need to look up how to install HACS, once you have HACS there are many other integrations and frontend addon you can install for HA.

You can also manually copy the 'mini-graph-card-bundle.js' file to config/www on to your HA setup or even easier use the VScode terminal to copy the file accross to HA make sure your are in config/www folder then run the following command, please note that this command will be outdated with new versions of Mini Graph in the future so please check the github site for install instructions:

www$ wget https://github.com/kalkih/mini-graph-card/releases/download/v0.10.0/mini-graph-card-bundle.js

And then in configuation/lovelace dashboards you need to add the module as a resource and then restart HA

/local/mini-graph-card-bundle.js?v=0.10.0

Once the module is installed we can add the card to the dashboard or into a vertical or horizontal stack that we might have on our dashboard already.


Even though it is a custom card we can search for it in the GUI dashboard editor and select it, once it is selected we will need to add some yaml to setup the card on the dashboard properly, there is plenty of information about option and extras on the github site.


First we will add the configuation to display instantaneous power usage in watts using our combined template sensor with the entity_id sensor.power_total, of course you might have choosen a different name for your entity. Be aware the entity_id is not the friendly name in this example that I using, I used it to highlight that you can spend heaps of time getting the graph to work simple to find out your using the friendly name instead of the actual entity ID, I have done this a number of times in the past myself in different senario's. In the picture below, is a template sensor, I have HIGHLIGHTED what will become the entity ID, because it is not immediately obvious to everyone.

Now the actually YAML for the mini graph card:


type: 'custom:mini-graph-card'
entities:
  - entity: sensor.power_total
    show_points: false
  - entity: binary_sensor.night
    color: grey
    y_axis: secondary
    aggregate_func: min
    show_state: true
    show_line: false
    show_points: false
hours_to_show: 12
icon: 'mdi:gauge'
points_per_hour: 60
state_map:
  - value: 'off'
    label: Day
  - value: 'on'
    label: Night

This graphs total power, you can play with some of the parameters and see what changes or add some new ones from the github docs. There is also a secondary graph and secondary y-axis is is graphing a time of day entity that is either on or off as you see in the state map off is day and on is night, the entity is called binary_sensor.night you could also use this elsewhere in automation that you want to only run at night or day time or as trigger for changes from day to night. Lets setup a TOD - Time of day sensor, you can have more than one with different offsets, this particular sensor changes state exactly at sunset and sunrise, you could make a sensor with offsets that indicates dark and light too if you needed that. You can see that effect on the graph that this TOD has in the screen shot above.

This would be placed in the binary_sensor.yaml file, and in configuation.yaml file it must be included as well binary_sensor: !include binary_sensor.yaml like so.

- platform: tod
  name: Night
  after: sunset
  before: sunrise

These are very useful feature in home assistant.


Finally we will add another card, with a bar graph that spans 7 days to show daily total usage. Remember these cards will not look very good or correct till in this case 7 days has past. The utility meter uses the same domain as sensor, some while you might expect the entity_id to be utility_meter.daily_energy it is not it will start with sensor.

type: 'custom:mini-graph-card'
entities:
   - entity: sensor.daily_energy
name: ENERGY CONSUMPTION
hours_to_show: 168
aggregate_func: max
group_by: date
show:
   graph: bar

The only this that we have not covered but you will see up the top is the cost of energy so far today. My energy provider charges me a supply charge of $0.93 per day to that I have only one tariff to add. Energy is charged in units, 1 unit is 1kWhr or 1000w of energy used consistently for 1 hour. So essential sensor.daily_energy is the number of units we have used so far today so multiply that by the cost per unit and then add the supply charge and we have cost so far today. Which I have simply added to my dashboard using a standard entities card.


Well that's everything we are covering today, I hope you are not too shocked by your power use like I was, I have found it helps to motivate me to turn of appliances and lights, when I can see the power adding up daily and I hope it helps you save power and money too. If you have any further question please leave a comment and I do my best to help out and if you have not already please subscribe to Jimmy James my channel on YouTube.






511 views0 comments

Recent Posts

See All