Countdown to time of day

Hi, I'm trying to have a dashboard icon trigger different presets of "countdown to times of day". All of the times are the same, but would be triggered at different times.

I have the setup working (see the picture for details) where I insert the amount of seconds into the countdown node. The problem is I need a way to calculate the amount of seconds from when the command is triggered to the time specified so that it can correctly set the duration of the timer. Is there a way to do this in Node-RED?

There's almost always a way to do something. I'm having trouble wrapping my head around your issue. Basically it sounds like you need to set a time when you start the command then take the start time and subtract that from the start command. You will need to set up something to hold the variables, think either context.save or flow.save then do the math. It would be easier if you posted flow instead of screen shots as well.

Yes, basically I need to take the "targeted" time and subtract from the current time then input that into the countdown.

I can't figure out how I would calculate the time from January 1st 1970 or whatever the like "default" time is to the targeted countdown time. I pasted the flow below.

[
    {
        "id": "d534248f9847b5fd",
        "type": "countdown",
        "z": "31025f12f7a78095",
        "name": "",
        "topic": "",
        "payloadTimerStart": "true",
        "payloadTimerStartType": "bool",
        "payloadTimerStop": "false",
        "payloadTimerStopType": "bool",
        "timer": 30,
        "resetWhileRunning": false,
        "outputOnReset": false,
        "setTimeToNewWhileRunning": true,
        "startCountdownOnControlMessage": true,
        "x": 290,
        "y": 1000,
        "wires": [
            [
                "fd08a1f6c0e70d14"
            ],
            [
                "05e8438de8335447"
            ]
        ]
    },
    {
        "id": "fd08a1f6c0e70d14",
        "type": "ui_text",
        "z": "31025f12f7a78095",
        "group": "82c733d55132f43d",
        "order": 16,
        "width": "11",
        "height": "1",
        "name": "Running status",
        "label": "Running:",
        "format": "{{msg.payload}}",
        "layout": "row-center",
        "x": 540,
        "y": 1000,
        "wires": []
    },
    {
        "id": "c5cd621235afc6a3",
        "type": "ui_text",
        "z": "31025f12f7a78095",
        "group": "82c733d55132f43d",
        "order": 17,
        "width": "11",
        "height": "1",
        "name": "",
        "label": "Time Remaining:",
        "format": "{{msg.payload}}",
        "layout": "row-center",
        "x": 550,
        "y": 1040,
        "wires": []
    },
    {
        "id": "3a3082eb796fa8da",
        "type": "inject",
        "z": "31025f12f7a78095",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "control",
        "payload": "60",
        "payloadType": "num",
        "x": 120,
        "y": 1000,
        "wires": [
            [
                "d534248f9847b5fd"
            ]
        ]
    },
    {
        "id": "05e8438de8335447",
        "type": "calculator",
        "z": "31025f12f7a78095",
        "name": "",
        "inputMsgField": "payload",
        "outputMsgField": "payload",
        "operation": "mult",
        "constant": "1000",
        "round": false,
        "decimals": 0,
        "x": 375,
        "y": 1060,
        "wires": [
            [
                "9ececab1dc0842c9"
            ]
        ],
        "l": false
    },
    {
        "id": "9ececab1dc0842c9",
        "type": "moment",
        "z": "31025f12f7a78095",
        "name": "",
        "topic": "",
        "input": "payload",
        "inputType": "msg",
        "inTz": "Indianapolis",
        "adjAmount": "0",
        "adjType": "days",
        "adjDir": "add",
        "format": "H:mm:ss",
        "locale": "en-US",
        "output": "payload",
        "outputType": "msg",
        "outTz": "ETC/UTC",
        "x": 425,
        "y": 1060,
        "wires": [
            [
                "c5cd621235afc6a3"
            ]
        ],
        "l": false
    },
    {
        "id": "82c733d55132f43d",
        "type": "ui_group",
        "name": "Group 1",
        "tab": "18e3984f2ee15abe",
        "order": 1,
        "disp": false,
        "width": "11",
        "collapse": false
    },
    {
        "id": "18e3984f2ee15abe",
        "type": "ui_tab",
        "name": "Kitchen",
        "icon": "dashboard",
        "order": 2,
        "disabled": false,
        "hidden": true
    }
]

Sorry if this doesn't make sense, I'm still trying to wrap my head around this myself.

The overall goal is that I'm basically wanting to be able to type in a time in the dashboard and it starts a countdown from the current time to the time I specified.

Have you looked at ui-time-scheduler node?

The node can send out trigger message at specified time of day, and day of week.

