How to do a timer switch?

image

please help me, how to make this circuit?
If timestamp2 is activated less than 30s aftertime stamp1, then output1; otherwise output 2.

To compare 2 values from different inputs - you need either a context to collect data or join them to one message object. Here is the second option to use one msg object:

The payload contains the timestamps of both Inject Nodes - as timestamp1 and timestamp2

[
    {
        "id": "caf33cc421d60cc9",
        "type": "inject",
        "z": "7fd8fff652f013f7",
        "name": "timerstamp1",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "30",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "timestamp1",
        "payload": "",
        "payloadType": "date",
        "x": 520,
        "y": 2700,
        "wires": [
            [
                "573e5c83f4c58711"
            ]
        ]
    },
    {
        "id": "c045dc5af2c4ee14",
        "type": "inject",
        "z": "7fd8fff652f013f7",
        "name": "timestamp2",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "timestamp2",
        "payload": "",
        "payloadType": "date",
        "x": 510,
        "y": 2760,
        "wires": [
            [
                "573e5c83f4c58711"
            ]
        ]
    },
    {
        "id": "2ce6af9c4c69d452",
        "type": "switch",
        "z": "7fd8fff652f013f7",
        "name": "",
        "property": "payload.timestamp2 - payload.timestamp1",
        "propertyType": "jsonata",
        "rules": [
            {
                "t": "btwn",
                "v": "0",
                "vt": "num",
                "v2": "30000",
                "v2t": "num"
            },
            {
                "t": "else"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 910,
        "y": 2760,
        "wires": [
            [
                "0ecde1ec68971efd"
            ],
            [
                "18fa985cfca36a10"
            ]
        ]
    },
    {
        "id": "573e5c83f4c58711",
        "type": "join",
        "z": "7fd8fff652f013f7",
        "name": "",
        "mode": "custom",
        "build": "object",
        "property": "payload",
        "propertyType": "msg",
        "key": "topic",
        "joiner": "\\n",
        "joinerType": "str",
        "accumulate": true,
        "timeout": "",
        "count": "2",
        "reduceRight": false,
        "reduceExp": "",
        "reduceInit": "",
        "reduceInitType": "",
        "reduceFixup": "",
        "x": 770,
        "y": 2760,
        "wires": [
            [
                "2ce6af9c4c69d452",
                "1470b1c4d3b44a90"
            ]
        ]
    },
    {
        "id": "0ecde1ec68971efd",
        "type": "debug",
        "z": "7fd8fff652f013f7",
        "name": "output1",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 1060,
        "y": 2720,
        "wires": []
    },
    {
        "id": "18fa985cfca36a10",
        "type": "debug",
        "z": "7fd8fff652f013f7",
        "name": "output2",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 1060,
        "y": 2780,
        "wires": []
    },
    {
        "id": "1470b1c4d3b44a90",
        "type": "change",
        "z": "7fd8fff652f013f7",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "payload.timestamp2 - payload.timestamp1",
                "tot": "jsonata"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 950,
        "y": 2840,
        "wires": [
            [
                "5a3c97e10e8b6332"
            ]
        ]
    },
    {
        "id": "5a3c97e10e8b6332",
        "type": "debug",
        "z": "7fd8fff652f013f7",
        "name": "",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 1150,
        "y": 2840,
        "wires": []
    }
]

The switch creates the difference and send the msg if between 0 and 30,000 ms to output 1 otherwise to output 2

image

1 Like

Thanks. It seems to be what I need.

I also need to add a condition - if the timestamp1 is not activated at all, but the timestamp2 is activated, then the output2.

Just change the join node to wait for 1 instead of 2 messages.

If there is still an old timestamp1 stamp is in the join object it doesn't matter as then the time difference is greater than 30 s as well and the otherwise case will be chosen.

Yes, but then activating timestamp1 also results in output2. I need to process only the timestamp2. Add a filter?

Hmm - yes - but I thought that was the intention. If timestamp2 is activate and then timestamp1 and you want to have this as output 1 then you need the absolute value - then it doesn#t matter which one is first - and sends always to output 1 - if the difference is < 30s.

image

No, not like this. Timestamp1 is only a condition for timestamp2. There should be no action after it.

This will work as it should. But after each deployment, the first hit will be incorrect.

Ok - so you there is a strict order.

  1. If timestamp1 is first and then timestamp2 is within 30s then output1
  2. If timestamp1 is first and then timestamp2 is after 30s then output2
  3. If timestamp2 is alone then output2
  4. The next timestamp1 has to be ignored and the next timestamp2 directs to output 1 or 2 dependent on time difference.

I fear you need a function node.

I try to implement this.

Would adding a msg.complete to timestamp2 do what you need.
e.g.

[{"id":"caf33cc421d60cc9","type":"inject","z":"bf9e1e33.030598","name":"timerstamp1","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"timestamp1","payload":"","payloadType":"date","x":100,"y":80,"wires":[["573e5c83f4c58711"]]},{"id":"573e5c83f4c58711","type":"join","z":"bf9e1e33.030598","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":360,"y":140,"wires":[["2ce6af9c4c69d452"]]},{"id":"c045dc5af2c4ee14","type":"inject","z":"bf9e1e33.030598","name":"timestamp2","props":[{"p":"payload"},{"p":"topic","vt":"str"},{"p":"complete","v":"true","vt":"bool"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"timestamp2","payload":"","payloadType":"date","x":100,"y":140,"wires":[["573e5c83f4c58711"]]},{"id":"2ce6af9c4c69d452","type":"switch","z":"bf9e1e33.030598","name":"","property":"$abs(payload.timestamp1 - payload.timestamp2)","propertyType":"jsonata","rules":[{"t":"btwn","v":"0","vt":"num","v2":"5000","v2t":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":500,"y":140,"wires":[["0ecde1ec68971efd"],["18fa985cfca36a10"]]},{"id":"0ecde1ec68971efd","type":"debug","z":"bf9e1e33.030598","name":"output1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":650,"y":100,"wires":[]},{"id":"18fa985cfca36a10","type":"debug","z":"bf9e1e33.030598","name":"output2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":650,"y":160,"wires":[]}]
2 Likes

so here a variant with a function node:

[
    {
        "id": "55a36a4283820f43",
        "type": "inject",
        "z": "7fd8fff652f013f7",
        "name": "timerstamp1",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "timestamp1",
        "payload": "",
        "payloadType": "date",
        "x": 310,
        "y": 3160,
        "wires": [
            [
                "24a3e35cd8a39b34"
            ]
        ]
    },
    {
        "id": "e09fb12e0bc4fa10",
        "type": "inject",
        "z": "7fd8fff652f013f7",
        "name": "timestamp2",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "timestamp2",
        "payload": "",
        "payloadType": "date",
        "x": 310,
        "y": 3240,
        "wires": [
            [
                "24a3e35cd8a39b34"
            ]
        ]
    },
    {
        "id": "24a3e35cd8a39b34",
        "type": "function",
        "z": "7fd8fff652f013f7",
        "name": "",
        "func": "if (msg.topic === \"timestamp1\") context.set(\"t1\",msg.payload);\n\nif (msg.topic === \"timestamp2\"){\n    var ts1 = context.get(\"t1\");\n    if (ts1 === undefined) return [null, msg]; else {\n        context.set (\"t1\", undefined);\n        if (msg.payload - ts1 >= 0 && msg.payload - ts1 <= 30000) return [msg,null]; else return [null, msg];\n    }\n}\n",
        "outputs": 2,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 500,
        "y": 3200,
        "wires": [
            [
                "39598c3dda919b38"
            ],
            [
                "bc5efa4cf2803c2e"
            ]
        ]
    },
    {
        "id": "39598c3dda919b38",
        "type": "debug",
        "z": "7fd8fff652f013f7",
        "name": "output1",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 700,
        "y": 3160,
        "wires": []
    },
    {
        "id": "bc5efa4cf2803c2e",
        "type": "debug",
        "z": "7fd8fff652f013f7",
        "name": "output2",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 700,
        "y": 3220,
        "wires": []
    }
]

Yes. Looks like this is exactly what I need.
And I already began to invent too difficult:

[{"id":"caf33cc421d60cc9","type":"inject","z":"91dceac70a0f544f","name":"timerstamp1","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"timestamp1","payload":"","payloadType":"date","x":130,"y":320,"wires":[["573e5c83f4c58711"]]},{"id":"c045dc5af2c4ee14","type":"inject","z":"91dceac70a0f544f","name":"timestamp2","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"timestamp2","payload":"","payloadType":"date","x":130,"y":380,"wires":[["573e5c83f4c58711"]]},{"id":"2ce6af9c4c69d452","type":"switch","z":"91dceac70a0f544f","name":"","property":"payload.timestamp2 - payload.timestamp1","propertyType":"jsonata","rules":[{"t":"btwn","v":"0","vt":"num","v2":"30000","v2t":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":570,"y":380,"wires":[["0ecde1ec68971efd"],["2e7ce5c7e23c7d80"]]},{"id":"573e5c83f4c58711","type":"join","z":"91dceac70a0f544f","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"1","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":390,"y":380,"wires":[["2ce6af9c4c69d452"]]},{"id":"0ecde1ec68971efd","type":"debug","z":"91dceac70a0f544f","name":"output1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":900,"y":340,"wires":[]},{"id":"18fa985cfca36a10","type":"debug","z":"91dceac70a0f544f","name":"output2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":900,"y":400,"wires":[]},{"id":"2e7ce5c7e23c7d80","type":"switch","z":"91dceac70a0f544f","name":"","property":"topic","propertyType":"msg","rules":[{"t":"cont","v":"timestamp2","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":750,"y":400,"wires":[["18fa985cfca36a10"]]}]

Ok yours @E1cid is smarter. :wink:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.