Solution on ESP32 to count the total Running time from a Pump. (without History stats)

Hello everybody,
i am searching since many days how to get a total Running time for a Pump directly on ESP32. I don’t want to use History stats because it is running on HA server and not directly on the esp32 (in case of loosing the WiFi ).
I got some code from another Topic but it is not running.
I just want to capture the time at teh start and get the running time when it is stopping and save it in a variable like 00:00:00
Can somebody help here to debug?

here is my modified code :

  • id: Switch1_TotalRuntimeStart
    type: int
    restore_value: no
    initial_value: ‘0’

  • id: Switch1_TotalRuntime
    type: int
    restore_value: yes
    initial_value: ‘0’

  • platform: template
    name: “Switch1_TotalRuntimeTest2”
    id: Switch1_TotalRuntimeTest2

switch:

  • platform: gpio
    name: “Switch1”
    inverted: yes
    pin: 13
    restore_mode: ALWAYS_OFF
    id: switch1
    on_turn_on:
    then:
    - output.turn_on: SensorChooser_S0
    - output.turn_off: SensorChooser_S1
    - output.turn_on: SensorChooser_S2
    - output.turn_on: SensorChooser_Z
    - output.turn_off: SensorChooser_E
    - lambda: ‘id(Switch1_TotalRuntimeStart) = id(sntp_time).now().timestamp;’
    - logger.log: {level: INFO, format: "Switch1_TotalRuntimeStart %i ", args: [Switch1_TotalRuntimeStart]}
    #- delay: 300s
    #- switch.turn_off: switch1

    on_turn_off:
    then:
    - output.turn_off: SensorChooser_S0
    - output.turn_off: SensorChooser_S1
    - output.turn_off: SensorChooser_S2
    - output.turn_off: SensorChooser_Z
    - output.turn_on: SensorChooser_E
    - lambda: |-
    id(Switch1_TotalRuntime) = id(Switch1_TotalRuntime) + (id(Switch1_TotalRuntimeStart)- id(sntp_time).now().timestamp);
    id(Switch1_TotalRuntimeStart) = 0;
    - sensor.template.publish:
    id: Switch1_TotalRuntimeTest2
    state: !lambda ‘return id(Switch1_TotalRuntimeTest2).state + (id(sntp_time).now().timestamp - id(Switch1_TotalRuntimeStart));’
    - logger.log: {level: INFO, format: "Switch1_TotalRuntimeTest2 %i ", args: [id(Switch1_TotalRuntimeTest2).state]}
    - logger.log: {level: INFO, format: "Switch1_TotalRuntimeStart %i ", args: [Switch1_TotalRuntimeStart]}
    - logger.log: {level: INFO, format: "sntp_time %i ", args: [id(sntp_time).now().timestamp]}

here nothing is calculated.
why is the start value in Int so far from the stop value(sntp_time) , it runned only few seconds

Hi - can you edit your post please to place your YAML inside preformatted text tags, so we can read it properly. They are accessed using the </> button on the posting toolbar, or by placing three backticks around your code: ```