Sorry folks:
..... I'm ba-ack.....!
I've recently been working on a subflow
and made a lot of silly mistakes.
(I'll cop that.)
So entered the command correctly and all should be good - yeah?
Nah.
One machine it works, the other machine, doesn't.
Details:
NR 3.0.2
node -v 16.20.02
both machines.
So what's going on?
The error I get is:
{"message":"Invalid JSONata expression: Cannot read properties of undefined (reading 'startsWith')","source":{"id":"da42a7e9064de621","type":"change","name":"#1","count":1}}
What throws me is:
undefined (reading 'startsWith')
It is a change
node.
(code)
[
{
"id": "27d2dd9a875ab1a5",
"type": "change",
"z": "94cf2ea396e5b6f6",
"name": "#1",
"rules": [
{
"t": "set",
"p": "topic",
"pt": "msg",
"to": "DN",
"tot": "env"
},
{
"t": "set",
"p": "topic",
"pt": "msg",
"to": "topic & '/cmnd/power' & $env(power)",
"tot": "jsonata"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 1080,
"y": 160,
"wires": [
[]
]
}
]
There's no startingwith( ) in there.
So I'm guessing it is a syntactical problem.
For the sake of sharing:
The code - no GUI stuff or MQTT nodes.
But the guts of the code:
[{"id":"94cf2ea396e5b6f6","type":"subflow","name":"MQTT Button Toggle","info":"# 2023 02 08\nAdded status text to icon to \nshow status of device.\n\n# 2023 01 25\nControl MQTT devices (power points)\nwith this node.\n\n`Button` node MUST have `topic` set to include\nthe device's name. eg: `A1/button`\nand the payload MUST be `X`\n\nSet the `Device Name` to (eg) `A1` to\ncontrol `A1` device.\n\n`payload` must be `X` to TOGGLE output\n\nsend `ON` to force ON state\n\nsend `OFF` to force OFF state\n\nsend `RELOAD` to force button to\ndisplay current state.\n(added 2024 01 28)\n\n","category":"","in":[{"x":100,"y":160,"wires":[{"id":"0cc9b2fa9b2af8df"}]}],"out":[{"x":1280,"y":220,"wires":[{"id":"66fb54e88f4acb8d","port":0}]},{"x":1250,"y":160,"wires":[{"id":"27d2dd9a875ab1a5","port":0}]}],"env":[{"name":"DN","type":"str","value":"","ui":{"label":{"en-US":"Device Name"},"type":"input","opts":{"types":["str"]}}},{"name":"power","type":"num","value":"","ui":{"label":{"en-US":"Output"},"type":"input","opts":{"types":["num"]}}}],"meta":{},"color":"#DDAA99","inputLabels":["MQTT (tele and LWT) and button input"],"outputLabels":["Back to `button` input","MQTT output"],"status":{"x":1200,"y":270,"wires":[{"id":"45a85f7621f5c28f","port":0}]}},{"id":"541720ad73579447","type":"switch","z":"94cf2ea396e5b6f6","name":"what message?","property":"topic","propertyType":"msg","rules":[{"t":"cont","v":"/stat/POWER","vt":"str"},{"t":"cont","v":"/tele/LWT","vt":"str"},{"t":"cont","v":"button","vt":"str"}],"checkall":"true","repair":false,"outputs":3,"x":315,"y":160,"wires":[["61843ca1684f9f9d"],["4788b5265af1d45a"],["5b8cd9fe254ea8d1"]],"l":false},{"id":"61843ca1684f9f9d","type":"delay","z":"94cf2ea396e5b6f6","name":"Delay","pauseType":"delay","timeout":"200","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":460,"y":80,"wires":[["bdb2c46daf62b36f"]]},{"id":"6d4704f6fc9b98be","type":"change","z":"94cf2ea396e5b6f6","name":"RESET","rules":[{"t":"set","p":"payload","pt":"msg","to":"RESET","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":470,"y":120,"wires":[["bdb2c46daf62b36f"]]},{"id":"4788b5265af1d45a","type":"switch","z":"94cf2ea396e5b6f6","name":"LWT","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"Online","vt":"str"},{"t":"eq","v":"Offline","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":460,"y":160,"wires":[["6d4704f6fc9b98be","bc2aabd04561d534"],["6a82c12953657965"]]},{"id":"6a82c12953657965","type":"function","z":"94cf2ea396e5b6f6","name":"BAN","func":"msg = {icon: '<font color = \"red\"><i class=\"fa fa-ban fa-3x\"></i></font>'};\nreturn msg;","outputs":1,"noerr":0,"x":460,"y":200,"wires":[["8dc8a0dd2b669ae3","704ae850657c9724"]]},{"id":"bc2aabd04561d534","type":"change","z":"94cf2ea396e5b6f6","name":"Enable","rules":[{"t":"set","p":"enabled","pt":"msg","to":"true","tot":"bool"},{"t":"set","p":"ARLEC-1","pt":"flow","to":"ONLINE","tot":"str"},{"t":"delete","p":"payload","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":660,"y":160,"wires":[["66fb54e88f4acb8d"]]},{"id":"bdb2c46daf62b36f","type":"function","z":"94cf2ea396e5b6f6","name":"Set state","func":"let device = msg.topic.split(\"/\");\nnode.warn(\"Device \" + device[0]);\nnode.warn(\"Payload \" + msg.payload);\n\nif (msg.payload == \"RESET\")\n{\n let state = context.get(device[0]);\n if (state === 0)\n {\n //\n msg= {\"icon\": '<font color = \"red\"><i class=\"fa fa-toggle-on fa-rotate-270 fa-3x\"></i></font>',\"background\":\"black\"};\n return msg;\n }\n //\n}\nif (msg.payload == \"ON\")\n{\n msg = { \"icon\": '<font color = \"lime\"><i class=\"fa fa-toggle-on fa-rotate-90 fa-3x\"></i></font>', \"background\": \"black\" };\n context.set(\"STATE\",1);\n context.set(device[0],1);\n} else\n{\n msg = { \"icon\": '<font color = \"red\"><i class=\"fa fa-toggle-on fa-rotate-270 fa-3x\"></i></font>', \"background\": \"black\" };\n context.set(\"STATE\",0);\n context.set(device[0], 1);\n}\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":660,"y":120,"wires":[["66fb54e88f4acb8d"]]},{"id":"8dc8a0dd2b669ae3","type":"change","z":"94cf2ea396e5b6f6","name":"Disable","rules":[{"t":"set","p":"enabled","pt":"msg","to":"false","tot":"bool"},{"t":"set","p":"ARLEC-1","pt":"flow","to":"OFFLINE","tot":"str"},{"t":"delete","p":"payload","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":660,"y":200,"wires":[["66fb54e88f4acb8d"]]},{"id":"5b8cd9fe254ea8d1","type":"function","z":"94cf2ea396e5b6f6","name":"Button control","func":"let msg1 = {};\n\nlet x = msg.payload;\nlet counter = context.get(\"counter\") || 0;\nif (x == \"X\")\n{\n counter = (counter + 1) % 2;\n context.set(\"counter\",counter);\n if (counter === 0)\n {\n //\n msg.payload = \"OFF\";\n msg1 = {\"icon\": '<font color = \"orangered\"><i class=\"fa fa-toggle-on fa-rotate-270 fa-3x\"></i></font>',\"background\":\"black\"};\n } else\n {\n //\n msg.payload = \"ON\";\n msg1 = {\"icon\": '<font color = \"orangered\"><i class=\"fa fa-toggle-on fa-rotate-90 fa-3x\"></i></font>',\"backgound\":\"black\"};\n }\n}\nif (x == \"RELOAD\")\n{\n // Reload/display status.\n if (counter == 0)\n {\n // OFF\n x = \"OFF\";\n msg.payload = \"OFF\";\n msg1 = { \"icon\": '<font color = \"orangered\"><i class=\"fa fa-toggle-on fa-rotate-270 fa-3x\"></i></font>', \"background\": \"black\" };\n }\n else\n if (counter == 1)\n {\n // ON\n x = \"ON\";\n msg.payload = \"ON\";\n msg1 = { \"icon\": '<font color = \"orangered\"><i class=\"fa fa-toggle-on fa-rotate-90 fa-3x\"></i></font>', \"backgound\": \"black\" };\n }\n}\n\n\nif (x == \"ON\")\n{\n // Force power ON\n msg.payload = \"ON\";\n msg1 = { \"icon\": '<font color = \"orangered\"><i class=\"fa fa-toggle-on fa-rotate-90 fa-3x\"></i></font>', \"backgound\": \"black\" };\n context.set(\"counter\",1);\n}\nif (x == \"OFF\")\n{\n // Force power OFF\n msg.payload = \"OFF\";\n msg1 = { \"icon\": '<font color = \"orangered\"><i class=\"fa fa-toggle-on fa-rotate-90 fa-3x\"></i></font>', \"backgound\": \"black\" };\n context.set(\"counter\",0);\n}\n\nreturn [msg,msg1];","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":870,"y":120,"wires":[["4f29a481b6f0d0e3","0ae8f43ccf9a1016"],["66fb54e88f4acb8d"]]},{"id":"4f29a481b6f0d0e3","type":"delay","z":"94cf2ea396e5b6f6","name":"Delay","pauseType":"delay","timeout":"100","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"outputs":1,"x":1080,"y":120,"wires":[["27d2dd9a875ab1a5"]]},{"id":"27d2dd9a875ab1a5","type":"change","z":"94cf2ea396e5b6f6","name":"#1","rules":[{"t":"set","p":"topic","pt":"msg","to":"DN","tot":"env"},{"t":"set","p":"topic","pt":"msg","to":"topic & '/cmnd/power' & $env(power)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":1080,"y":160,"wires":[[]]},{"id":"7207fbe19cc07923","type":"switch","z":"94cf2ea396e5b6f6","name":"Only for this device","property":"topic","propertyType":"msg","rules":[{"t":"cont","v":"DN","vt":"env"},{"t":"eq","v":"MASTER","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":255,"y":160,"wires":[["541720ad73579447"],["6a82c12953657965"]],"l":false},{"id":"0ae8f43ccf9a1016","type":"function","z":"94cf2ea396e5b6f6","name":"Identify","func":"let dev = env.get(\"DN\") + \" \" + env.get(\"power\");\nlet status = msg.payload;\n\nnode.status(dev + \" \" + status);","outputs":1,"noerr":0,"initialize":"// Code added here will be run once\n// whenever the node is started.\nnode.status(env.get(\"DN\"));\n","finalize":"","libs":[],"x":660,"y":270,"wires":[[]]},{"id":"45a85f7621f5c28f","type":"status","z":"94cf2ea396e5b6f6","name":"","scope":["0ae8f43ccf9a1016"],"x":1070,"y":270,"wires":[[]]},{"id":"704ae850657c9724","type":"change","z":"94cf2ea396e5b6f6","name":"Wipe status when unplugged","rules":[{"t":"set","p":"payload","pt":"msg","to":" ","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":425,"y":270,"wires":[["0ae8f43ccf9a1016"]],"l":false},{"id":"0cc9b2fa9b2af8df","type":"switch","z":"94cf2ea396e5b6f6","name":"Reload?","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"RELOAD","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":175,"y":160,"wires":[["5b8cd9fe254ea8d1"],["7207fbe19cc07923"]],"l":false},{"id":"66fb54e88f4acb8d","type":"junction","z":"94cf2ea396e5b6f6","x":800,"y":220,"wires":[[]]},{"id":"e72bae9952eddd60","type":"inject","z":"26262ba1.62dcbc","name":"Button","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"A5/button","payload":"X","payloadType":"str","x":1530,"y":4730,"wires":[["76117c24ef0f5cf3"]]},{"id":"76117c24ef0f5cf3","type":"link out","z":"26262ba1.62dcbc","name":"Button","mode":"link","links":["bfdb3c588016491b"],"x":1625,"y":4770,"wires":[]},{"id":"bfdb3c588016491b","type":"link in","z":"26262ba1.62dcbc","name":"Button","links":["76117c24ef0f5cf3"],"x":1035,"y":4840,"wires":[["7ebd5a2bb58fd95c"]]},{"id":"9508b1d704f5549d","type":"subflow:94cf2ea396e5b6f6","z":"26262ba1.62dcbc","name":"","env":[{"name":"DN","value":"A5","type":"str"},{"name":"power","value":"1","type":"num"},{"name":"Device name","value":"A1","type":"str"}],"x":1260,"y":4800,"wires":[["099a494a54542f2d","3b578a30c600e702"],["e87f2ab9ebfdeb7f"]]}]
On machine 1 I press the inject
and the subflow works.
On machine 2 I press the inject
and get the error.
Note:
the node-status
changes on the faulty machine though. So all is good to there.
P.P.S
If I change that change
node to this:
[
{
"id": "27d2dd9a875ab1a5",
"type": "change",
"z": "94cf2ea396e5b6f6",
"name": "#1",
"rules": [
{
"t": "set",
"p": "topic",
"pt": "msg",
"to": "DN",
"tot": "env"
},
{
"t": "set",
"p": "power",
"pt": "msg",
"to": "power",
"tot": "env"
},
{
"t": "set",
"p": "topic",
"pt": "msg",
"to": "topic & '/cmnd/power' & power",
"tot": "jsonata"
},
{
"t": "delete",
"p": "power",
"pt": "msg"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 1080,
"y": 160,
"wires": [
[]
]
}
]
No error.
P.P.P.S.
Just checked.
the change
node on both machines: same version.
3.0.2
Sorry folks, I seem to be missing something.