Shelly 1PM?

Just wanted to share my esphome file. It’s calibrated by me according to frenck’s guide… It may need some calibration for you. I haven’t tried if the button sensor is working yet and I just remembered that it also got a temperature sensor. I will update with a fix for this later…

# From Tasmota template
# "NAME":"Shelly 1PM","GPIO":[0: Led1i,0,0,0,4: D_SENSOR_SWITCH,5: BL0937 CF,0,0,0,0,0,15: Relay1,0],"FLAG":2,"BASE":18}

substitutions:
  plug_name: "shelly1pm"

esphome:
  name: ${plug_name}
  platform: ESP8266
  board: esp01_1m

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  fast_connect: true
  domain: !secret domain

  manual_ip:
    static_ip: 10.222.50.198
    gateway: 10.222.50.1
    subnet: 255.255.255.0
    dns1: 10.222.50.1
    
# Enable logging
logger:
  level: VERBOSE

# Enable Home Assistant API
api:
  password: !secret password

ota:
  password: !secret password
  
time:
  - platform: homeassistant
    id: homeassistant_time

switch:
  - platform: gpio
    name: "${plug_name}_switch"
    icon: "mdi:power-socket-eu"
    pin: GPIO15
    id: relay

sensor:
  - platform: hlw8012
    cf_pin: GPIO05
    cf1_pin: GPIO13 # not used because it is not available on the 1PM but it is needed to compile
    sel_pin: GPIO14 # not used because it is not available on the 1PM but it is needed to compile
    power:
      name: "${plug_name}_power"
      unit_of_measurement: W
      id: "${plug_name}_power"
      icon: mdi:flash-circle
      accuracy_decimals: 0
      filters:
      # Map from sensor -> measured value
      - calibrate_linear:
          - 0.0 -> 1.0
          - 110.33186 -> 20.62
          - 131.01909 -> 24.32
          - 341.33920 -> 62.08
          - 5561.41553 -> 1000.0
          - 2975.51221 -> 535.7
          - 9612.66309 -> 1720.0
          - 14891.35352 -> 2679.0      
      # Make everything below 2W appear as just 0W.
      # Furthermore it corrects 1.0W for the power usage of the plug.
      - lambda: if (x < (2 + 1)) return 0; else return (x - 1);
    update_interval: 3s

binary_sensor:
  # Binary sensor for the button press
  - platform: gpio
    name: button
    pin:
      number: GPIO4
      inverted: true
    on_press:
      - switch.toggle: relay

output:
  # Relay state led
  - platform: esp8266_pwm
    id: state_led
    pin:
      number: GPIO00
      inverted: true
4 Likes

Thanks a lot @Anders_Karlstrom!
Your config helped me with my shelly-1pm.

How did you find the GPIO15?
Over at esphome they just name " GPIO4" for the relay.

Any progress regarding the temperature sensor? Would be awesome to have!

Regards!
Brandon

I looked at the tasmota config.
The temperature sensor were a bit more complicated. I think i will need to write some logic and push it to esphome to make it work. I don’t really know when I got time for it. I will make a feature request for it, maybe someone else got time :slight_smile:

1 Like

watching :nerd_face:

Thermistor seems to be working here. I predicted the componentry would be similar to the Shelly 2.5, so I copied the thermistor part from my shelly 2.5 config and it seems to also work for the shelly 1pm. Luckily the ESP8266 doesn’t have many analog inputs (one to be exact), and it’s likely manufacturers have saved by using the same components for the thermistor divider on all their products. So it was an easy guess. I haven’t frozen it or caught it on fire, or otherwise calibrated it, but it seems reasonable hovering around 37C after warming up on my desk, and jumping up when I heat it with a blow dryer.

I haven’t hooked it up to mains voltage to calibrate it yet, and the power reading seems to be floating around 37-40W (this could be from floating nodes in the circuit due to not being wired up). The LED is correct, OTA works (didn’t with the PWM led config above), and not sure if the relay works until I wire it up. If it does, the relay is super duper quiet because I can’t hear it click when I toggle it (at least compared to my Shelly 2.5 relay’s).

substitutions:
  name: "garage_lights"
  friendly_name: "Garage Lights"

esphome:
  name: ${name}
  platform: ESP8266
  board: esp01_1m

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  
  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "${friendly_name} Fallback Hotspot"
    password: "oops"

