Hi All,
I have a cron-plus node that gets sunset and sunrise time and stores it into a global variable.
It has been causing me issues for months now, every time I stop and start the NR service I have to manually re-run the CRON job else I get an error when reading the stored value in the global variable.
This made no sense to me, but no matter how much I debugged it I couldn't figure out the problem.
After more time spent on it, I got one step closer. I found that even though the value of the property in my Global is identical before and after restarting NR, the code would not run because on restart the "type" of the property in the Global JSON changes from an "object" to a "string".
I have pasted the code from a mini-made-up flow below. You need cron-plus installed to run it...
[
{
"id": "651f808f980c783e",
"type": "cronplus",
"z": "1b60fd7bbe7aa8e7",
"name": "",
"outputField": "payload",
"timeZone": "Europe/London",
"persistDynamic": false,
"commandResponseMsgOutput": "output1",
"outputs": 1,
"options": [
{
"name": "schedule1",
"topic": "solarEvents",
"payloadType": "default",
"payload": "",
"expressionType": "solar",
"expression": "0 * * * * * *",
"location": "51.434320273775626 -0.1318359375",
"offset": "0",
"solarType": "selected",
"solarEvents": "sunrise,sunset"
}
],
"x": 1680,
"y": 280,
"wires": [
[
"a01603c461b77e06"
]
]
},
{
"id": "a01603c461b77e06",
"type": "function",
"z": "1b60fd7bbe7aa8e7",
"name": "Run first",
"func": "let lighting_system = {\n \"sunset\" : null,\n \"sunrise\" : null,\n}\nglobal.set(\"AB_system_lighting111\",lighting_system);\n\nlet system = global.get(\"AB_system_lighting111\")\n\nvar sunrise = msg.payload.status.solarTimes.find(_=>_.event == \"sunrise\");\nvar sunset = msg.payload.status.solarTimes.find(_=>_.event == \"sunset\");\n\nsystem.sunrise = sunrise.time\nsystem.sunset = sunset.time\n\nlet type = typeof system.sunrise\nlet type1 = typeof system.sunset\n\n node.warn([\"type output\",type, type] )",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 1840,
"y": 280,
"wires": [
[]
]
}
]
[
{
"id": "131bd77c2138a20d",
"type": "cronplus",
"z": "1b60fd7bbe7aa8e7",
"name": "",
"outputField": "payload",
"timeZone": "Europe/London",
"persistDynamic": false,
"commandResponseMsgOutput": "output1",
"outputs": 1,
"options": [
{
"name": "schedule1",
"topic": "solarEvents",
"payloadType": "default",
"payload": "",
"expressionType": "solar",
"expression": "0 * * * * * *",
"location": "51.434320273775626 -0.1318359375",
"offset": "0",
"solarType": "selected",
"solarEvents": "sunrise,sunset"
}
],
"x": 1680,
"y": 340,
"wires": [
[
"a0782992a46edfee"
]
]
},
{
"id": "a0782992a46edfee",
"type": "function",
"z": "1b60fd7bbe7aa8e7",
"name": "Run before and after restart",
"func": "let system = global.get(\"AB_system_lighting111\")\n\nlet type = typeof system.sunrise\nlet type1 = typeof system.sunset\n\n node.warn([\"type output\",type, type] )",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 1900,
"y": 340,
"wires": [
[]
]
}
]
- run the first flow, titled: "run first"
- run the second flow titled: "run before and after restart" -- to see that the properties are still objects 3. stop and start node-red
- run the second one titled: "run before and after restart" -- to see that the properties have now changed to strings!
I am running:
Cron-plus 1.5.7
NR 3.0.0