Thank you. You confirmed my suspicion that it's garbage. Again, I know next to nothing about coding. I'm a virologist by trade. I used chatGPT for a much simpler flow before and it worked. Thought I'd try something harder.
An export of the flow is below. Maybe that flow or function might be handy for others as a template? Maybe that's asking for too much but any help there would be appreciated.
[
{
"id": "a97346c8eca08538",
"type": "tab",
"label": "Based on timestamp",
"disabled": false,
"info": "event:state (Device 1) --> Function node 1\nevent:state (Device 2) --> Function node 1\nevent:state (Device 3) --> Function node 1\nevent:state (Device 4) --> Function node 1\nevent:state (Device 5) --> Function node 1\nFunction node 1 --> Function node 1.5\nFunction node 1.5 --> Function node 2\n",
"env": []
},
{
"id": "547dcf8b572a2d08",
"type": "server-state-changed",
"z": "a97346c8eca08538",
"name": "SPA2",
"server": "a7b15a72.3e9048",
"version": 4,
"exposeToHomeAssistant": false,
"haConfig": [
{
"property": "name",
"value": ""
},
{
"property": "icon",
"value": ""
}
],
"entityidfilter": "sensor.kauf_plug_current_2",
"entityidfiltertype": "exact",
"outputinitially": false,
"state_type": "num",
"haltifstate": "",
"halt_if_type": "num",
"halt_if_compare": "is",
"outputs": 1,
"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": "device",
"propertyType": "msg",
"value": "SPA2",
"valueType": "str"
}
],
"x": 90,
"y": 100,
"wires": [
[
"13301a4b3ff36bcd"
]
]
},
{
"id": "4a81f00ee9ef624f",
"type": "server-state-changed",
"z": "a97346c8eca08538",
"name": "SPA1",
"server": "a7b15a72.3e9048",
"version": 4,
"exposeToHomeAssistant": false,
"haConfig": [
{
"property": "name",
"value": ""
},
{
"property": "icon",
"value": ""
}
],
"entityidfilter": "sensor.kauf_plug_current",
"entityidfiltertype": "exact",
"outputinitially": false,
"state_type": "num",
"haltifstate": "",
"halt_if_type": "str",
"halt_if_compare": "is",
"outputs": 1,
"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": "device",
"propertyType": "msg",
"value": "SPA1",
"valueType": "str"
}
],
"x": 90,
"y": 40,
"wires": [
[
"13301a4b3ff36bcd"
]
]
},
{
"id": "13301a4b3ff36bcd",
"type": "function",
"z": "a97346c8eca08538",
"name": "Identify offending device",
"func": "// Check if msg object exists\nif (msg) {\n // Parse the message object\n var deviceCurrent = msg.payload; // Current usage reported by the device\n var deviceToTurnOn = context.get('deviceToTurnOn');\n var totalCurrent = context.get('totalCurrent') || 0;\n\n // Calculate total current usage\n totalCurrent += deviceCurrent;\n\n // Check for overload condition\n if (totalCurrent > 20) {\n var deviceToTurnOff = msg.device;\n var currentNeeded = deviceCurrent;\n\n // Turn off the device\n msg.payload = {\n domain: 'switch',\n service: 'toggle',\n data: {\n entity_id: deviceToTurnOff\n }\n };\n\n // Include overload information in the message\n msg.overload = true;\n msg.offendingDevice = deviceToTurnOff;\n } else {\n // Reset deviceToTurnOff and currentNeeded if no overload\n deviceToTurnOff = null;\n currentNeeded = null;\n\n // Include copacetic information in the message\n msg.overload = false;\n }\n\n // Track current usage for each device\n context.set(msg.device, deviceCurrent);\n\n // Restore power to the device\n if (deviceToTurnOn && totalCurrent >= deviceToTurnOn) {\n msg.payload = {\n domain: 'switch',\n service: 'toggle',\n data: {\n entity_id: deviceToTurnOn\n }\n };\n deviceToTurnOn = null; // Reset the device to turn on\n }\n\n // Include total current in the message\n msg.totalCurrent = totalCurrent;\n\n // Store variables in context\n context.set('deviceToTurnOn', deviceToTurnOn);\n context.set('totalCurrent', totalCurrent);\n}\n\nreturn msg; // Output the message\n",
"outputs": 1,
"noerr": 0,
"initialize": "// Step 4: Calculate total current usage\nvar totalCurrent = context.get('totalCurrent') || 0;\nvar deviceCurrent = msg.payload; // Current usage reported by the device\ntotalCurrent += deviceCurrent;\n\n// Step 5: Check for overload condition\nif (totalCurrent > 20) {\n var deviceToTurnOff = msg.device;\n var currentNeeded = deviceCurrent;\n\n // Step 6: Turn off the device\n msg.payload = {\n domain: 'switch',\n service: 'turn_off',\n data: {\n entity_id: deviceToTurnOff\n }\n };\n\n // Include overload information in the message\n msg.overload = true;\n msg.offendingDevice = deviceToTurnOff;\n} else {\n // Reset deviceToTurnOff and currentNeeded if no overload\n context.set('deviceToTurnOff', null);\n context.set('currentNeeded', null);\n\n // Include copacetic information in the message\n msg.overload = false;\n}\n\n// Step 7: Track current usage for each device\ncontext.set(msg.device, deviceCurrent);\n\n// Step 8: Restore power to the device\nvar deviceToTurnOn = context.get('deviceToTurnOn');\nif (deviceToTurnOn && totalCurrent >= deviceToTurnOn) {\n msg.payload = {\n domain: 'switch',\n service: 'turn_on',\n data: {\n entity_id: deviceToTurnOn\n }\n };\n context.set('deviceToTurnOn', null); // Reset the device to turn on\n}\n\n// Include total current in the message\nmsg.totalCurrent = totalCurrent;\n\nreturn msg; // Output the message\n",
"finalize": "",
"libs": [],
"x": 430,
"y": 180,
"wires": [
[
"87c348af35da8e64"
]
]
},
{
"id": "69dc4d741b0ed124",
"type": "api-call-service",
"z": "a97346c8eca08538",
"name": "Toggle SPA1",
"server": "7f820507.34c1cc",
"version": 5,
"debugenabled": false,
"domain": "switch",
"service": "toggle",
"areaId": [],
"deviceId": [
"4f28887c7842dfba82c9c8bad769438f"
],
"entityId": [
"switch.kauf_plug"
],
"data": "",
"dataType": "jsonata",
"mergeContext": "",
"mustacheAltTags": false,
"outputProperties": [],
"queue": "none",
"x": 590,
"y": 280,
"wires": [
[]
]
},
{
"id": "87c348af35da8e64",
"type": "switch",
"z": "a97346c8eca08538",
"name": "",
"property": "device",
"propertyType": "msg",
"rules": [
{
"t": "eq",
"v": "SPA1",
"vt": "str"
},
{
"t": "eq",
"v": "SPA2",
"vt": "str"
},
{
"t": "eq",
"v": "SPA3",
"vt": "str"
}
],
"checkall": "true",
"repair": false,
"outputs": 3,
"x": 390,
"y": 340,
"wires": [
[
"69dc4d741b0ed124"
],
[
"5b1f69d8ef405e85"
],
[
"f3fbc2431fa2e4b8"
]
]
},
{
"id": "5b1f69d8ef405e85",
"type": "api-call-service",
"z": "a97346c8eca08538",
"name": "Toggle SPA1",
"server": "7f820507.34c1cc",
"version": 5,
"debugenabled": false,
"domain": "switch",
"service": "toggle",
"areaId": [],
"deviceId": [
"811926f05efcc93f9f2e9f21dbb95efe"
],
"entityId": [
"switch.kauf_plug_2"
],
"data": "",
"dataType": "jsonata",
"mergeContext": "",
"mustacheAltTags": false,
"outputProperties": [],
"queue": "none",
"x": 590,
"y": 340,
"wires": [
[]
]
},
{
"id": "2059234c12c52fec",
"type": "server-state-changed",
"z": "a97346c8eca08538",
"name": "SPA3. Not configured yet.",
"server": "7f820507.34c1cc",
"version": 4,
"exposeToHomeAssistant": false,
"haConfig": [
{
"property": "name",
"value": ""
},
{
"property": "icon",
"value": ""
}
],
"entityidfilter": "",
"entityidfiltertype": "exact",
"outputinitially": false,
"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": 200,
"wires": [
[
"13301a4b3ff36bcd"
]
]
},
{
"id": "f3fbc2431fa2e4b8",
"type": "api-call-service",
"z": "a97346c8eca08538",
"name": "SPA3. not configured yet",
"server": "7f820507.34c1cc",
"version": 5,
"debugenabled": false,
"domain": "",
"service": "",
"areaId": [],
"deviceId": [],
"entityId": [],
"data": "",
"dataType": "jsonata",
"mergeContext": "",
"mustacheAltTags": false,
"outputProperties": [],
"queue": "none",
"x": 630,
"y": 400,
"wires": [
[]
]
},
{
"id": "a7b15a72.3e9048",
"type": "server",
"name": "Home Assistant",
"version": 5,
"addon": true,
"rejectUnauthorizedCerts": true,
"ha_boolean": "y|yes|true|on|home|open",
"connectionDelay": true,
"cacheJson": true,
"heartbeat": false,
"heartbeatInterval": 30,
"areaSelector": "friendlyName",
"deviceSelector": "friendlyName",
"entitySelector": "friendlyName",
"statusSeparator": "at: ",
"statusYear": "hidden",
"statusMonth": "short",
"statusDay": "numeric",
"statusHourCycle": "h23",
"statusTimeFormat": "h:m",
"enableGlobalContextStore": true
},
{
"id": "7f820507.34c1cc",
"type": "server",
"name": "Home Assistant",
"addon": true
}
]