Sonoff POW data resilience and energy yesterday sensor

I just upgraded my first Sonoff POW from Tasmota to ESPHome. Plan is to upgrade all my Sonoff, as long as I can get the same functionality.
Problem is, right now, when the Sonoff is restarted the Energy Today sensor resets and the values become inaccurate. I do reboot all my devices daily, which helps with stability of the network so that would be a dealbreaker.
I thought about using MQTT. That could work for my Energy Yesterday sensor where I only send the value at 23:59, but the Energy Today sensor still gets reset to 0 meaning the yesterday sensor also sends false readings… Anyone knows a workaround for this?
Here is the code used:

esphome:
  name: well_pump
  platform: ESP8266
  board: esp01_1m
  on_boot: 
    - switch.turn_off: well_switch
wifi:
  ssid: "xxx"
  password: "xxx"
# Optional manual IP
  manual_ip:
    static_ip: xxx
    gateway: xxx
    subnet: 255.255.255.0

# Enable logging
logger:

# Enable Web server
web_server:
  port: 80

# Enable Home Assistant API
api:

# Enable OTA Updates
ota:

# Sync time with Home Assistant.
time:
  - platform: homeassistant
    id: homeassistant_time
# Restart every 24 hours
  - platform: homeassistant
    on_time:
      - seconds: 0
        minutes: 0
        hours: 4
        days_of_week: MON-SUN
        then:
           - switch.toggle: restart_switch
# Write Energy Yesterday sensor value
      - seconds: 0
        minutes: 59
        hours: 23
        then:
           - sensor.template.publish:
               id: template_yesterday
               state: !lambda return id(well_power).state ;       
binary_sensor:
  - platform: gpio
    pin:
      number: GPIO0
      mode: INPUT_PULLUP
      inverted: True
    name: "Well Button"
    on_press:
      - switch.toggle: well_switch

switch:
  - platform: restart
    name: "Well Reboot"
    id: restart_switch
  - platform: gpio
    name: "Well Switch"
    pin: GPIO12
    id: well_switch

status_led:
  pin:
    number: GPIO13
    inverted: yes

# Text sensors with general information.
text_sensor:
  # Expose ESPHome version as sensor.
  - platform: version
    name: Well ESPHome Version
  # Expose WiFi information as sensors.
  - platform: wifi_info
    ip_address:
      name: Well IP
    ssid:
      name: Well SSID
    bssid:
      name: Well BSSID

# Example configuration entry
sensor:
  - platform: total_daily_energy
    name: "Well Pump Energy Today"
    power_id: well_power
    accuracy_decimals: 3
    unit_of_measurement: kWh
    filters:
      - multiply: 0.001
  - platform: template
    name: "Well Pump Energy Yesterday"
    id: template_yesterday
    accuracy_decimals: 3
    unit_of_measurement: kWh
    filters:
      - multiply: 0.001
  - platform: hlw8012
    sel_pin: 5
    cf_pin: 14
    cf1_pin: 13
    update_interval: 5s
    current:
      name: "Well Current"
      id: well_current
      accuracy_decimals: 1
    voltage:
      name: "Well Voltage"
      id: well_voltage
      accuracy_decimals: 0
      filters: 
        offset: 25
    power:
      name: "Well Power"
      id: well_power
      accuracy_decimals: 0
 # Uptime sensor.
  - platform: uptime
    name: Well Uptime
  # WiFi Signal sensor.
  - platform: wifi_signal
    name: Well WiFi Signal
    update_interval: 60s

Can’t find any on_reboot keep same values function. Does that exist?

Why not do the “energy today” functions in home assistant?

1 Like

Yup, could be. Need to look into templating this in HA. I do like having this type of extra work done on the device as the ever-increasing size of my HA yaml files gets out of hand!
Any example templating for this that you know of?

Not sure that you need to do any templating. This integration integration (no I am not stuttering) should do the trick https://www.home-assistant.io/integrations/integration

Something like that?

utility_meter:
  well_energy_today:
    source: sensor.well_power
    cycle: daily
  well_energy_monthly:
    source: sensor.well_power
    cycle: monthly

Then I can publish an MQTT sensor at 23:59 on each night for the Yesterday sensor!

sensor:
  - platform: mqtt
    name: "Well Pump Yesterday"
    state_topic: "well_pump/yesterday"
    value_template: '{{ value_json["yesterday"] | round(1) }}'
    force_update: true
    unit_of_measurement: 'kWh'
automation:
  - alias: Well End Of Day Energy
    initial_state: 'on'
    trigger:
    - platform: time
      at: '23:59:30'
    action:
    - service: mqtt.publish
      data_template:
        topic: 'well_pump/yesterday'
        payload: '{"yesterday":{{ states("sensor.well_energy_today") }}'

Cheers for the pointers @nickrout

Will indeed add a lot of code though once you measure energy for 10+ devices…Tasmota may make my life easier!

[NB] This code is untested as of now!

I think that is right, or close to it!

You can also put the yesterday value in an input_number

Wow @nickrout,

I never knew about the integration (component) integration

I have learnt something valuable today!

Actually @monkey-house picked the correct one which is utility_meter https://www.home-assistant.io/integrations/utility_meter

There are lots of interesting integrations in the utility category (not to be confused with the utility_meter sensor) https://www.home-assistant.io/integrations/#utility. Well worth taking a look through.