# Enable logging
logger:

api:
  password: !secret garage_lights_password

ota:
  password: !secret ota_password

switch:
  - platform: gpio
    name: "${friendly_name} Switch"
    icon: "mdi:power-socket-us"
    pin: GPIO15
    id: relay
    
sensor:
  - platform: wifi_signal
    name: "${friendly_name} WiFi Signal"
    update_interval: 60s
    
  - platform: hlw8012
    cf_pin: GPIO05
    cf1_pin: GPIO13 # not used because it is not available on the 1PM but it is needed to compile
    sel_pin: GPIO14 # not used because it is not available on the 1PM but it is needed to compile
    power:
      name: "${friendly_name} Power"
      unit_of_measurement: W
      id: "${name}_power"
      icon: mdi:flash-circle
      accuracy_decimals: 0
      filters:
      # Make everything below 2W appear as just 0W.
      # Furthermore it corrects 1.0W for the power usage of the plug.
      - lambda: if (x < (2 + 1)) return 0; else return (x - 1);
    update_interval: 10s
    
  # NTC Temperature
  - platform: ntc
    sensor: ${name}_resistance_sensor
    name: "${friendly_name} Temperature"
    unit_of_measurement: "°C"
    accuracy_decimals: 1
    icon: mdi:thermometer
    calibration:
      b_constant: 3350
      reference_resistance: 10kOhm
      reference_temperature: 298.15K
  - platform: resistance
    id: ${name}_resistance_sensor
    sensor: ${name}_source_sensor
    configuration: DOWNSTREAM
    resistor: 32kOhm
  - platform: adc
    id: ${name}_source_sensor
    pin: A0
    update_interval: 10s

binary_sensor:
  - platform: status
    name: "${friendly_name} Status"
  # Binary sensor for the button press
  - platform: gpio
    name: "${friendly_name} Button"
    pin:
      number: GPIO4
      inverted: true

status_led:
  pin: GPIO0

Got it wired up today and all is working well; current, voltage, relay, input switch, and temperature!

The calibration of the power sensor is way off, showing 700W for what should be a <100W or so load. I’ll be calibrating this with my diy ‘killer watts’ meter, but otherwise consider this a working shelly 1pm config. Regardless, the sensor is responding properly, so calibration is all that is needed here.

I included some small automations to make the existing toggle style wall switch behave like a standard wall switch (up=on, down=off). I also have node red doing some additional automations based on motion, luminance, and switch position, but that’s out of the scope of this post. Hope this helps someone down the line!

# Shelly 1pm Example#############################

substitutions:
  name: "garage_lights"
  friendly_name: "Garage Lights"

esphome:
  name: ${name}
  platform: ESP8266
  board: esp01_1m

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  
  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "${friendly_name} Fallback Hotspot"
    password: "oops"

# Enable logging
logger:

api:
  password: !secret garage_lights_password

ota:
  password: !secret ota_password

switch:
  - platform: gpio
    name: "${friendly_name} Relay"
    icon: "mdi:power-socket-us"
    pin: GPIO15
    id: relay
    
sensor:
  - platform: wifi_signal
    name: "${friendly_name} WiFi Signal"
    update_interval: 60s
    
  - platform: hlw8012
    cf_pin: GPIO05
    cf1_pin: GPIO13 # not used because it is not available on the 1PM but it is needed to compile
    sel_pin: GPIO14 # not used because it is not available on the 1PM but it is needed to compile
    power:
      name: "${friendly_name} Power"
      unit_of_measurement: W
      id: "${name}_power"
      icon: mdi:flash-circle
      accuracy_decimals: 0
      filters:
      #Reading -> actual
      - calibrate_linear:
          - 2.5 -> 0.16
          - 747.0 -> 125.0
          - 1409.0 -> 237.0
          - 2663.0 -> 444.0
          - 8600.0 -> 1390.0
      - lambda: if (x <= 6.0) return 0; else return (x - 6);
    update_interval: 10s
    
  # NTC Temperature
  - platform: ntc
    sensor: ${name}_resistance_sensor
    name: "${friendly_name} Temperature"
    unit_of_measurement: "°C"
    accuracy_decimals: 1
    icon: mdi:thermometer
    calibration:
      b_constant: 3350
      reference_resistance: 10kOhm
      reference_temperature: 298.15K
  - platform: resistance
    id: ${name}_resistance_sensor
    sensor: ${name}_source_sensor
    configuration: DOWNSTREAM
    resistor: 32kOhm
  - platform: adc
    id: ${name}_source_sensor
    pin: A0
    update_interval: 10s

