[{"id":"c68594981cf4df93","type":"server-state-changed","z":"e1f44cc40f36d3a0","name":"Update Alarm","version":3,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"_prossimo_allarme_offset, _prossimo_allarme_enabled, _prossimo_allarme","entityidfiltertype":"substring","outputinitially":true,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"for":"0","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"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":130,"y":1000,"wires":[["bd2261de4c7422c2"]]},{"id":"bd2261de4c7422c2","type":"function","z":"e1f44cc40f36d3a0","name":"Save values","func":"const [, phone] = msg.topic.match(/\\.(.+)_prossimo_allarme/);\nconst key = `alarm['${phone}']`;\nconst fullKey = `alarm['${phone}']['${msg.topic}']`\nflow.set(fullKey, msg.payload);\nconst alarmPhone = flow.get(key);\nif (Object.keys(alarmPhone).length === 3) {\n    const alarmRaw = alarmPhone[`sensor.${phone}_prossimo_allarme`];\n    const enabled = alarmPhone[`input_boolean.${phone}_prossimo_allarme_enabled`] === 'on';\n    if ( enabled && alarmRaw !== \"unavailable\") {\n        const now = Date.now();\n        const alarm = new Date(alarmRaw);\n        const offset = alarmPhone[`input_number.${phone}_prossimo_allarme_offset`] * 60000;\n        const timeDifference = alarm.getTime() + offset;\n        const delay = timeDifference - now;\n\n        if (delay < 0) {\n            node.status({ fill: 'red', text: 'Offset put alarm in the past' });\n            node.warn(\"Offset put alarm in the past.\");\n            return { reset: true };\n        }\n\n        node.status({});\n        msg = {\n            topic: phone,\n            delay,\n            payload: alarm\n        };\n\n        // Reset the delay node before setting the new delay\n        return [[{ reset: true }, msg]];\n    } else {\n        return { reset: true };\n    }\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":330,"y":1000,"wires":[["31a1b9a97061185e"]]},{"id":"31a1b9a97061185e","type":"delay","z":"e1f44cc40f36d3a0","name":"wait until time","pauseType":"delayv","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":520,"y":1000,"wires":[["35675e2e29e54de6"]]},{"id":"764b91777c83fd57","type":"debug","z":"e1f44cc40f36d3a0","name":"do stuff","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":320,"y":1080,"wires":[]},{"id":"35675e2e29e54de6","type":"switch","z":"e1f44cc40f36d3a0","name":"Phone","property":"topic","propertyType":"msg","rules":[{"t":"eq","v":"pixel_2_xl","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":690,"y":1000,"wires":[["b2992c1a93db3836"]]},{"id":"b2992c1a93db3836","type":"link out","z":"e1f44cc40f36d3a0","name":"Pixel 2 XL","mode":"link","links":["2a280002119b6339"],"x":795,"y":1000,"wires":[]},{"id":"2a280002119b6339","type":"link in","z":"e1f44cc40f36d3a0","name":"Alarm Pixel 2 XL","links":["b2992c1a93db3836"],"x":175,"y":1080,"wires":[["764b91777c83fd57"]]}]
