Time between pump starts

Hi,

I have a Shelly plug to measure watt to determine when my drain pump starts.

I want to calculate the time between every start of the pump and then 1) display the result between last start and the start before that. I also want to save the result so I can se the difference over time.

Start 1 12:12:00
Start 2 12:15:00

A) Duration 3 minutes

Start 3 12:17:00

B) Duration 2 minutes

Start 4 12:19:00

C) Duration 2 minutes

And so on……

I only have to save the duration between every cycle and I Just want to display the time in the last cycle.

Can someone pls point me in the right direction

You will have to create template sensor that will measure time between each pump start. You can do it in a few ways, either using template helper or measureit hacs integration or write your yaml code. It better to use helper or integration just because you don’t have to restart it every time just to find out that it doesn’t work.

Agree there is probably a million ways to do this. In my opinion the best way would be to split up into two features:

  • First create a binary sensor that turns on when the pump is on, and turns off when the pump is off. This can be done in the UI using helpers (integrations → helpers, select “threshold”). Use a hysteresis value to make the sensor more immune to noise.
  • Next you want a sensor that calculates the time from the previous off-on transition to the latest off-on transition. For this, I think a trigger-based template sensor is your best bet. That allows you to specify the specific state to trigger on, and it also retains its state over HA restarts. You will need to keep track of the datetime for the previous transition, and you can do this in an attribute of that sensor.

So, the template sensor would be something like

template:
  - trigger:
      - platform: state
        entity_id:
          - binary_sensor.threshold
        from:
          - 'off'
        to:
          - 'on'
    sensor:
      - name: Time Since Last Turn-On
        unique_id: 7af2369b-04cb-40ca-8527-49933e76fe46
        device_class: duration
        unit_of_measurement: s
        state: >
          {{ now().timestamp() - as_timestamp(this.attributes.previous_trigger_datetime | default( now() )) }}
        attributes:
          previous_trigger_datetime: "{{ now() }}"
1 Like

Thanks alot, I will try as soon as possible.

Really appreciate your effort :slight_smile: