Runtime of heatpump to average runtime and starts of heatpump

I have a sensor which shows the run time of the heatpump:

I would like to create two helpers out of this sensor:

1: how often has the heat pump started?

To achieve this I would calculate the amount of times the value changed from 0 to some other value and count this on daily basis.

How can I achieve this with a helper?

2: what is the average runtime?

Get the maximum value before the value switches to 0 and take the average of all this values.

How can I achieve this with a helper?

This post has most of what you want.