'On ESPHome Counting and display pulses on gpio binary sensor and duration of time switched on'

On ESPHome with an ESP8266 i have a remote waterpump connected to a binary_sensor where it shows me the status of the pump ‘ON or OFF’
I try to figured out how to count the ‘ON/OFF’ status over 24hrs and the total ‘ON’ time in that period and to display the results but I was not successful.
I need help for this problem.

It’s a lot easier to do in home assistant with the history stats sensor:

Is it possible to program it in ESPhome (ESP8266) not in configuration.yaml?

You write a simple yaml configuration for each sensor/device you want to attach to the ESP and ESPHome compiles the result into an executable file.

You also have the option of including C code as lambdas.

There is provision to include conditional automations on the device. So it can run completely autonomously if you wish.

I did this for the already with several sensors on an ESP32 and ESP8266 there is my working setup. I want to expand the features on

 - **platform: gpio**
**    name: "Wasserpumpe Pin 19"**
**    pin: 19**

to record length of ON and total time. But maybe you have a sample available to do it because I am a newbie and not familiar with lambda and C programming.


switch:
  - platform: restart
    name: "ESP32 Restart" 

  - platform: shutdown
    name: "ESP32_Shutdown"

sensor:
#wifi signal
  - platform: wifi_signal
    name: "ESP32_WiFi Signal"
    update_interval: 6s
    
#bme280
  - platform: bme280
    temperature:
      name: "Temperatur"
    pressure:
      name: "Luftdruck"
    humidity:
      name: "Feuchtigkeit"
    address: 0x76
    update_interval: 6s

#battery
  - platform: adc
    pin: 34
    attenuation: 11db
    name: "Solarpanel 24V Pin34 "
    filters:
     - multiply: 20
    update_interval: 6s
    
  - platform: adc
    pin: 35
    attenuation: 11db
    name: "Akku 24V Pin35"
    filters:
      - multiply: 10
    update_interval: 6s

  - platform: adc
    pin: 32
    attenuation: 11db
    name: "Solarpanel 12V Pin32"
    filters:
      - multiply: 10
    update_interval: 6s
    
  - platform: adc
    pin: 33
    attenuation: 11db
    name: "Akku 12V Pin 33"
    filters:
      - multiply: 10   
    update_interval: 6s

binary_sensor:

  - platform: gpio
    name: "Reservoir voll Pin 16"
    pin: 16
  
  - platform: gpio
    name: "Reservoir low Pin 17"
    pin: 17

  - platform: gpio
    name: "Haus Stromversorgung Pin 18"
    pin: 18
    
  - platform: gpio
    name: "Wasserpumpe Pin 19"
    pin: 19

  - platform: gpio
    name: "Werkstatt Pin 5"
    pin: 5

  - platform: gpio
    name: "Wechselrichter Pin 4"
    pin: 4

  - platform: status
    name: "ESP32 Status" 

Just do it in home assistant. Use the tools you have. Easy as anything.