{"id":481,"date":"2023-01-11T19:39:07","date_gmt":"2023-01-11T18:39:07","guid":{"rendered":"https:\/\/robinpeeten.com\/?p=481"},"modified":"2025-05-26T19:26:41","modified_gmt":"2025-05-26T19:26:41","slug":"shelly-power-plug-s-with-esphome","status":"publish","type":"post","link":"https:\/\/labparvum.com\/index.php\/2023\/01\/11\/shelly-power-plug-s-with-esphome\/","title":{"rendered":"Shelly power plug S with esphome"},"content":{"rendered":"\n<p>To monitor the power usage of many of my devices I started looking into smart plugs and came across the &#8220;Shelly plug S&#8221;<\/p>\n\n\n\n<p>advantages of this model:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>cheap (\u20ac20 a piece at the time of writing)<\/li>\n\n\n\n<li>ESP8266 used allows for reflashing with your own code<\/li>\n\n\n\n<li>power monitoring and switchable<\/li>\n<\/ul>\n\n\n\n<p>disadvantages:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>only 10A rated<\/li>\n\n\n\n<li>Accuracy is quite bad, even after calibration the values jump around<\/li>\n\n\n\n<li>low update speed to accurately measure low power (explained why below)<\/li>\n\n\n\n<li>wifi instead of Zwave or Zigbee (been stable for me but this depends on your wifi network)<\/li>\n\n\n\n<li>Wifi antenna is not great but not terrible<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">How to program?<\/h2>\n\n\n\n<p>Opening it is easy, there is a screw hidden inside the ground plug, unscrew this and the front pops loose.<br>Be aware there is a small thermal pad that can fall out.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"768\" src=\"https:\/\/labparvum.com\/wp-content\/uploads\/2023\/01\/IMG20230111182041-scaled-1-1024x768.jpg\" alt=\"\" class=\"wp-image-483\" style=\"aspect-ratio:1;width:880px;height:auto\" srcset=\"https:\/\/labparvum.com\/wp-content\/uploads\/2023\/01\/IMG20230111182041-scaled-1-1024x768.jpg 1024w, https:\/\/labparvum.com\/wp-content\/uploads\/2023\/01\/IMG20230111182041-scaled-1-300x225.jpg 300w, https:\/\/labparvum.com\/wp-content\/uploads\/2023\/01\/IMG20230111182041-scaled-1-768x576.jpg 768w, https:\/\/labparvum.com\/wp-content\/uploads\/2023\/01\/IMG20230111182041-scaled-1-1536x1152.jpg 1536w, https:\/\/labparvum.com\/wp-content\/uploads\/2023\/01\/IMG20230111182041-scaled-1-2048x1536.jpg 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/labparvum.com\/wp-content\/uploads\/2023\/01\/74b3dbfdbdbc0639c1a4a6cae896e889f8c84d35-1024x576.jpeg\" alt=\"\" class=\"wp-image-484\" srcset=\"https:\/\/labparvum.com\/wp-content\/uploads\/2023\/01\/74b3dbfdbdbc0639c1a4a6cae896e889f8c84d35-1024x576.jpeg 1024w, https:\/\/labparvum.com\/wp-content\/uploads\/2023\/01\/74b3dbfdbdbc0639c1a4a6cae896e889f8c84d35-300x169.jpeg 300w, https:\/\/labparvum.com\/wp-content\/uploads\/2023\/01\/74b3dbfdbdbc0639c1a4a6cae896e889f8c84d35-768x432.jpeg 768w, https:\/\/labparvum.com\/wp-content\/uploads\/2023\/01\/74b3dbfdbdbc0639c1a4a6cae896e889f8c84d35-1536x864.jpeg 1536w, https:\/\/labparvum.com\/wp-content\/uploads\/2023\/01\/74b3dbfdbdbc0639c1a4a6cae896e889f8c84d35.jpeg 1600w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-text-align-left\">I got the pinout from <a rel=\"noreferrer noopener\" href=\"https:\/\/community.home-assistant.io\/t\/solved-flashing-shelly-plug-s-with-usb-pinout\/474729\" target=\"_blank\">this<\/a> post on the HA forum.<br>I was able to flash the ESP by pushing Dupont wires which are also used with breadboards through the holes and keeping a bit of sideways pressure while flashing.<br>But because that method would always work well I switched over to using pogo pins with a header soldered on.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"717\" src=\"https:\/\/labparvum.com\/wp-content\/uploads\/2023\/06\/image-11-1024x717.png\" alt=\"\" class=\"wp-image-531\" style=\"width:512px;height:359px\" srcset=\"https:\/\/labparvum.com\/wp-content\/uploads\/2023\/06\/image-11-1024x717.png 1024w, https:\/\/labparvum.com\/wp-content\/uploads\/2023\/06\/image-11-300x210.png 300w, https:\/\/labparvum.com\/wp-content\/uploads\/2023\/06\/image-11-768x538.png 768w, https:\/\/labparvum.com\/wp-content\/uploads\/2023\/06\/image-11.png 1271w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">The hardware<\/h2>\n\n\n\n<p>The power plug uses the eps8266 as its main IC, and because there is a header that exposes the pins needed for programming, reprograming it is really easy.<\/p>\n\n\n\n<p>The antenna is a flex pcb with a trace, this does work but does not offer the best connection, I have noticed it is slightly worse than a normal pcb antena as some esp dev boards use.<\/p>\n\n\n\n<p>The HLW8012 IC is used for the energy monitoring, this IC can measure active power,voltage and current but does not calculate energy used, this has to be done in software.<\/p>\n\n\n\n<p>The active power value is sent to the esp via a variable frequency from the HLW8012, so when the measured active power is low the output frequency is low as well. 12w is already only 1HZ, so if we would want to measure 1.2w it would take us 10 seconds to know that..<br>Because of that the update speed of the measured power can get really slow when trying to measure low power devices.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"700\" height=\"325\" src=\"https:\/\/labparvum.com\/wp-content\/uploads\/2023\/07\/image-12.png\" alt=\"\" class=\"wp-image-566\" srcset=\"https:\/\/labparvum.com\/wp-content\/uploads\/2023\/07\/image-12.png 700w, https:\/\/labparvum.com\/wp-content\/uploads\/2023\/07\/image-12-300x139.png 300w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"991\" height=\"524\" src=\"https:\/\/labparvum.com\/wp-content\/uploads\/2023\/07\/image-11.png\" alt=\"\" class=\"wp-image-565\" srcset=\"https:\/\/labparvum.com\/wp-content\/uploads\/2023\/07\/image-11.png 991w, https:\/\/labparvum.com\/wp-content\/uploads\/2023\/07\/image-11-300x159.png 300w, https:\/\/labparvum.com\/wp-content\/uploads\/2023\/07\/image-11-768x406.png 768w\" sizes=\"auto, (max-width: 991px) 100vw, 991px\" \/><\/figure>\n\n\n\n<p>The current sense resistor used in the shelly plug is 1m\u03a9 with an accuracy of 10-20%, if you want to be somewhat accurate I should suggest calibrating the plug before putting them to use.<\/p>\n\n\n\n<p>The internal temperature is monitored with a NTC, this is really important for the safety of the device. when drawing large loads for longer duration the temperature can get dangerously high. I have experienced overtemperature when drawing a load of 2000W for 15 min.<br>The 2300W power rating of the plug cannot be sustained for long..<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The software<\/h2>\n\n\n\n<p>Most of my code is based on the config found on the esphome devices forum, I made some adjustments to make it safer and adjust it to my liking.<\/p>\n\n\n\n<p>One important change I made was adding an overcurrent protection before the esp devices forum had some code that did not have that present. When a large load is connected with a bad power factor the current can go well above the rated 10A while staying below the 2300w limit. The temperature protection might not trip fast enough if the overcurrent is large enough.<\/p>\n\n\n\n<p>Each shelly plug has their own config that contains the name and some config and calibration data.<\/p>\n\n\n\n<pre class=\"wp-block-code has-red-color has-text-color has-link-color has-extra-small-font-size wp-elements-7f3ade8f44b687830fe3f507b4b43003\"><code>substitutions:\n  devicename: shelly-ps-01\n  channel_1: Relay\n\n  max_power: \"2200\"\n\n  # Higher value gives lower watt readout\n  current_res: \"0.001100\"\n  # Lower value gives lower voltage readout\n  voltage_div: \"1911\"\n  # Power does not seem to calculated with the calibrated voltage and current vallues, Power needs its own callibration\n  Power_calibration_multiplier: \"1.03888\"\n\npackages:\n  base-config: !include x-shelly-ps-00-base-config.yaml\n  debug-info: !include x-debug-info.yaml<\/code><\/pre>\n\n\n\n<p>This is the base config for the plugs containing all the sensor configuration.<\/p>\n\n\n\n<pre class=\"wp-block-code has-red-color has-text-color has-link-color has-extra-small-font-size wp-elements-1c92f02218ceec6c6f4c0c6b8c9060a7\"><code>substitutions:\n  #filter settings\n  window_size: \"3\"\n  send_every: \"1\"\n  send_first_at: \"1\"\n\n  max_current: \"10\"\n  max_temp: \"70.0\"\n\n  update_interval: \"3s\" #update interval in seconds # low update interval will cause inacurate results, measurements are based on pulses # https:\/\/tinkerman.cat\/post\/hlw8012-ic-new-sonoff-pow\n  update_interval_slow: \"5s\" #update interval in seconds\n\nesphome:\n  name: \"${devicename}\"\n\nesp8266:\n  board: esp01_1m\n\nlogger:\n\napi:\n  encryption:\n    key: \"ThPfR6f+epdPwN0bx63o4+yEfejlq4DP8juJfaNr0jE=\"\n\nota:\n  password: \"297e7ca2c43a39cf7c8cb62c808797b2\"\n\nwifi:\n  networks:\n  - ssid: !secret wifi_ssid\n    password: !secret wifi_password\n    #manual_ip:\n    #  static_ip: \"${static_ip}\"\n    #  gateway: 10.10.1.1\n    #  subnet: 255.255.255.0\n    #use_address: \"${use_ip}\"\n  reboot_timeout: 120min\n\ntime:\n  - platform: sntp\n    id: my_time\n\nbinary_sensor:\n  - platform: gpio\n    pin:\n      number: GPIO13\n      inverted: True\n    name: \"${devicename}_button\"\n    on_press:\n      - switch.toggle: relay\n\n#red led for status\nstatus_led:\n  pin:\n    number: GPIO00\n    inverted: True\n\n#blue light to indicate power\noutput:\n  - platform: gpio\n    pin: GPIO02\n    inverted: true\n    id: led\n\n#relay\nswitch:\n  - platform: gpio\n    pin: GPIO15\n    id: relay\n    restore_mode: RESTORE_DEFAULT_ON\n    name: \"${channel_1}\"\n    on_turn_on:\n      - output.turn_on: led\n    on_turn_off:\n      - output.turn_off: led\n\nsensor:\n  # NTC Temperature\n  - platform: ntc\n    sensor: temp_resistance_reading\n    name: ${devicename} temperature\n    unit_of_measurement: \"\u00b0C\"\n    accuracy_decimals: 1\n    icon: \"mdi:thermometer\"\n    filters:\n       - median:\n          window_size: ${window_size}\n          send_every: ${send_every}\n          send_first_at: ${send_first_at}\n    calibration:\n      b_constant: 3350\n      reference_resistance: 10kOhm\n      reference_temperature: 298.15K\n    on_value_range:\n      - above: ${max_temp}\n        then:\n          - switch.turn_off: relay\n          - homeassistant.service:\n              service: persistent_notification.create\n              data:\n                title: Message from ${devicename}\n              data_template:\n                message: Switch turned off because temperature exceeded ${max_temp}\u00b0C\n  - platform: resistance\n    id: temp_resistance_reading\n    sensor: temp_analog_reading\n    configuration: DOWNSTREAM\n    resistor: 32kOhm\n  - platform: adc\n    id: temp_analog_reading\n    pin: A0\n    update_interval: ${update_interval_slow}\n\n  - platform: hlw8012\n    model: BL0937\n    sel_pin:\n      number: GPIO12\n      inverted: true\n    cf_pin: GPIO05\n    cf1_pin: GPIO14\n    current_resistor: ${current_res}\n    voltage_divider: ${voltage_div}\n    current:\n      name: \"${channel_1} current\"\n      unit_of_measurement: \"A\"\n      accuracy_decimals: 1\n      icon: mdi:flash-outline\n      filters:\n       - median:\n          window_size: ${window_size}\n          send_every: ${send_every}\n          send_first_at: ${send_first_at}\n      on_value_range:\n        - above: ${max_current}\n          then:\n            - switch.turn_off: relay\n            - homeassistant.service:\n                service: persistent_notification.create\n                data:\n                  title: Message from ${devicename}\n                data_template:\n                  message: Switch turned off because current exceeded ${max_current}A\n    voltage:\n      name: \"${channel_1} voltage\"\n      unit_of_measurement: \"V\"\n      icon: mdi:flash-outline\n      accuracy_decimals: 0\n      filters:\n       - median:\n          window_size: ${window_size}\n          send_every: ${send_every}\n          send_first_at: ${send_first_at}\n    power:\n      name: \"${channel_1} power\"\n      id: power\n      unit_of_measurement: \"W\"\n      accuracy_decimals: 0\n      filters:\n       - multiply: ${Power_calibration_multiplier}\n       - median:\n          window_size: ${window_size}\n          send_every: ${send_every}\n          send_first_at: ${send_first_at}\n      icon: mdi:flash-outline\n      on_value_range:\n        - above: ${max_power}\n          then:\n            - switch.turn_off: relay\n            - homeassistant.service:\n                service: persistent_notification.create\n                data:\n                  title: Message from ${devicename}\n                data_template:\n                  message: Switch turned off because power exceeded ${max_power}W\n    change_mode_every: \"2\"\n    update_interval: ${update_interval}\n\n  - platform: total_daily_energy\n    name: \"${channel_1} daily energy\"\n    power_id: power\n    filters:\n      # Multiplication factor from W to kW is 0.001\n      - multiply: 0.001\n    unit_of_measurement: kWh<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>To monitor the power usage of many of my devices I started looking into smart plugs and came across the &#8220;Shelly plug S&#8221; advantages of this model: disadvantages: How to program? Opening it is easy, there is a screw hidden inside the ground plug, unscrew this and the front pops<\/p>\n<p><a href=\"https:\/\/labparvum.com\/index.php\/2023\/01\/11\/shelly-power-plug-s-with-esphome\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\">Shelly power plug S with esphome<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":484,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,5],"tags":[],"class_list":["post-481","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-electronics","category-esphome"],"_links":{"self":[{"href":"https:\/\/labparvum.com\/index.php\/wp-json\/wp\/v2\/posts\/481","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/labparvum.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/labparvum.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/labparvum.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/labparvum.com\/index.php\/wp-json\/wp\/v2\/comments?post=481"}],"version-history":[{"count":2,"href":"https:\/\/labparvum.com\/index.php\/wp-json\/wp\/v2\/posts\/481\/revisions"}],"predecessor-version":[{"id":785,"href":"https:\/\/labparvum.com\/index.php\/wp-json\/wp\/v2\/posts\/481\/revisions\/785"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/labparvum.com\/index.php\/wp-json\/wp\/v2\/media\/484"}],"wp:attachment":[{"href":"https:\/\/labparvum.com\/index.php\/wp-json\/wp\/v2\/media?parent=481"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/labparvum.com\/index.php\/wp-json\/wp\/v2\/categories?post=481"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/labparvum.com\/index.php\/wp-json\/wp\/v2\/tags?post=481"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}