Sonoff POW Elite Configuration

@fstof I’ve recently posted a problem with the POWR320D and the example you post, with the switch not working, but readings seems to work OK, not solved yet…
Try tasmota to see if all is working fine, i don’t think is a diferent sensor…

@GenGT do you perhaps have a link to the issue you posted? I’d like to watch it.

I’m not too keen in tasmota, as I like having all my esp stuff running on ESPhome and I don’t have an mqtt broker set up at the moment…

But anyway, I might just give it a test.

Thanks

This is the link

https://community.home-assistant.io/t/help-config-sonoff-powr320d-that-works-on-tasmota-but-not-in-esphome/492524

Below you have the link to tasmota. You only have to download the tasmota32.bin, upload to POWR320D, reboot, connect to the new SSID go to http://192.168.4.1 config your ssid, and finally choose powr320d template from the list. You can see all values on the web without configure anything of mqtt

A HA!
That post cleared something up on my config… I had the wrong pin on my uart (RX instead of GPIO16)

So now mine are working fine (relay and sensors)

Is fine to know it works for you, could you paste your config so i can see why my relay isn’t working?

Thanks

Here is my config
Note, some of the button functions I removed as I won’t be using the screen

substitutions:
  friendly_name: Pool Pump
  device_name: sonoff-pow-elite-pool-pump

esphome:
  name: $device_name

esp32:
  board: nodemcu-32s

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  ap:
    ssid: $device_name
    password: !secret wifi_failover

captive_portal:
api:
ota:
logger:
  baud_rate: 0

uart:
  rx_pin: GPIO16
  baud_rate: 4800

time:
  - platform: homeassistant
    id: homeassistant_time

sensor:
  - platform: cse7766
    update_interval: 2s
    current:
      name: $friendly_name Current
      id: a_sensor
    voltage:
      name: $friendly_name Voltage
      id: v_sensor
    power:
      name: $friendly_name Power
      id: w_sensor
    energy:
      name: $friendly_name Energy
      id: wh_sensor

  - platform: total_daily_energy
    name: $friendly_name Total Daily Energy
    power_id: w_sensor

  - platform: wifi_signal
    name: $friendly_name Wifi RSSI
    update_interval: 60s

binary_sensor:
  - platform: status
    name: $friendly_name Status

  - platform: gpio
    pin:
      number: GPIO0
      mode: INPUT_PULLUP
      inverted: True
    id: button_1
    on_press:
      then:
        - switch.toggle: relay_1

  # - platform: gpio
  #   pin: GPIO00
  #   id: reset
  #   internal: true
  #   filters:
  #     - invert:
  #     - delayed_off: 10ms
  #   on_click:
  #     - max_length: 350ms # short press to toggle the relay
  #       then:
  #         switch.toggle: relay_1
  #     - min_length: 360ms # long press to cycle display info
  #       max_length: 3s
  #       then:
  #         - if:
  #             condition:
  #               binary_sensor.is_on: page
  #             then:
  #               binary_sensor.template.publish:
  #                 id: page
  #                 state: OFF
  #             else:
  #               binary_sensor.template.publish:
  #                 id: page
  #                 state: ON

  - platform: template # this is a fake sensor to tell the screen which info to show on display
    id: page
    publish_initial_state: true
    internal: true

display:
  platform: tm1621
  id: tm1621_display
  cs_pin: GPIO25
  data_pin: GPIO14
  read_pin: GPIO26
  write_pin: GPIO27
  lambda: |-
    if (id(page).state) {
      it.display_voltage(true);
      it.display_kwh(false);
      it.printf(0, "%.1f", id(v_sensor).state);
      it.printf(1, "%.1f", id(a_sensor).state);
    } else {  
      it.display_voltage(false);
      it.display_kwh(true);
      it.printf(0, "%.1f", id(wh_sensor).state);
      it.printf(1, "%.1f", id(w_sensor).state);
    }

output:
  - platform: ledc
    id: led
    pin:
      number: GPIO18
      inverted: True

switch:
  - platform: template
    name: $friendly_name
    optimistic: true
    id: relay_1
    lambda: |-
      if (isnan(id(w_sensor).state)) {
        return {};
      } else if (id(w_sensor).state > 4) {
        // Running
        return true;
      } else {
        // Not running
        return false;
      }
    turn_off_action:
      - switch.turn_on: relay_off
    turn_on_action:
      - switch.turn_on: relay_on
  - platform: gpio
    restore_mode: ALWAYS_ON
    internal: true
    id: relay_off
    pin: GPIO04
    on_turn_on:
      - delay: 500ms
      - switch.turn_off: relay_off  #bi-stable relay so no need to keep on.
      - light.turn_off: switch_led
    interlock: [relay_on]
  - platform: gpio
    restore_mode: ALWAYS_OFF
    internal: true
    id: relay_on
    pin: GPIO02
    on_turn_on:
      - delay: 500ms
      - switch.turn_off: relay_on  #bi-stable relay so no need to keep on.
      - light.turn_on: switch_led
    interlock: [relay_off]
  - platform: restart
    name: $friendly_name Restart

