Hello eveyone.
I am struggeling creating the proper flow for my project.
I have tried using the GitHub AI to help, but it is taking forever to create a proper flow.
This is what I am trying to do:
I have more than 100 switches in my project, but for this example I only have 2.
I want the switches to react on a UDP command (string)
So when UDP in receives "10" switch_1 must turn on.
When UDP in receives "11" switch_1 must turn off.
For switch 2 i have UDP commands 11 and 12 (in my big projects thes numbers respresents trigger numbers in Pharos, a light controlling system)
Ok, This function is not so difficult , but, when I do an manua switch using the UI, the proper command must now be sent using UDP Out.
So, when an UDP command is received, the switch must be set in the proper state (also on the UI) and then stop.
When i do a manual action, it must send an UDP command, but NOT when a UDP command is received. This, to prevent a UDP loop.
I would also like to store the current state of the switches, so I can use that for further actions.
This is what i have got so far, but it is not working properly yet.
Help is most appreiciated.
[
{
"id": "c35de18abd79a888",
"type": "udp in",
"z": "8ad5300281b2ec47",
"name": "",
"iface": "",
"port": "5000",
"ipv": "udp4",
"multicast": "false",
"group": "",
"datatype": "utf8",
"x": 80,
"y": 1040,
"wires": [
[
"68d93f411feed87c",
"4c3e5cd30a82fc7b"
]
]
},
{
"id": "d0a4de3e4fe28d98",
"type": "ui_switch",
"z": "8ad5300281b2ec47",
"name": "Schakelaar_1",
"label": "Schakelaar_1",
"tooltip": "",
"group": "46a4f3965c383ef3",
"order": 0,
"width": 0,
"height": 0,
"passthru": true,
"decouple": "false",
"topic": "Schakelaar_1",
"topicType": "str",
"style": "",
"onvalue": "10",
"onvalueType": "str",
"onicon": "",
"oncolor": "",
"offvalue": "11",
"offvalueType": "str",
"officon": "",
"offcolor": "",
"animate": false,
"className": "",
"x": 300,
"y": 1020,
"wires": [
[
"2bed3e7f55348941"
]
]
},
{
"id": "b7b2a24c7dfea7d1",
"type": "ui_switch",
"z": "8ad5300281b2ec47",
"name": "Schakelaar_2",
"label": "Schakelaar_2",
"tooltip": "",
"group": "46a4f3965c383ef3",
"order": 4,
"width": 0,
"height": 0,
"passthru": true,
"decouple": "false",
"topic": "Schakelaar_2",
"topicType": "str",
"style": "",
"onvalue": "12",
"onvalueType": "str",
"onicon": "",
"oncolor": "",
"offvalue": "13",
"offvalueType": "str",
"officon": "",
"offcolor": "",
"animate": false,
"className": "",
"x": 300,
"y": 1080,
"wires": [
[
"2bed3e7f55348941"
]
]
},
{
"id": "4c3e5cd30a82fc7b",
"type": "function",
"z": "8ad5300281b2ec47",
"name": "Process UDP",
"func": "let status = context.get('status') || {};\nlet value = msg.payload;\n\n// Check if the value is already in the status object\nlet switchName = Object.keys(status).find(key => status[key] !== value);\n\nif (switchName) {\n // Update status\n status[switchName] = value;\n context.set('status', status);\n \n // Forward to the switch\n return [{...msg, topic: switchName, from_udp: true}, null];\n}\n\nreturn [null, {status: status}];",
"outputs": 2,
"noerr": 0,
"initialize": "context.set('status', { Schakelaar_1: '11', Schakelaar_2: '13' });",
"finalize": "",
"libs": [],
"x": 300,
"y": 940,
"wires": [
[
"9eefe465ecd36dd3",
"95688211c8fd81b4"
],
[
"0280c4c769840612"
]
]
},
{
"id": "2bed3e7f55348941",
"type": "function",
"z": "8ad5300281b2ec47",
"name": "After Switch",
"func": "let status = context.get('status') || {};\nlet switchName = msg.topic;\nlet value = msg.payload;\n\nif (msg.from_udp) {\n // Update the status and reset from_udp\n status[switchName] = value;\n context.set('status', status);\n msg.from_udp = false;\n // Output the status object\n node.send([null, {status: status}]);\n} else {\n // Handmatige actie, stuur UDP-bericht\n node.send([{...msg, payload: value}, {status: status}]);\n}",
"outputs": 2,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 570,
"y": 1120,
"wires": [
[
"849d2ff8ed2a1398",
"b8e6f1549bb99b16"
],
[
"0280c4c769840612"
]
]
},
{
"id": "9eefe465ecd36dd3",
"type": "trigger",
"z": "8ad5300281b2ec47",
"name": "",
"op1": "",
"op2": "",
"op1type": "pay",
"op2type": "pay",
"duration": "100",
"extend": false,
"overrideDelay": false,
"units": "ms",
"reset": "",
"bytopic": "all",
"topic": "topic",
"outputs": 1,
"x": 580,
"y": 920,
"wires": [
[
"d0a4de3e4fe28d98",
"b7b2a24c7dfea7d1",
"829c26b35f6cb579"
]
]
},
{
"id": "849d2ff8ed2a1398",
"type": "udp out",
"z": "8ad5300281b2ec47",
"name": "",
"addr": "192.168.123.250",
"iface": "",
"port": "5002",
"ipv": "udp4",
"outport": "",
"base64": false,
"multicast": "false",
"x": 1000,
"y": 1040,
"wires": []
},
{
"id": "68d93f411feed87c",
"type": "debug",
"z": "8ad5300281b2ec47",
"name": "UDP received",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 300,
"y": 1220,
"wires": []
},
{
"id": "b8e6f1549bb99b16",
"type": "debug",
"z": "8ad5300281b2ec47",
"name": "UDP Out",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 1020,
"y": 1100,
"wires": []
},
{
"id": "0280c4c769840612",
"type": "debug",
"z": "8ad5300281b2ec47",
"name": "Status Object",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "true",
"targetType": "full",
"statusVal": "",
"statusType": "auto",
"x": 1000,
"y": 1180,
"wires": []
},
{
"id": "95688211c8fd81b4",
"type": "debug",
"z": "8ad5300281b2ec47",
"name": "Process UDP Output",
"active": false,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "true",
"targetType": "full",
"statusVal": "",
"statusType": "auto",
"x": 580,
"y": 840,
"wires": []
},
{
"id": "829c26b35f6cb579",
"type": "debug",
"z": "8ad5300281b2ec47",
"name": "Trigger Output",
"active": false,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "true",
"targetType": "full",
"statusVal": "",
"statusType": "auto",
"x": 880,
"y": 840,
"wires": []
},
{
"id": "46a4f3965c383ef3",
"type": "ui_group",
"name": "GitHubTest",
"tab": "321a0aed.9d57e6",
"order": 3,
"disp": true,
"width": "6",
"collapse": false,
"className": ""
},
{
"id": "321a0aed.9d57e6",
"type": "ui_tab",
"name": "Test",
"icon": "dashboard",
"order": 11,
"disabled": false,
"hidden": false
}
]