Trying to understand "Duty Time"

Hello,
i’m building a smart “mosquitto repellent”, i have a switch toggling it on and off based on scheduler and so on.
I’d like to track how long the switch is ON so i can understand when the cartridge is exausted.
So i thought about the duty time component. Seems perfect! It can also be reset when a new cartridge is installed…
Now, my problem. In Home Assistant the value goes back to zero at every reboot of the esphome device. I expected it to be summed (when not storing values on the ESP). I know i can setup the local saving of data, but i’m a little bit scared about eeprom wear, so i was wondering if there is a way to store the value HA side, keeping the functionalities of reset of the counter.
On the other side i could create an helper, but then resetting it seems a little less “logic” than a toggle on the esp device…
any hint?
thanks

Don’t be. Esphome default flash write interval for Esp32 is 1min and I haven’t really heard of failures in common use.

Can try to use Home Assistant history stats: History Stats - Home Assistant to accumulate hours/counts on HA side.

For something ike using following in HA:

  input_datetime:
    xxx_filter_cleaned:
      name: "XXX Filter Cleaned"
      icon: mdi:clock-time-two-outline
      has_time: true
      has_date: true

sensor:
    - platform: history_stats
      name: "XXX Running Time"
      entity_id: binary_sensor.xxx_on
      state: "on"
      type: time
      start: "{{ states('input_datetime.xxx_filter_cleaned') }}"
      end: "{{ now() }}"

when binary_sensor.xxx_on is exposed from ESPHome device indicating it is running.
Me setting input_datetime.xxx_filter_cleaned manually, but can create also button to set it to current date/time, kind a reset.
Only issue is - if HA not receive state changes due to network or HA issues, it will not count running time.

i ended up setting “restore” to true (so saving in flash) and an update period of 15 minutes. The residual issue i see is that it gets erased when i update the esphome firmware. can live with this…
thanks

Good, and flash write occurs every 15min only if the switch state has changed, the actual write interval might be much longer

do you think there is any way to force the “duty time” sensor to start from a certain value? i could force it toe that value when flashing new code…

No idea, documentation is next to zero… It has reset action, try if it accepts a value

switch:
  - platform: template
    name: "duty time reset"
    id: dutytime_reset
    turn_on_action:
      - sensor.duty_time.reset:
          id: YOUR_dt_sensor_id
          value: 500

change the id to match your duty time id