dirkjanfaber / node-threshold-control

Node-RED threshold control
MIT License
1 stars 2 forks source link

Geyser Control with AND operator #1

Closed Johan-Bo closed 9 months ago

Johan-Bo commented 9 months ago

Hi Dirkjanfaber

I want to know how can I use the "AND" operator in NodeRed to check what my PV Yield is as well as SOC. If my PV is above 4000Watt and my battery is above 40% I want my geyser to switch on via my shelly device. Can you maybe assist me with this? I have tried making a "AND" Javascript in the function Node but It won't work, I don't know how to fix this.

Kind Regards,

dirkjanfaber commented 9 months ago

Problem is that you need the 2 inputs at the same time, while Node-RED typically uses a single msg.payload when running. This can be solved by storing the values into context.

Taking a look at this example for inspiration. The new flow looks like this:

image

The first group stores the current values of the SOC and PV power into a variable. The inject node checks. every 15 minutes if the conditions are met. If so, it steers the relay (which you need to modify to steer the Shelly).

Once you have it deployed, you can check the context menu for the current values:

image

The full (example) flow becomes:

[{"id":"5b6dddc274a79bc6","type":"group","z":"b2f8921e4d83919e","style":{"stroke":"#999999","stroke-opacity":"1","fill":"none","fill-opacity":"1","label":true,"label-position":"nw","color":"#a4a4a4"},"nodes":["6f5910b8ec1ace37","3039a8bf46712c06","876bedb31b400764","0953515881ecd7f8"],"x":34,"y":79,"w":572,"h":122},{"id":"6f5910b8ec1ace37","type":"victron-input-battery","z":"b2f8921e4d83919e","g":"5b6dddc274a79bc6","service":"com.victronenergy.battery/256","path":"/Soc","serviceObj":{"service":"com.victronenergy.battery/256","name":"BMV-702"},"pathObj":{"path":"/Soc","type":"float","name":"State of charge (%)"},"initial":"","name":"","onlyChanges":true,"roundValues":"0","x":180,"y":120,"wires":[["3039a8bf46712c06"]]},{"id":"3039a8bf46712c06","type":"change","z":"b2f8921e4d83919e","g":"5b6dddc274a79bc6","name":"Store SOC","rules":[{"t":"set","p":"SOC","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":490,"y":120,"wires":[[]]},{"id":"876bedb31b400764","type":"victron-input-pvinverter","z":"b2f8921e4d83919e","g":"5b6dddc274a79bc6","service":"com.victronenergy.pvinverter/32","path":"/Ac/Power","serviceObj":{"service":"com.victronenergy.pvinverter/32","name":"PV inverter on input 1"},"pathObj":{"path":"/Ac/Power","type":"float","name":"Total Power (W)"},"name":"","onlyChanges":true,"roundValues":"0","x":210,"y":160,"wires":[["0953515881ecd7f8"]]},{"id":"0953515881ecd7f8","type":"change","z":"b2f8921e4d83919e","g":"5b6dddc274a79bc6","name":"Store PV power","rules":[{"t":"set","p":"pv_power","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":500,"y":160,"wires":[[]]},{"id":"7e82f60bd826ca47","type":"group","z":"b2f8921e4d83919e","style":{"stroke":"#999999","stroke-opacity":"1","fill":"none","fill-opacity":"1","label":true,"label-position":"nw","color":"#a4a4a4"},"nodes":["cf9d688b7ae528a7","3788e2040212ca78","38217b011e840726","a57361c263f31345","f5ce302d70b73b1e"],"x":34,"y":219,"w":892,"h":142},{"id":"cf9d688b7ae528a7","type":"inject","z":"b2f8921e4d83919e","g":"7e82f60bd826ca47","name":"Check each 5 minutes between 8.00 and 20.00","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"*/5 8-19 * * *","once":false,"onceDelay":0.1,"topic":"Start geyser","payload":"","payloadType":"date","x":260,"y":260,"wires":[["3788e2040212ca78"]]},{"id":"3788e2040212ca78","type":"switch","z":"b2f8921e4d83919e","g":"7e82f60bd826ca47","name":"Continue if SOC is <= 40 %","property":"SOC","propertyType":"flow","rules":[{"t":"lte","v":"40","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":600,"y":260,"wires":[["a57361c263f31345"],[]],"outputLabels":["Start generator","Do not start generator"]},{"id":"38217b011e840726","type":"trigger","z":"b2f8921e4d83919e","g":"7e82f60bd826ca47","name":"Run for 15 minutes","op1":"1","op2":"0","op1type":"num","op2type":"num","duration":"15","extend":false,"overrideDelay":false,"units":"min","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":470,"y":320,"wires":[["f5ce302d70b73b1e"]]},{"id":"a57361c263f31345","type":"switch","z":"b2f8921e4d83919e","g":"7e82f60bd826ca47","name":"Continue if PV power <= 4000","property":"pv_power","propertyType":"flow","rules":[{"t":"lte","v":"4000","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":210,"y":320,"wires":[["38217b011e840726"],[]],"outputLabels":["Start generator",null]},{"id":"f5ce302d70b73b1e","type":"victron-output-relay","z":"b2f8921e4d83919e","g":"7e82f60bd826ca47","service":"com.victronenergy.system/0","path":"/Relay/0/State","serviceObj":{"service":"com.victronenergy.system/0","name":"Venus device"},"pathObj":{"path":"/Relay/0/State","type":"enum","name":"Venus relay 1 state","enum":{"0":"Open","1":"Closed"},"writable":true},"initial":"","name":"","onlyChanges":false,"x":760,"y":320,"wires":[]}]

You still need to make some adjustments for your situation, but I am sure you'll manage.

Johan-Bo commented 7 months ago

@dirkjanfaber Please see attached my nodered to solve the issue I had. Thanks for the advice and help.

[
    {
        "id": "369b878858d61cf7",
        "type": "tab",
        "label": "Flow 1",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "947189c494a069cb",
        "type": "change",
        "z": "369b878858d61cf7",
        "name": "Gen Signal",
        "rules": [
            {
                "t": "set",
                "p": "Gen",
                "pt": "flow",
                "to": "payload",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 390,
        "y": 880,
        "wires": [
            [
                "5c0e95e5351d30c4"
            ]
        ]
    },
    {
        "id": "5c0e95e5351d30c4",
        "type": "debug",
        "z": "369b878858d61cf7",
        "name": "Debug  - Gen ON/OFF",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 660,
        "y": 860,
        "wires": []
    },
    {
        "id": "79d40481b29f3be5",
        "type": "function",
        "z": "369b878858d61cf7",
        "name": "relay on - Gen Start",
        "func": "msg.payload = {\n    type : 'relay',\n    channel : 0,\n    \n    id : 'a8032ab81aa4',\n    turn : 'on',\n};\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1270,
        "y": 1080,
        "wires": [
            [
                "9c5854a70da5ca70",
                "5faea0d370d373b9"
            ]
        ]
    },
    {
        "id": "29f0271bcfcce405",
        "type": "function",
        "z": "369b878858d61cf7",
        "name": "relay off - Gen Stop",
        "func": "msg.payload = {\n    type : 'relay',\n    channel : 0,\n    \n    id : 'a8032ab81aa4',\n    turn : 'off',\n};\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1250,
        "y": 1280,
        "wires": [
            [
                "11752255028805fb",
                "5faea0d370d373b9"
            ]
        ]
    },
    {
        "id": "9c5854a70da5ca70",
        "type": "debug",
        "z": "369b878858d61cf7",
        "name": "Gen Start",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 1520,
        "y": 1080,
        "wires": []
    },
    {
        "id": "11752255028805fb",
        "type": "debug",
        "z": "369b878858d61cf7",
        "name": "Gen off",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 1520,
        "y": 1280,
        "wires": []
    },
    {
        "id": "cf9d688b7ae528a7",
        "type": "inject",
        "z": "369b878858d61cf7",
        "name": "Watchdog Pulse",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "15",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "Start generator",
        "payload": "",
        "payloadType": "date",
        "x": 210,
        "y": 1240,
        "wires": [
            [
                "3788e2040212ca78",
                "b501da9f12e18eb4"
            ]
        ]
    },
    {
        "id": "3039a8bf46712c06",
        "type": "change",
        "z": "369b878858d61cf7",
        "name": "Store Grid condition",
        "rules": [
            {
                "t": "set",
                "p": "Gridd",
                "pt": "flow",
                "to": "payload",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1240,
        "y": 380,
        "wires": [
            [
                "e6063520689ade46"
            ]
        ]
    },
    {
        "id": "3788e2040212ca78",
        "type": "switch",
        "z": "369b878858d61cf7",
        "name": "Start if Grid loss",
        "property": "Gridd",
        "propertyType": "flow",
        "rules": [
            {
                "t": "eq",
                "v": "0",
                "vt": "num"
            },
            {
                "t": "neq",
                "v": "0",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 460,
        "y": 1180,
        "wires": [
            [
                "45fb6237a1cf9f3c"
            ],
            [
                "de626943b9d14338"
            ]
        ],
        "outputLabels": [
            "Start generator",
            "Do not start generator"
        ]
    },
    {
        "id": "de626943b9d14338",
        "type": "change",
        "z": "369b878858d61cf7",
        "name": "2. Gen Start Command = NO",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "0",
                "tot": "num"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 800,
        "y": 1260,
        "wires": [
            [
                "29f0271bcfcce405",
                "356fd691fe23ab6a"
            ]
        ]
    },
    {
        "id": "45fb6237a1cf9f3c",
        "type": "switch",
        "z": "369b878858d61cf7",
        "name": "1. Grid is OFF - Now eval command",
        "property": "Gen",
        "propertyType": "flow",
        "rules": [
            {
                "t": "eq",
                "v": "1",
                "vt": "num"
            },
            {
                "t": "neq",
                "v": "1",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 820,
        "y": 1160,
        "wires": [
            [
                "79d40481b29f3be5",
                "356fd691fe23ab6a"
            ],
            [
                "de626943b9d14338"
            ]
        ],
        "outputLabels": [
            "Start generator",
            "Do not start generator"
        ]
    },
    {
        "id": "e6063520689ade46",
        "type": "debug",
        "z": "369b878858d61cf7",
        "name": "Debug - Grid ON/OFF",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 1520,
        "y": 380,
        "wires": []
    },
    {
        "id": "56a26ed4d6ce812d",
        "type": "victron-input-relay",
        "z": "369b878858d61cf7",
        "service": "com.victronenergy.system/0",
        "path": "/Relay/0/State",
        "serviceObj": {
            "service": "com.victronenergy.system/0",
            "name": "Venus system"
        },
        "pathObj": {
            "path": "/Relay/0/State",
            "type": "enum",
            "name": "Venus relay 1 state",
            "enum": {
                "0": "Open",
                "1": "Closed"
            }
        },
        "initial": "",
        "name": "Gen Start Command",
        "onlyChanges": false,
        "x": 170,
        "y": 880,
        "wires": [
            [
                "947189c494a069cb"
            ]
        ]
    },
    {
        "id": "644928513bd1bc2e",
        "type": "victron-output-relay",
        "z": "369b878858d61cf7",
        "service": "com.victronenergy.system/0",
        "path": "/Relay/1/State",
        "serviceObj": {
            "service": "com.victronenergy.system/0",
            "name": "Venus device"
        },
        "pathObj": {
            "path": "/Relay/1/State",
            "type": "enum",
            "name": "Venus relay 2 state",
            "enum": {
                "0": "Open",
                "1": "Closed"
            },
            "writable": true
        },
        "initial": "",
        "name": "",
        "onlyChanges": false,
        "x": 1040,
        "y": 1540,
        "wires": []
    },
    {
        "id": "b501da9f12e18eb4",
        "type": "switch",
        "z": "369b878858d61cf7",
        "name": "Start if Grid loss",
        "property": "Gridd",
        "propertyType": "flow",
        "rules": [
            {
                "t": "eq",
                "v": "0",
                "vt": "num"
            },
            {
                "t": "eq",
                "v": "1",
                "vt": "num"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 460,
        "y": 1300,
        "wires": [
            [
                "4bbb777766823b41"
            ],
            [
                "e9441ae3ec2bdf2e"
            ]
        ],
        "outputLabels": [
            "Start generator",
            "Do not start generator"
        ]
    },
    {
        "id": "4bbb777766823b41",
        "type": "change",
        "z": "369b878858d61cf7",
        "name": "1. Grid is OFF",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "0",
                "tot": "num"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 780,
        "y": 1360,
        "wires": [
            [
                "644928513bd1bc2e"
            ]
        ]
    },
    {
        "id": "e9441ae3ec2bdf2e",
        "type": "change",
        "z": "369b878858d61cf7",
        "name": "2. Grid is ON",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "1",
                "tot": "num"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 770,
        "y": 1440,
        "wires": [
            [
                "644928513bd1bc2e"
            ]
        ]
    },
    {
        "id": "307cf013fbf30de2",
        "type": "debug",
        "z": "369b878858d61cf7",
        "name": "Debug - Shelly Status",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 700,
        "y": 200,
        "wires": []
    },
    {
        "id": "2279a71d4a3ee2f9",
        "type": "inject",
        "z": "369b878858d61cf7",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "15",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 110,
        "y": 260,
        "wires": [
            [
                "3bf7f0d633a9fa24"
            ]
        ]
    },
    {
        "id": "3bf7f0d633a9fa24",
        "type": "function",
        "z": "369b878858d61cf7",
        "name": "status",
        "func": "msg.payload = {\n    type : 'status',\n    id: 'a8032ab7d6a8'\n};\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 310,
        "y": 260,
        "wires": [
            [
                "ef4767ae5f68f144"
            ]
        ]
    },
    {
        "id": "32a02819a0e82083",
        "type": "switch",
        "z": "369b878858d61cf7",
        "name": "",
        "property": "payload.data.device_status[\"switch:0\"].output",
        "propertyType": "msg",
        "rules": [
            {
                "t": "true"
            },
            {
                "t": "false"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 670,
        "y": 400,
        "wires": [
            [
                "1cb806074c06a720"
            ],
            [
                "2e739e54b076d002"
            ]
        ]
    },
    {
        "id": "1cb806074c06a720",
        "type": "change",
        "z": "369b878858d61cf7",
        "name": "Grid = ON",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "1",
                "tot": "num"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 920,
        "y": 320,
        "wires": [
            [
                "3039a8bf46712c06"
            ]
        ]
    },
    {
        "id": "2e739e54b076d002",
        "type": "change",
        "z": "369b878858d61cf7",
        "name": "Grid = OFF",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "0",
                "tot": "num"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 930,
        "y": 440,
        "wires": [
            [
                "3039a8bf46712c06"
            ]
        ]
    },
    {
        "id": "4e3e1adbc1f3384b",
        "type": "debug",
        "z": "369b878858d61cf7",
        "name": "Shelly Gen",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 1790,
        "y": 1140,
        "wires": []
    },
    {
        "id": "0c20d29b1cc34cc6",
        "type": "comment",
        "z": "369b878858d61cf7",
        "name": "Enter Grid Shelly ID",
        "info": "",
        "x": 270,
        "y": 220,
        "wires": []
    },
    {
        "id": "6670f357aa9bda92",
        "type": "comment",
        "z": "369b878858d61cf7",
        "name": "Enter Remote Start Shelly ID",
        "info": "",
        "x": 1300,
        "y": 1020,
        "wires": []
    },
    {
        "id": "7678931987dafeb5",
        "type": "comment",
        "z": "369b878858d61cf7",
        "name": "Enter Remote Start Shelly ID",
        "info": "",
        "x": 1260,
        "y": 1240,
        "wires": []
    },
    {
        "id": "ef4767ae5f68f144",
        "type": "shelly-cloud",
        "z": "369b878858d61cf7",
        "server": "5e8326f9fcf2f8c3",
        "description": "",
        "outputs": 1,
        "x": 470,
        "y": 340,
        "wires": [
            [
                "32a02819a0e82083",
                "307cf013fbf30de2"
            ]
        ]
    },
    {
        "id": "5faea0d370d373b9",
        "type": "shelly-cloud",
        "z": "369b878858d61cf7",
        "server": "928354150eb13039",
        "description": "",
        "outputs": 1,
        "x": 1550,
        "y": 1180,
        "wires": [
            [
                "4e3e1adbc1f3384b"
            ]
        ]
    },
    {
        "id": "58efdbb32ef800dd",
        "type": "victron-output-pvinverter",
        "z": "369b878858d61cf7",
        "service": "com.victronenergy.pvinverter/20",
        "path": "/Ac/PowerLimit",
        "serviceObj": {
            "service": "com.victronenergy.pvinverter/20",
            "name": "Fronius Primo 4.6-1"
        },
        "pathObj": {
            "path": "/Ac/PowerLimit",
            "type": "float",
            "name": "Power limit (W)",
            "writable": true
        },
        "name": "",
        "onlyChanges": false,
        "x": 1550,
        "y": 820,
        "wires": []
    },
    {
        "id": "356fd691fe23ab6a",
        "type": "switch",
        "z": "369b878858d61cf7",
        "name": "",
        "property": "payload",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "0",
                "vt": "num"
            },
            {
                "t": "eq",
                "v": "1",
                "vt": "num"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 1010,
        "y": 920,
        "wires": [
            [
                "bf24030f33fa8de8"
            ],
            [
                "bf24030f33fa8de8"
            ]
        ]
    },
    {
        "id": "bf24030f33fa8de8",
        "type": "trigger",
        "z": "369b878858d61cf7",
        "name": "",
        "op1": "0",
        "op2": "0",
        "op1type": "num",
        "op2type": "str",
        "duration": "-0.25",
        "extend": false,
        "overrideDelay": false,
        "units": "s",
        "reset": "0",
        "bytopic": "all",
        "topic": "topic",
        "outputs": 1,
        "x": 1250,
        "y": 860,
        "wires": [
            [
                "58efdbb32ef800dd",
                "d5376855a7a6ec0c"
            ]
        ]
    },
    {
        "id": "d5376855a7a6ec0c",
        "type": "debug",
        "z": "369b878858d61cf7",
        "name": "PV Export Limit",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 1500,
        "y": 680,
        "wires": []
    },
    {
        "id": "5e8326f9fcf2f8c3",
        "type": "shelly-cloud-server",
        "description": ""
    },
    {
        "id": "928354150eb13039",
        "type": "shelly-cloud-server",
        "description": ""
    }
]

(DF: Edited for readability)

Johan-Bo commented 7 months ago

@dirkjanfaber Do you know how I can set the Gen to start every second Monday in a month? I can get it to start every Monday at 07:00 and run for a few minutes to charge the gen battery but how can I set it to only run every second Monday?

[
    {
        "id": "c00fdc5480fa9ce0",
        "type": "tab",
        "label": "Flow 1",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "83d9d548ee26e156",
        "type": "trigger",
        "z": "c00fdc5480fa9ce0",
        "name": "",
        "op1": "1",
        "op2": "0",
        "op1type": "num",
        "op2type": "num",
        "duration": "30",
        "extend": false,
        "overrideDelay": false,
        "units": "min",
        "reset": "",
        "bytopic": "all",
        "topic": "topic",
        "outputs": 1,
        "x": 250,
        "y": 300,
        "wires": [
            [
                "ed8358d355931c4e"
            ]
        ]
    },
    {
        "id": "bec69d690a1129e4",
        "type": "inject",
        "z": "c00fdc5480fa9ce0",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "00 07 * * 1",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 90,
        "y": 300,
        "wires": [
            [
                "83d9d548ee26e156"
            ]
        ]
    },
    {
        "id": "5c349e8aeb1f8d2e",
        "type": "victron-output-relay",
        "z": "c00fdc5480fa9ce0",
        "service": "com.victronenergy.system/0",
        "path": "/Relay/1/State",
        "serviceObj": {
            "service": "com.victronenergy.system/0",
            "name": "Venus device"
        },
        "pathObj": {
            "path": "/Relay/1/State",
            "type": "enum",
            "name": "Venus relay 2 state",
            "enum": {
                "0": "Open",
                "1": "Closed"
            },
            "writable": true
        },
        "initial": "",
        "name": "Relay 2 - Grid Mains Sense (Mains Fail = 1, Mains Return=0)",
        "onlyChanges": false,
        "x": 1200,
        "y": 240,
        "wires": []
    },
    {
        "id": "0292dfd17a709e38",
        "type": "victron-output-generator",
        "z": "c00fdc5480fa9ce0",
        "service": "com.victronenergy.generator/0",
        "path": "/ManualStart",
        "serviceObj": {
            "service": "com.victronenergy.generator/0",
            "name": "Generator start/stop"
        },
        "pathObj": {
            "path": "/ManualStart",
            "type": "enum",
            "name": "Manual Start",
            "enum": {
                "0": "Stop generator",
                "1": "Start generator"
            },
            "writable": true
        },
        "initial": "",
        "name": "",
        "onlyChanges": false,
        "x": 1120,
        "y": 320,
        "wires": []
    },
    {
        "id": "93ab0225cbef75be",
        "type": "debug",
        "z": "c00fdc5480fa9ce0",
        "name": "Gen start scheduled",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 580,
        "y": 140,
        "wires": []
    },
    {
        "id": "ad5f6889f3727692",
        "type": "debug",
        "z": "c00fdc5480fa9ce0",
        "name": "Gen Stop after schedule",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 570,
        "y": 460,
        "wires": []
    },
    {
        "id": "e814cd61436c1959",
        "type": "change",
        "z": "c00fdc5480fa9ce0",
        "name": "SImultae Mains fail. Payload = 1",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "1",
                "tot": "num"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 610,
        "y": 240,
        "wires": [
            [
                "93ab0225cbef75be",
                "5c349e8aeb1f8d2e",
                "0292dfd17a709e38"
            ]
        ]
    },
    {
        "id": "ed8358d355931c4e",
        "type": "switch",
        "z": "c00fdc5480fa9ce0",
        "name": "",
        "property": "payload",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "1",
                "vt": "num"
            },
            {
                "t": "eq",
                "v": "0",
                "vt": "num"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 390,
        "y": 300,
        "wires": [
            [
                "e814cd61436c1959"
            ],
            [
                "967f7ba918317847"
            ]
        ]
    },
    {
        "id": "967f7ba918317847",
        "type": "change",
        "z": "c00fdc5480fa9ce0",
        "name": "Simulated Main Return. Payload = 0",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "0",
                "tot": "num"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 630,
        "y": 360,
        "wires": [
            [
                "ad5f6889f3727692",
                "5c349e8aeb1f8d2e",
                "0292dfd17a709e38"
            ]
        ]
    }
]

(DF: Edited for readability)

dirkjanfaber commented 7 months ago

You can do that by adding an extra function node, checking for the timestamp to match the second Monday of the month. Eg:

[{"id":"83d9d548ee26e156","type":"trigger","z":"c00fdc5480fa9ce0","name":"","op1":"1","op2":"0","op1type":"num","op2type":"num","duration":"30","extend":false,"overrideDelay":false,"units":"min","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":550,"y":200,"wires":[["ed8358d355931c4e"]]},{"id":"bec69d690a1129e4","type":"inject","z":"c00fdc5480fa9ce0","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"00 07 * * 1","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":130,"y":200,"wires":[["017fb9dad68a3f0f"]]},{"id":"5c349e8aeb1f8d2e","type":"victron-output-relay","z":"c00fdc5480fa9ce0","service":"com.victronenergy.system/0","path":"/Relay/1/State","serviceObj":{"service":"com.victronenergy.system/0","name":"Venus device"},"pathObj":{"path":"/Relay/1/State","type":"enum","name":"Venus relay 2 state","enum":{"0":"Open","1":"Closed"},"writable":true},"initial":"","name":"Relay 2 - Grid Mains Sense (Mains Fail = 1, Mains Return=0)","onlyChanges":false,"x":1000,"y":280,"wires":[]},{"id":"0292dfd17a709e38","type":"victron-output-generator","z":"c00fdc5480fa9ce0","service":"com.victronenergy.generator/0","path":"/ManualStart","serviceObj":{"service":"com.victronenergy.generator/0","name":"Generator start/stop"},"pathObj":{"path":"/ManualStart","type":"enum","name":"Manual Start","enum":{"0":"Stop generator","1":"Start generator"},"writable":true},"initial":"","name":"","onlyChanges":false,"x":920,"y":340,"wires":[]},{"id":"93ab0225cbef75be","type":"debug","z":"c00fdc5480fa9ce0","name":"Gen start scheduled","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":640,"y":260,"wires":[]},{"id":"ad5f6889f3727692","type":"debug","z":"c00fdc5480fa9ce0","name":"Gen Stop after schedule","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":630,"y":380,"wires":[]},{"id":"e814cd61436c1959","type":"change","z":"c00fdc5480fa9ce0","name":"SImultae Mains fail. Payload = 1","rules":[{"t":"set","p":"payload","pt":"msg","to":"1","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":330,"y":300,"wires":[["93ab0225cbef75be","5c349e8aeb1f8d2e","0292dfd17a709e38"]]},{"id":"ed8358d355931c4e","type":"switch","z":"c00fdc5480fa9ce0","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"num"},{"t":"eq","v":"0","vt":"num"}],"checkall":"true","repair":false,"outputs":2,"x":90,"y":320,"wires":[["e814cd61436c1959"],["967f7ba918317847"]]},{"id":"967f7ba918317847","type":"change","z":"c00fdc5480fa9ce0","name":"Simulated Main Return. Payload = 0","rules":[{"t":"set","p":"payload","pt":"msg","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":350,"y":340,"wires":[["ad5f6889f3727692","5c349e8aeb1f8d2e","0292dfd17a709e38"]]},{"id":"017fb9dad68a3f0f","type":"function","z":"c00fdc5480fa9ce0","name":"Check for seond monday","func":"function isSecondMonday(timestamp) {\n  const date = new Date(timestamp);\n  const month = date.getMonth();\n  const year = date.getFullYear();\n\n  // Get the day of the week for the first day of the month\n  const firstDayOfMonth = new Date(year, month, 1);\n  const firstDayOfWeek = firstDayOfMonth.getDay();\n\n  // Calculate the difference between the first day of the month and Monday\n  const daysUntilMonday = (8 - firstDayOfWeek) % 7;\n\n  // Calculate the date of the second Monday\n  const secondMonday = new Date(year, month, daysUntilMonday + 7);\n\n  // Check if the given date is equal to the second Monday\n  return date.getDate() === secondMonday.getDate();\n}\n\nif (isSecondMonday(msg.payload)) {\n  node.warn('The given timestamp is the second Monday of the month.');\n  return msg;\n} else {\n  node.warn('The given timestamp is not the second Monday of the month.');\n}\n\n","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":330,"y":200,"wires":[["83d9d548ee26e156"]]}]

The used function looks like this:

function isSecondMonday(timestamp) {
  const date = new Date(timestamp);
  const month = date.getMonth();
  const year = date.getFullYear();

  // Get the day of the week for the first day of the month
  const firstDayOfMonth = new Date(year, month, 1);
  const firstDayOfWeek = firstDayOfMonth.getDay();

  // Calculate the difference between the first day of the month and Monday
  const daysUntilMonday = (8 - firstDayOfWeek) % 7;

  // Calculate the date of the second Monday
  const secondMonday = new Date(year, month, daysUntilMonday + 7);

  // Check if the given date is equal to the second Monday
  return date.getDate() === secondMonday.getDate();
}

if (isSecondMonday(msg.payload)) {
  node.warn('The given timestamp is the second Monday of the month.');
  return msg;
} else {
  node.warn('The given timestamp is not the second Monday of the month.');
}

Remove the node.warn lines if you don't want it to be verbose in the debug tab.