Building a derivative function with a timeout

I want to convert a train of pulses from a water meter (1 pulse per litre) into a flow rate (litres per minute). The derivative helper is kind of OK, but it lacks the timeout option that the ESPhome pulse_meter integration has. I need this because otherwise the derivate shows the last-calculated flow rate indefinitely, waiting for the next pulse to recalculate it.

My meter won’t register less than 40 litres per hour. So… if no pulse arrives within 90 seconds of the previous one, the function should show the flow rate as zero litres per minute and sit like that until the next pulse arrives, when it can start calculating again.

I can’t find a way to achieve this, short of writing an automation which would be triggered either by a change in the water meter’s pulse counter or a 90 second timeout after the last pulse. If a pulse arrives and it’s less than 90 seconds since the last, we can calculate a flow rate in litres per minute. If the 90 second timeout has been reached, the flow rate is effectively zero. But… I need to store timestamps between triggers in the automation - not something I’ve done before. Any tips, please?

It would be much nicer if the derivative sensor would be fixed to show the actual derivative instead of it failing as it does now.

it’s had an open issue (the last I knew) for months about that problem but no one wants to address it.

+1 Stuck with the same issue. The sensor should trigger itself after leaving time_window to recognise ‘no change’ and set its value to zero.

1 Like