Time sensor like a stop watch

Hi

Seems lika noob question but I have tried to find out how to measure time with ESPHome. I have a pump running on and off and I would like to measure the time its on. I have a relay connected to data pin but how do I get a sensor to report how many seconds it has been on the last minute?

Regards,

Ozcar

It would be easier to implement in homeassistant using the history stats sensor:

That could work but the time resolution is too low. I need at least second resolution since the pump can start and stop several times per minute. Then of course am using this measured time to calculate other stuff.

you could use a couple of global variables to keep track of pump_seconds, and last_pump_time. The second one (last_pump_time) stores the epoch time of when the pump was last turned on.

Then use a binary sensor which indicates when the pump in on or off, and use the on_press automation with a lambda to read epoch time to calculate how many seconds have elapsed and add that to your global variable. Something along these lines anyway.

You could combine this with a 1min interval to figure out how many seconds pump has been on in last minute, and then reset the pump_seconds global.

Yes that could work. Epoch time makes it easier to do it. I will try it out.

Still strange that there is no simple way to measure time like with a sensor.

I too would like a way for ESPhome to know how long an output has been on so I can use it as a failsafe. Currently I have HA controlling an ESP to control irrigation. A couple of times I have not realised that I restarted HA while the irrigation was on. This caused HA to essentially reset the irrigation script and then not send a stop command to the ESP… leaving the sprinklers on far longer than they should have been before I noticed what had happened. If there is a way to have an in-built timer I could include a failsafe to turn off the ESP output after say 1 hr.

I’m wondering if this can be done with the ESPhome automations, something like:

on_press:
  - then:
      - delay: '01:00:00'
      - switch.turn_off: output_whatever

I would also like something similar for my irrigation setup. And also on top a failsafe if for whatever reason it loses wifi or api connection for more than e.g. 5mins so can auto-stop watering, otherwise will never receive the off command from HA.

I never thought of a delay option, because that seems much easier than my thinking. Should be possible like you mention

Found this on the documentation of esphome

on_...:
  then:
    - switch.turn_on: relay_1
    - delay: 2s
    - switch.turn_off: relay_1
    # Templated, waits for 1s (1000ms) only if a reed switch is active
    - delay: !lambda "if (id(reed_switch).state) return 1000; else return 0;"

This is how I’ve implemented a fail-safe. When it switches on, a script is started that will delay for whatever time you like and then switch off. If it is switched off before the script finishes, the script is stopped.

switch:
  - platform: gpio
    id: device_relay_1
    name: ${entity_prefix_1}
    pin: 
      number: GPIO12
    on_turn_off:
      - script.stop: timer_script_1
    on_turn_on:
      - script.stop: timer_script_1
      - script.execute: timer_script_1

script:
  - id: timer_script_1
    then:
      - delay: 15 min
      - switch.turn_off: device_relay_1
3 Likes