Hello,
Please help me with this flow. I cannot understand why the function "camera state" never goes through the first output when the condition is not met. The idea is to put a timer and, when the timer is reached, turn off the light. I know it could be done directly from the state node, but I need the timer variable to depend on other factors, so I need to write a custom function.
[{"id":"b84d4a9292736038","type":"server-state-changed","z":"66e8f946d2d274c4","name":"Camera","server":"5f04c7c3.35aa98","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"light.luci_camera","entityidfiltertype":"exact","outputinitially":false,"state_type":"habool","haltifstate":"true","halt_if_type":"bool","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"for":"","forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"}],"x":670,"y":1280,"wires":[["28523199ac937e5c"],[]]},{"id":"acd2084c1e178df0","type":"api-call-service","z":"66e8f946d2d274c4","name":"Camera OFF","server":"5f04c7c3.35aa98","version":5,"debugenabled":false,"domain":"light","service":"turn_off","areaId":[],"deviceId":[],"entityId":[],"data":"","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1430,"y":1300,"wires":[[]]},{"id":"c834634c5948518c","type":"api-current-state","z":"66e8f946d2d274c4","name":"Check","server":"5f04c7c3.35aa98","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"light.luci_camera","state_type":"habool","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":950,"y":1280,"wires":[["412ab48095d319c0"]]},{"id":"90e2c490bd435d56","type":"delay","z":"66e8f946d2d274c4","name":"","pauseType":"delay","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":940,"y":1200,"wires":[["c834634c5948518c"]]},{"id":"28523199ac937e5c","type":"function","z":"66e8f946d2d274c4","name":"SetTime","func":"var lastChange = global.set('lastChange', new Date().getTime());\nvar timeout = global.set('timeout', 5000);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":800,"y":1280,"wires":[["c834634c5948518c"]]},{"id":"412ab48095d319c0","type":"function","z":"66e8f946d2d274c4","name":"Check Camera State","func":"var currentState = msg.payload;\nnode.warn(currentState);\nvar lastChange = global.get('lastChange');// || new Date().getTime();\nvar timeout = global.get('timeout') || 5000; // Timeout in milliseconds\nnode.warn(\"Timeout: \"+ timeout);\nnode.warn(\"Last Change: \"+lastChange);\nif (currentState) {\n var currentTime = new Date().getTime();\n node.warn(\"currentTime: \" + currentTime);\n var elapsedTime = currentTime - lastChange; // in milliseconds\n node.warn(\"elapsedTime :\" + elapsedTime);\n if (elapsedTime > timeout) {\n return [null, { payload: { data: { entity_id: 'light.luci_camera' } } }]; // payload to turn off the light\n }\n} else {\n node.warn(\"Timeout not reached\");\n //global.set('lastChange', new Date().getTime());\n return [msg, null];\n}\n\nreturn [null, null];","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1160,"y":1280,"wires":[["90e2c490bd435d56","269eda0ba0a23e24"],["acd2084c1e178df0"]],"outputLabels":["","Turn OFF"]},{"id":"269eda0ba0a23e24","type":"debug","z":"66e8f946d2d274c4","name":"Output","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1410,"y":1220,"wires":[]},{"id":"5f04c7c3.35aa98","type":"server","name":"Home Assistant","addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"","connectionDelay":false,"cacheJson":false,"heartbeat":false,"heartbeatInterval":"","statusSeparator":"","enableGlobalContextStore":false}]