Calculation of running time in the last 6 hours - get history

I want to run a command to check in my Hone Assistant if switch has been running for a cumulative two hours for the last 6 hours and then an automatic timeout for 6 hours.

I know the correct way is through api-get-history, but I can not summarize the run time.
I'm really happy to help.

[{"id":"b0586d5c700d873d","type":"api-get-history","z":"968b7d95.6db39","name":"","server":"ac03b698.19e7c8","version":0,"startdate":"","enddate":"","entityid":"switch.boiler","entityidtype":"is","useRelativeTime":true,"relativeTime":"6 hours","flatten":true,"output_type":"array","output_location_type":"msg","output_location":"payload","x":560,"y":700,"wires":[["9e4c4967738529f3"]]},{"id":"379edfb5b6e5fc0e","type":"inject","z":"968b7d95.6db39","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":380,"y":700,"wires":[["b0586d5c700d873d"]]},{"id":"9e4c4967738529f3","type":"debug","z":"968b7d95.6db39","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":730,"y":700,"wires":[]},{"id":"ac03b698.19e7c8","type":"server","name":"Home Assistant","version":1,"legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

In the meantime I have been trying to build something based on this

[{"id":"79ce266e56a40891","type":"trigger-state","z":"968b7d95.6db39","name":"Boiler on","server":"ac03b698.19e7c8","version":0,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityid":"switch.boiler","entityidfiltertype":"exact","debugenabled":false,"constraints":[{"targetType":"this_entity","targetValue":"","propertyType":"current_state","comparatorType":">","comparatorValueDatatype":"str","comparatorValue":"0.0","propertyValue":"new_state.state"}],"outputs":2,"customoutputs":[],"outputinitially":false,"state_type":"str","x":100,"y":500,"wires":[["8d16e77f.480b3"],["8d16e77f.480b3"]]},{"id":"fb7dd21fe21406c4","type":"api-call-service","z":"968b7d95.6db39","name":"Timeout Boiler","server":"ac03b698.19e7c8","version":3,"debugenabled":false,"service_domain":"switch","service":"turn_off","entityId":"switch.boiler","data":"","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":700,"y":540,"wires":[["458bcb237cdc2f53"]]},{"id":"e56cbc00.56df5","type":"inject","z":"968b7d95.6db39","name":"Reset","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"00 16 * * *","once":false,"onceDelay":0.1,"topic":"","payload":"reset","payloadType":"str","x":880,"y":480,"wires":[["195356b.fb27129"]]},{"id":"195356b.fb27129","type":"function","z":"968b7d95.6db39","name":"","func":"flow.set(\"counter\",0);\nflow.set(\"status\",\"off\");","outputs":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1060,"y":560,"wires":[]},{"id":"8d16e77f.480b3","type":"function","z":"968b7d95.6db39","name":"","func":"if (msg.payload == \"on\") {\n    flow.set(\"status\",\"on\");\n}\nelse if (msg.payload == \"off\") {\n    flow.set(\"status\",\"off\");\n}\n","outputs":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":300,"y":500,"wires":[]},{"id":"8f2c92c5.7b4c18","type":"inject","z":"968b7d95.6db39","name":"counter","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"1","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":100,"y":560,"wires":[["fa23b6c6.f3b2a8"]]},{"id":"fa23b6c6.f3b2a8","type":"function","z":"968b7d95.6db39","name":"","func":"var status = flow.get(\"status\") || \"on\";\nvar count  = flow.get(\"counter\") ||0;\n\nif (status == \"on\"){\n    msg.payload = count;\n    count = count + 1;\n    flow.set(\"counter\",count);\n    return msg;\n}\nelse if (status == \"off\") {\n    msg.payload = count;\n    return msg;\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":240,"y":560,"wires":[["b0bc0239db9aac61"]]},{"id":"b0bc0239db9aac61","type":"switch","z":"968b7d95.6db39","name":"","property":"payload","propertyType":"msg","rules":[{"t":"gt","v":"7210","vt":"str"},{"t":"gt","v":"900","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":390,"y":560,"wires":[["d13ea540ceecfa22"],["50cf8019934dcf1b"]]},{"id":"d13ea540ceecfa22","type":"api-current-state","z":"968b7d95.6db39","name":"Boiler on","server":"ac03b698.19e7c8","version":2,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","entity_id":"switch.boiler","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":540,"y":540,"wires":[["fb7dd21fe21406c4"],[]]},{"id":"458bcb237cdc2f53","type":"delay","z":"968b7d95.6db39","name":"","pauseType":"delay","timeout":"3","timeoutUnits":"hours","rate":"1","nbRateUnits":"3","rateUnits":"hour","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":true,"allowrate":false,"outputs":1,"x":880,"y":520,"wires":[["195356b.fb27129"]]},{"id":"50cf8019934dcf1b","type":"delay","z":"968b7d95.6db39","name":"","pauseType":"rate","timeout":"6","timeoutUnits":"hours","rate":"1","nbRateUnits":"6","rateUnits":"hour","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":true,"allowrate":false,"outputs":1,"x":580,"y":600,"wires":[["7c74683744d10982"]]},{"id":"7c74683744d10982","type":"delay","z":"968b7d95.6db39","name":"","pauseType":"delay","timeout":"6","timeoutUnits":"hours","rate":"1","nbRateUnits":"3","rateUnits":"hour","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":true,"allowrate":false,"outputs":1,"x":760,"y":600,"wires":[["195356b.fb27129"]]},{"id":"ac03b698.19e7c8","type":"server","name":"Home Assistant","version":1,"legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.