First pass below. Compiles but in no way tested.
globals:
# used to store timestamp for sensor on
- id: timestamp
type: int
time:
- platform: sntp
id: sntp_time
# at midnight:
on_time:
- seconds: 0
minutes: 0
hours: 0
then:
# reset daily time counter
- sensor.template.publish:
id: run_time
state: !lambda 'return 0;'
binary_sensor:
# input binary sensor we want to calculate total on-time for
- platform: gpio
name: "Input Sensor"
id: input_sensor
pin:
number: GPIO26
mode:
input: true
pullup: true
on_press:
# store the current epoch time (seconds since 1/1/70)
- lambda: 'id(timestamp) = id(sntp_time).now().timestamp;'
on_release:
# calculate the total time = (old total time) + (current epoch time) - (stored on_press epoch time)
# and publish in the template sensor
- sensor.template.publish:
id: run_time
state: !lambda 'return id(run_time).state + id(sntp_time).now().timestamp - id(timestamp);'
sensor:
# total run time for the day sensor
- platform: template
name: "Run Time"
id: run_time