light:
  - platform: monochromatic
    id: switch_led
    output: led
    internal: True
  - platform: status_led
    id: wifi_status_led
    internal: True
    pin:
      number: GPIO05
      inverted: True

interval:
  - interval: 30s
    then:
      if:
        condition:
          wifi.connected:
        then:
          - light.turn_on: wifi_status_led
        else:
          - light.turn_off: wifi_status_led

Finally!

Seems the problem is on this code

    lambda: |-
      if (isnan(id(w_sensor).state)) {
        return {};
      } else if (id(w_sensor).state > 4) {
        // Running
        return true;
      } else {
        // Not running
        return false;
      }

I’m making test without having anything connected to the output, and this code seems that only works if a device with consumption is connected…

Yea not sure about that, because it was working fine when I tested with no load on the output.

this lambda is a little confusing, I need to still try and understand it, there is a lot of simplification to be done in my opinion

This is how i solved this part. You don’t need the lambda part… Now also working without load. (sorry don’t know how to publich the data in the way You people do

switch:
  - platform: template
    name: $friendly_name
    optimistic: true
    id: relay_1
#    lambda: |-
#      if (isnan(id(w_sensor).state)) {
#        return {};
#      } else if (id(w_sensor).state > 4) {
#        // Running
#        return true;
#      } else {
#        // Not running
#        return false;
#      }
    turn_off_action:
      - switch.turn_on: relay_off
    turn_on_action:
      - switch.turn_on: relay_on
  - platform: gpio
    restore_mode: ALWAYS_OFF
    internal: true
    id: relay_off
    pin: GPIO04
    on_turn_on:
      - switch.turn_on: relay_off     
      - delay: 500ms
      - switch.turn_off: relay_off  # bi-stable relay so no need to keep on.
      - light.turn_off: switch_led
    interlock: [relay_on]
  - platform: gpio
    restore_mode: ALWAYS_OFF
    internal: true
    id: relay_on
    pin: GPIO02
    on_turn_on:
      - switch.turn_on: relay_on      
      - delay: 500ms
      - switch.turn_off: relay_on  # bi-stable relay so no need to keep on.
      - light.turn_on: switch_led
    interlock: [relay_off]
  - platform: restart
    name: $friendly_name Restart


1 Like

Hello
I confirm with these #lambda settings…
The switch works.
But after reboot or power outage it doesn’t show status in HA.

edit

is it possible to go back to the original sonoff firmware ?

Hi, you can go back to the original sonoff firmware via serial, but only if you have a backup from the original firmware. I have no problem with the status… Try with tasmota firmware, if you select he correct model all will run out of the box

1 Like

Hello there,

I tried every trick in the book and then some.
My recently bought sonoff powr320D works fine with tasmota but not is esphome.
I tried even the basic CSE7766 Power Sensor examples. Esphome 2023.5.5 CSE7766 platform doen’t respond at all. It gives no readings. Not even in logs. I checked the chip on the PCB and it is the CSE7759B-SO.
Maybe CSE7766 platform it is not working properly with this chip?
Does anybody have any suggestions?

Hello, after debug uart on powr316 & reading CSE7766 datasheet, i add the line “parity: even” on uart config . Now i can view every electrical data. perhaps it can help you ?

3 Likes

Perfect. That does the trick!!!.. Thanks Chris

uart:
  rx_pin: GPIO16
  baud_rate: 4800
  parity: even
1 Like

Thank you. I will try that. In the meantime I have tried a different approach.
It also seems to work commenting out the “baud_rate” line in logger.

logger:
  level: DEBUG
  #  baud_rate: 0

uart:
  - rx_pin: GPIO16
    baud_rate: 4800

sensor:
  - platform: cse7766

Agree.

Even commenting out the the ’ level: DEBUG ’ line works.

logger:
#  level: DEBUG
#    baud_rate: 0

uart:
  rx_pin: GPIO16
  baud_rate: 4800
#  parity: even

I came across this thread after dealing with an issue of not being able to turn off the relay on my Sonoff 320D.

After looking at the code copied from Sonoff-POW-Elite-20a I came to the same conlusion that the Lamda code was the problem.

#      if (isnan(id(w_sensor).state)) {
#        return {};
#      } else if (id(w_sensor).state > 4) {
#        // Running
#        return true;
#      } else {
#        // Not running
#        return false;
#      }

I don’t know the rational behind the code but there is a serious problem with it.

If there is no current flowing you cannot turn off the relay. In my use case I am using this to switch on an immersion heater. If the immersion heather thermostat cuts power to the heater you cannot turn off the relay supplying power to the immersion heater.

I have updated the ESP code to comment out the Lamda code as other have done and now have proper control over the relay state.

what is the current recommended configuration for the sonoff powr320d ?

can you paste the whole yaml?

Hello guys, it’s been a year since I posted my config file, and I had to revisit it cuz I updated the EspHome version of the device and I lost all readings and I thought I could use my original post as a backup config, but was even better, chris1313 on post 25 solved it.

Apparently now you can find the config file on esphome website but google seems to like this post so here goes my updated config file with chris1313 solution and beaj improvement:

substitutions:
  device_name: your_device_name
  device_ip: your_device_ip

esphome:
  name: ${device_name}

esp32:
  board: nodemcu-32s

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  manual_ip:
    static_ip: ${device_ip}
    gateway: !secret wifi_gateway
    subnet: 255.255.255.0
  ap:
    password: !secret wifi_password
captive_portal:

logger:
    baud_rate: 0
    
api:
  reboot_timeout: 60min
  encryption:
    key: !secret api_key_cryp

ota:
  password: !secret ota_password

web_server:
  port: 80

uart:
  rx_pin: GPIO16
  baud_rate: 4800
  parity: even

time:
  - platform: homeassistant
    id: homeassistant_time

sensor:
  - platform: cse7766
    update_interval: 2s
    current:
      name: ${device_name} Current
      id: a_sensor
    voltage:
      name: ${device_name} Voltage
      id: v_sensor
    power:
      name: ${device_name} Power
      id: w_sensor
    energy:
      name: ${device_name} Energy
      id: wh_sensor
  - platform: total_daily_energy
    name: ${device_name} Total Daily Energy
    power_id: w_sensor
  - platform: wifi_signal
    name: ${device_name} Wifi Signal Strength
    update_interval: 60s
  - platform: uptime
    id: uptime_sensor
    internal: True
    on_raw_value:
      then:
        - text_sensor.template.publish:
            id: uptime_human
            state: !lambda |-
              int seconds = round(id(uptime_sensor).raw_state);
              int days = seconds / (24 * 3600);
              seconds = seconds % (24 * 3600);
              int hours = seconds / 3600;
              seconds = seconds % 3600;
              int minutes = seconds /  60;
              seconds = seconds % 60;
              return (
                (days ? to_string(days) + "d " : "") +
                (hours ? to_string(hours) + "h " : "") +
                (minutes ? to_string(minutes) + "m " : "") +
                (to_string(seconds) + "s")
              ).c_str();
  - platform: template
    name: ${device_name} Power Factor
    device_class: power_factor
    id: power_factor
    lambda: return id(w_sensor).state / id(v_sensor).state / id(a_sensor).state;

binary_sensor:
  - platform: gpio
    pin: GPIO00
    id: reset
    internal: true
    filters:
      - invert:
      - delayed_off: 10ms
    on_click:
      - max_length: 350ms # short press to toggle the relay
        then:
          switch.toggle: relay_1
      - min_length: 360ms # long press to cycle display info
        max_length: 3s
        then:
          - if:
              condition:
                binary_sensor.is_on: page
              then:
                binary_sensor.template.publish:
                  id: page
                  state: OFF
              else:
                binary_sensor.template.publish:
                  id: page
                  state: ON
  - platform: template # this is a fake sensor to tell the screen which info to show on display
    id: page
    publish_initial_state: true
    internal: true
  - platform: template
    name: ${device_name} subordinate device
    id: subordinate_device_on
    lambda: |-
      if (isnan(id(w_sensor).state)) {
        return {};
      } else if (id(w_sensor).state > 4) {
        // Running
        return true;
      } else {
        // Not running
        return false;
      }

display:
  platform: tm1621
  id: tm1621_display
  update_interval: 1s
  cs_pin: GPIO25
  data_pin: GPIO14
  read_pin: GPIO26
  write_pin: GPIO27
  lambda: |-
    if (id(page).state) {
      it.display_voltage(true);
      it.display_kwh(false);
      it.printf(0, "%.1f", id(v_sensor).state);
      it.printf(1, "%.1f", id(a_sensor).state);
    } else {  
      it.display_voltage(false);
      it.display_kwh(true);
      it.printf(0, "%.1f", id(wh_sensor).state);
      it.printf(1, "%.1f", id(w_sensor).state);
    }

output:
  - platform: ledc
    id: led
    pin:
      number: GPIO18
      inverted: True

switch:
  - platform: gpio
    name: ${device_name}_switch
    icon: "mdi:light-switch-off"
    pin: GPIO13
    id: relay_1
    restore_mode: RESTORE_DEFAULT_OFF
    on_turn_on:
      - light.turn_on: switch_led
    on_turn_off:
      - light.turn_off: switch_led
  - platform: restart
    name: ${device_name} Restart


text_sensor:
  - platform: template
    name: ${device_name} Uptime
    id: uptime_human
    icon: mdi:clock-start
  - platform: wifi_info
    ip_address:
      name: ${device_name} IP
    ssid:
      name: ${device_name} SSID
    bssid:
      name: ${device_name} BSSID

light:
  - platform: monochromatic
    id: switch_led
    output: led
    internal: True
  - platform: status_led
    id: wifi_status_led
    internal: True
    pin:
      number: GPIO05
      inverted: True

interval:
  - interval: 30s
    then:
      if:
        condition:
          wifi.connected:
        then:
          - light.turn_on: wifi_status_led
        else:
          - light.turn_off: wifi_status_led
1 Like

Hey guys, any idea why the device is not visible in the HASS esphome integration? Actually none of the entities seems to be created, although I see it in web UI and also in esphome dashboard.