binary_sensor:
  - platform: status
    name: "${friendly_name} Status"
  # Binary sensor for the button press
  - platform: gpio
    name: "${friendly_name} Input"
    pin:
      number: GPIO4
    on_state:
      - switch.toggle: relay
    #on_press:
    #  - switch.turn_on: relay
    #on_release:
    #  - switch.turn_off: relay

status_led:
  pin: GPIO0

[edit: Got my calibration done… added it to this config. Incidentally, that’s the farthest off from calibration I’ve ever seen a sensor… not that it means anything, but usually output values are within a few 10% before calibrating, not ~800% off as in this case. Either way, a linear calibration does look valid for the sensor (roughly the same 8x factor across the range).]

5 Likes

@truglodite Thanks for your config. Finally able to calibrate the temperature sensor. I tried it without the ntc platform which wasn’t that clever :stuck_out_tongue:

Thats because of the relativly high shunt resistor value compared to the Sonoff’s (which are used for the default values in ESPHome). Higher Shunt Resistor value means higher accuracy when measuring the power consumtion but also higher heat dissipation which is why the shelly overheat when having >1000W for longer time. (Didn’t verify myself but read it)

I tried to implement a logic to turn off the relay when a cricical temperatur was reached. Not perfect since it still turns on the switch for at least 100ms. Maybe I can do something better with template switches and lambdas.

# Overheat Protection
interval:
  - interval: 100ms
    then:
      if:
        condition:
          and:
            - sensor.in_range:
                id: temperature
                above: 60.0
            - switch.is_on: relay
        then:
          - switch.turn_off: relay
2 Likes

I use nodered for all my automations, and I basically have it shut off the relay if either the temp or current go over spec. There’s no interval, but my safety method still does have to wait for the shelly to report the data. Since temps are only reported every 3sec (I figure that’s fast enough to catch something overheating), and watts are reported on state change, I may have a 3sec delay before trigger. That’s a lot worse than 100ms.

I think what you’re doing there is fine, and I will eventually convert my setup to do the same soon (keeping the safety internal on the esp so no HA requirement, and using an interval). I think 100ms is plenty fast don’t you? I suppose some sort of interrupt could be configured, but that might cause issues coming from an analog sensor pin, if even possible. At the end of the day, I think the best design would involve shelly adding a thermal switch, which could then be hooked to an interrupt for instant response… but then again we have the response of the thermal switch, lol.

I’ve made your of your config to flash 9 Shelly 1 Pms and it works great, apart from none of them report current or voltage. It’s always 0.0.

Power works fine though.

Is your’s reporting V and A OK?

What may I be missing?

Seems like current and voltage is not possible without hardware modification due to missing wiring.
See https://github.com/arendst/Tasmota/issues/5716

Thank you for finding that. I’ll waste no more time in trying to get the readings!

Are these able to be flashed with tuya-convert in the normal way? Are they affected by the encryption issues affecting other Tuya devices? I am trying to do one now but it isnt finding the device.

Shelly devices do not run Tuya, so no, you won’t be able to use tuya-convert.

However - in case you missed it - Shelly devices are now natively supported since v0.115, so there is no need to install a different firmware. If you still want to run Tasmota/ESPHome instead then you would need to flash with a serial connection, and the Shelly 1PM conveniently exposes the required pins on the outside of its case.

Does anyone know if the Shelly 1PM in HA displays/reads the current/voltage items natively as well? I can’t seem to find anyone to confirm/deny that.

Here is a sample Shelly 1PM entities list:


As you can see, current or voltage are not available.

I flashed shelly1pm with sonoff tasmota.i think it works fine.

I have connected CF1 to GPIO14 and SEL to GPIO12.
What would be the correct configuration? I think mA may have started working but not voltage…

@alferboy I have since replaced my 1pm’s with 2.5’s, which have a different sensor. So I am not setup to test this, but referring to the esphome docs it appears my originally posted config above may be incorrect… Try using the config posted on the info page instead:

