Measure time between peaks in a graph

This seems to work on one of my binary sensors.

[{"id":"d219dae1.b6c198","type":"trigger-state","z":"ebaa69a9.649708","name":"","server":"4bbca37b.1700ec","exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityid":"binary_sensor.<Your_Sensor>","entityidfiltertype":"exact","debugenabled":false,"constraints":[{"id":"g6fxg4ibdyk","targetType":"this_entity","targetValue":"","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"off"}],"constraintsmustmatch":"all","outputs":2,"customoutputs":[],"outputinitially":false,"state_type":"str","x":250,"y":780,"wires":[["58009c5e.d64514"],[]]},{"id":"58009c5e.d64514","type":"api-get-history","z":"ebaa69a9.649708","name":"","server":"4bbca37b.1700ec","startdate":"","enddate":"","entityid":"binary_sensor.<Your_Sensor>","entityidtype":"is","useRelativeTime":false,"relativeTime":"","flatten":true,"output_type":"array","output_location_type":"msg","output_location":"payload","x":590,"y":780,"wires":[["b46cd3b0.ccca5"]]},{"id":"f61838d3.b51388","type":"inject","z":"ebaa69a9.649708","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":430,"y":840,"wires":[["58009c5e.d64514"]]},{"id":"b46cd3b0.ccca5","type":"function","z":"ebaa69a9.649708","name":"","func":"var last = msg.payload[msg.payload.length - 1].last_changed;\nvar second_to_last = msg.payload[msg.payload.length - 2].last_changed;\n\nmsg.minutes = Math.round(((new Date(last).getTime()/1000) - (new Date(second_to_last).getTime()/1000))/60);\n\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":760,"y":780,"wires":[["69aa17fd.ff1b78","adcb26bb.e663f8"]]},{"id":"69aa17fd.ff1b78","type":"debug","z":"ebaa69a9.649708","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":970,"y":820,"wires":[]},{"id":"adcb26bb.e663f8","type":"ha-entity","z":"ebaa69a9.649708","name":"Peak to peak","server":"4bbca37b.1700ec","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"peak_to_peak"},{"property":"device_class","value":""},{"property":"icon","value":""},{"property":"unit_of_measurement","value":"minutes"}],"state":"minutes","stateType":"msg","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","x":980,"y":760,"wires":[[]]},{"id":"4bbca37b.1700ec","type":"server","name":"Home Assistant","legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

The blue nodes is the ones you need to edit or in peak to peak, look if it seems correct.

You need this custom component in order to create the sensor in HA.

(this is the blue last node in the sequence)

The inject node ( the grey one) has a button on the side which forces the sequence to run and if you open the debug window then you will see the msg.minutes in the debug window, the last peak to peak time.