Cumulative counter - is possible?

Hi!
It is possible to have a cumulative counter that save the value and restore after power cycle?
Either in ESP32 eeprom or HA.

I have one sensor that is turned on for 60 seconds, one time per hour (it’s a PM sensor). What I would like is to create a counter for how long this sensor was ‘on’ during his entire life.

You can use Globals with restore_value: true and activate esp8266_restore_from_flash to store the values in flash memory. But remember that flash has only about 100,000 write cycles.

I don’t think it will be enought :sleepy:

lol
thanks Wagner, I’ll take a look into these functions and tryto implement!

For sure it’s possible with NodeRed

Working perfectly!

globals:
  - id: sensor_time_counter
    type: int
    initial_value: '1'
    restore_value: yes
...
sensor:
    - platform: template
      id: pmsx_uptime
      name: "PMSX003 Uptime counter"
      update_interval: 1s
      accuracy_decimals: 0
      lambda: |-
          if (id(pms_set).state) {
            id(sensor_time_counter) += 1;
            return id(sensor_time_counter);
          } else {
            return {};
          }


Don’t even need to set esp8266_restore_from_flash since I’m using ESP32 and this property doesn’t work for ESP32.
Counter still even after flashing with a new version os firmware/program.

Tks guys!

3 Likes