ESPHome esp32 globals - restore value

Hi all,

I’m using esphome for some energy counters and it is working out nicely.

However, due to the storm we had in europe, our electricity went out for a couple of minutes and all my counters were reset to their initial value. Can I use restore_value to true? How will it be stored? I read that for the ESP8266 this might not be a good idea (see https://esphome.io/components/esphome.html#esp8266-restore-from-flash). How is this for the ESP32?

This is my configuration:

globals:
  - id: totalElectricity
    type: float
    restore_value: no
    initial_value: '4301.96'
  - id: totalGas
    type: float
    restore_value: no
    initial_value: '1319.37'
  
sensor:
  - platform: uptime
    name: Basement Uptime Sensor
  - platform: pulse_counter
    name: "Electricity House Pulses / Min"
    pin: 12
    id: pulsesElectricity
  - platform: template
    name: "Electricity House Current kWh / Min"
    unit_of_measurement: 'kWh'
    lambda: |-
      return id(pulsesElectricity).state * 0.06;
  - platform: template
    name: "Electricity House Total kWh"
    unit_of_measurement: 'kWh'
    accuracy_decimals: 3
    lambda: |-
      if (id(pulsesElectricity).state > 0) {
        id(totalElectricity) += (id(pulsesElectricity).state * 0.001);
      }
      return id(totalElectricity);
  - platform: pulse_counter
    name: "Gas House Pulses / Min"
    pin: 14
    id: pulsesGas
  - platform: template
    name: "Gas House Current m3 / Min"
    unit_of_measurement: 'm3'
    lambda: |-
      return id(pulsesGas).state * 0.6;
  - platform: template
    name: "Gas House Total m3"
    unit_of_measurement: 'm3'
    accuracy_decimals: 3
    lambda: |-
      if (id(pulsesGas).state > 0) {
        id(totalGas) += (id(pulsesGas).state * 0.01);
      }
      return id(totalGas);

Depends on how often you are saving new sensor values. The flash on the chip gets 10000 write cycles.

What might be better is to move the counting functionality out of esphome and into HA. Just have esphome compute the pulses.

I try to avoid that - reason is that when I’m working on HomeAssistant and restarting it, I’m losing pulses and thus my measurement is not exact.

That’s a good point. About how often do the pulses arrive?

I believe the write cycle are 100,000. Anyway these chips are like $3 and I keep writing it all the time. from my current calculation should last 2 years of continuous writes.

I made a comment regarding this on the esphome github. It is possible to detect that the power is down and then still write to flash using 2 capacitors and a resistor

See my comment at https://github.com/esphome/feature-requests/issues/461

2 Likes