I don't think at this point it's a node thing, it's figuring out a process. I'm sitting on a phone so won't be of much help for a while but when I do time things I try to stay in epoch time as much as I can as it makes math easier for me. But then I'm pretty poor at coding.

1 Like

can you post your flow so we can see what you have so far?
hint : put it between the tick marks, don't post it as text
[edit] you have two nodes that aren't standard, can you tell us what they are? maybe node-red-contrib-something?

so here is a quick and dirty "my take" on what I think you are asking. There is one node node-red-contrib-mytimeout that is non standard.

[{"id":"875aaaaf67c75478","type":"inject","z":"b7e805ceee0d9cbf","name":"time now","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"time now","payload":"","payloadType":"date","x":260,"y":120,"wires":[["74baafe725218c84","be885b7aea55800b"]]},{"id":"be885b7aea55800b","type":"function","z":"b7e805ceee0d9cbf","name":"calculate","func":"let Future = 0;\nlet Now = 0;\nlet FutureTime = 0;\nlet Difference = 0;\nlet TimeOut = 0;\n\nif (msg.topic == \"time now\"){\n    context.set(\"now\",msg.payload); //save time now\n}\n\nif (msg.topic == \"future\"){\n    context.set(\"difference\",msg.payload); //save future time\n}\n\nNow = context.get(\"now\");\nDifference = context.get(\"difference\");\nFuture = Now + Difference;\n\nTimeOut = Difference / 1000; // divide to get seconds\n\nlet Msg1 = {\n    payload: Future,\n}\n\nlet Msg2 = {\n    payload: \"on\",\n    timeout: TimeOut,\n    warning: 30\n}\nnode.status({ fill: \"blue\", shape: \"ring\", text: Msg1.payload });\n\nreturn [Msg1,Msg2];","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":580,"y":200,"wires":[["2302c9c1bafc4e7e"],["644c019aaff8d897"]]},{"id":"644c019aaff8d897","type":"mytimeout","z":"b7e805ceee0d9cbf","name":"","outtopic":"","outsafe":"","outwarning":"Warning","outunsafe":"off","warning":"5","timer":"30","debug":false,"ndebug":false,"ignoreCase":false,"repeat":false,"again":false,"x":690,"y":300,"wires":[[],["f16afd957ab09adb","6dc66cea757ab1cc"]]},{"id":"915a3b809df826a6","type":"debug","z":"b7e805ceee0d9cbf","name":"future","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"true","targetType":"full","statusVal":"payload","statusType":"auto","x":1010,"y":200,"wires":[]},{"id":"74baafe725218c84","type":"moment","z":"b7e805ceee0d9cbf","name":"","topic":"","input":"","inputType":"msg","inTz":"America/Los_Angeles","adjAmount":0,"adjType":"days","adjDir":"add","format":"h:mm:ss","locale":"en-US","output":"","outputType":"msg","outTz":"America/Los_Angeles","x":260,"y":200,"wires":[["d274e0bbbd8b81c5"]]},{"id":"d274e0bbbd8b81c5","type":"debug","z":"b7e805ceee0d9cbf","name":"time now","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"true","targetType":"full","statusVal":"payload","statusType":"auto","x":460,"y":300,"wires":[]},{"id":"c20d2ca7b3d0ec2e","type":"inject","z":"b7e805ceee0d9cbf","name":"add 40 minutes","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"future","payload":"2400000","payloadType":"num","x":520,"y":120,"wires":[["be885b7aea55800b"]]},{"id":"2302c9c1bafc4e7e","type":"moment","z":"b7e805ceee0d9cbf","name":"","topic":"","input":"","inputType":"msg","inTz":"America/Los_Angeles","adjAmount":0,"adjType":"days","adjDir":"add","format":"h:mm:ss","locale":"en-US","output":"","outputType":"msg","outTz":"America/Los_Angeles","x":820,"y":200,"wires":[["915a3b809df826a6"]]},{"id":"f16afd957ab09adb","type":"function","z":"b7e805ceee0d9cbf","name":"Time Remaining","func":"let Z = msg.payload;\n\nlet Y = Z / 60;\n\n Y = Y.toFixed(2);\n\nmsg = {\n    payload: Y + \" mins\",\n}\n\nnode.status({ fill: \"blue\", shape: \"ring\", text: msg.payload });\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":940,"y":300,"wires":[[]]},{"id":"6dc66cea757ab1cc","type":"debug","z":"b7e805ceee0d9cbf","name":"count down","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":910,"y":360,"wires":[]}]

The external nodes I have are
node-red-contrib-countdown
node-red-contrib-calc
node-red-contrib-moment

Sorry I didn't mention that previously!