sensor:
  - platform: hlw8012
    sel_pin: 5
    cf_pin: 14
    cf1_pin: 13
    current:
      name: "HLW8012 Current"
    voltage:
      name: "HLW8012 Voltage"
    power:
      name: "HLW8012 Power"
    energy:
      name: "HLW8012 Energy"
    update_interval: 60s

Also note at the bottom of the page there is a section about a “Permanent SEL Pin” hardware variant, which only has measurement mode available (likely power only). I suppose if you have a 1pm that has the pullup, you could find out how to remove it from the board to allow all 3 measurement modes.

@truglodite,

That was my base to add current and voltage, but according to the pins used as in https://github.com/arendst/Tasmota/issues/5716 (GPIO12 and GPIO14). There is no pull-up a.
I’ve connected those wires but it seems maybe something’s mixed up on the voltage/current (they seem maybe “swapped”).

With a 4w led bulb I was getting:

[19:32:32][D][hlw8012:063]: Got power=29.0W, current=21.7A
[19:32:42][D][hlw8012:063]: Got power=29.0W, current=21.7A
[19:33:02][D][hlw8012:071]: Got power=27.9W, voltage=1.5V
[19:33:12][D][hlw8012:071]: Got power=29.0W, voltage=1.6V
[19:33:32][D][hlw8012:063]: Got power=29.0W, current=21.7A
[19:33:42][D][hlw8012:063]: Got power=29.0W, current=21.7A
[19:34:02][D][hlw8012:071]: Got power=29.0W, voltage=1.6V
[19:34:12][D][hlw8012:071]: Got power=29.0W, voltage=1.5V
[19:34:32][D][hlw8012:063]: Got power=27.9W, current=21.8A

I changed the voltage divider so that voltage would go to around 230V and got (same wiring - check the power increase):

[19:35:22][D][hlw8012:071]: Got power=4282.2W, voltage=238.1V
[19:35:32][D][hlw8012:071]: Got power=4440.8W, voltage=231.8V
[19:35:52][D][hlw8012:063]: Got power=4282.2W, current=21.9A
[19:36:02][D][hlw8012:063]: Got power=4440.8W, current=21.9A
[19:36:22][D][hlw8012:071]: Got power=4440.8W, voltage=231.8V
[19:36:32][D][hlw8012:071]: Got power=4440.8W, voltage=231.8V
[19:36:52][D][hlw8012:063]: Got power=4440.8W, current=21.9A
[19:37:02][D][hlw8012:063]: Got power=4440.8W, current=21.9A
[19:37:22][D][hlw8012:071]: Got power=4282.2W, voltage=231.8V
[19:37:32][D][hlw8012:071]: Got power=4440.8W, voltage=238.1V
[19:37:52][D][hlw8012:063]: Got power=4282.2W, current=21.9A
[19:38:02][D][hlw8012:063]: Got power=4440.8W, current=21.9A
[19:38:22][D][hlw8012:071]: Got power=0.0W, voltage=75.2V
[19:38:32][D][hlw8012:071]: Got power=475.8W, voltage=87.7V
[19:38:52][D][hlw8012:063]: Got power=317.2W, current=21.7A
[19:39:02][D][hlw8012:063]: Got power=317.2W, current=21.7A
[19:39:22][D][hlw8012:071]: Got power=0.0W, voltage=81.5V
[19:39:32][D][hlw8012:071]: Got power=317.2W, voltage=68.9V
[19:39:52][D][hlw8012:063]: Got power=0.0W, current=21.7A
[19:40:02][D][hlw8012:063]: Got power=317.2W, current=21.6A
[19:40:22][D][hlw8012:071]: Got power=317.2W, voltage=87.7V
[19:40:32][D][hlw8012:071]: Got power=0.0W, voltage=75.2V
[19:40:52][D][hlw8012:063]: Got power=317.2W, current=21.7A

When voltage is belows 100v is when I turn off the relay. Yet current is always fixed, leading me to believe that they are somehow exchanged. Perhaps the sel pin needs invertion? very odd…

Reading that, my understanding is that the 1pm has some disconnected pins. It looks like Vorta came up with a hack that requires soldering 2 wires on the esp chip. That could be translated to esphome configs easily, but would still require the soldering.