biddster / node-red-contrib-schedex

Scheduler for node-red which allows you to enter on/off times as 24hr clock (e.g. 01:10) or suncalc events (e.g. goldenHour). It also allows you to offset times and randomise the time within the offset.
22 stars 17 forks source link

Offsets is not exact #71

Closed slurpey closed 4 years ago

slurpey commented 4 years ago

Hi, I'm having a curious behavior.

If my msg.payload is "ontime 23:11 onoffset 0"
I get: ON 2020-01-29 23:11 (which works)

If it's "ontime 10:45 onoffset 12" I get: ON 2020-01-30 10:57 (that's 24hrs and 12 minutes offset)

If it's "ontime 2.30 onoffset 36" I get on 2020-01-30 03:06 (that's 24 hours, 36 minutes

You can check my Node-red flow if you want below (it's planned to receive a webhook from IFTTT / Google Home and I had to do some weird time calculations)


[
    {
        "id": "c5994873.184318",
        "type": "tab",
        "label": "Scratchboard",
        "disabled": false,
        "info": ""
    },
    {
        "id": "f215d67.f2e1728",
        "type": "function",
        "z": "c5994873.184318",
        "name": "extract body",
        "func": "return {\n    payload: msg.payload.body\n};",
        "outputs": 1,
        "noerr": 0,
        "x": 290,
        "y": 140,
        "wires": [
            [
                "f4adf06.ff9081"
            ]
        ]
    },
    {
        "id": "f4adf06.ff9081",
        "type": "json",
        "z": "c5994873.184318",
        "name": "",
        "property": "payload",
        "action": "",
        "pretty": true,
        "x": 450,
        "y": 140,
        "wires": [
            [
                "54cf20a9.f476d"
            ]
        ]
    },
    {
        "id": "54cf20a9.f476d",
        "type": "function",
        "z": "c5994873.184318",
        "name": "Convert to Payload and change to variables needed later",
        "func": "var msg = {payload:{ontime: msg.payload.time_google, message: msg.payload.message_google}};\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 750,
        "y": 140,
        "wires": [
            [
                "2c386cbf.df4394"
            ]
        ]
    },
    {
        "id": "db88a700.3f6248",
        "type": "inject",
        "z": "c5994873.184318",
        "name": "2.30",
        "topic": "",
        "payload": "{\"type\":\"webhook\",\"meta\":{\"id\":\"815a9995-208e-44fd-be82-45ec2112dd6f\",\"bucked_id\":\"1ecec89d-b44e-4c53-87c5-f2abcc38392d\",\"bucket_name\":\"gactions\",\"input_id\":\"68922408-5099-4ab7-89b5-844210568a24\",\"input_name\":\"Default public endpoint\",\"output_name\":\"\",\"output_destination\":\"\"},\"headers\":{\"X-Newrelic-Id\":[\"VwAOU1RRGwAFUFZUAwQE\"],\"X-Newrelic-Transaction\":[\"PxQGUgNUClYGB1lWVgFSUUYdUFIOFQZOElMLBw8KUQRQXQ0AAQQEQEgUUQMDW1kEVQZDPw==\"],\"Content-Type\":[\"application/json\"],\"Content-Length\":[\"58\"]},\"query\":\"\",\"body\":\"{ \\\"time_google\\\": \\\"2.30\\\", \\\"message_google\\\": \\\"about potatoes\\\" }\",\"method\":\"POST\"}",
        "payloadType": "json",
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "x": 90,
        "y": 40,
        "wires": [
            [
                "f215d67.f2e1728",
                "a730ac16.b8b86"
            ]
        ]
    },
    {
        "id": "a8354c4f.3ce39",
        "type": "inject",
        "z": "c5994873.184318",
        "name": "10.45",
        "topic": "",
        "payload": "{\"type\":\"webhook\",\"meta\":{\"id\":\"815a9995-208e-44fd-be82-45ec2112dd6f\",\"bucked_id\":\"1ecec89d-b44e-4c53-87c5-f2abcc38392d\",\"bucket_name\":\"gactions\",\"input_id\":\"68922408-5099-4ab7-89b5-844210568a24\",\"input_name\":\"Default public endpoint\",\"output_name\":\"\",\"output_destination\":\"\"},\"headers\":{\"X-Newrelic-Id\":[\"VwAOU1RRGwAFUFZUAwQE\"],\"X-Newrelic-Transaction\":[\"PxQGUgNUClYGB1lWVgFSUUYdUFIOFQZOElMLBw8KUQRQXQ0AAQQEQEgUUQMDW1kEVQZDPw==\"],\"Content-Type\":[\"application/json\"],\"Content-Length\":[\"58\"]},\"query\":\"\",\"body\":\"{ \\\"time_google\\\": \\\"10.45\\\", \\\"message_google\\\": \\\"about potatoes\\\" }\",\"method\":\"POST\"}",
        "payloadType": "json",
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "x": 90,
        "y": 80,
        "wires": [
            [
                "f215d67.f2e1728",
                "a730ac16.b8b86"
            ]
        ]
    },
    {
        "id": "2c386cbf.df4394",
        "type": "function",
        "z": "c5994873.184318",
        "name": "Ontime to Flow",
        "func": "flow.set(\"ontime\", msg.payload.ontime);\nontime = flow.get(\"ontime\");\nmsg.payload.ontime = ontime;\nmsg.payload.timestamp = flow.get(\"timestamp\");\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 140,
        "y": 220,
        "wires": [
            [
                "8015134e.8588d"
            ]
        ]
    },
    {
        "id": "a730ac16.b8b86",
        "type": "function",
        "z": "c5994873.184318",
        "name": "Timestamp",
        "func": "// msg.payload : is the time we want to trigger an event. Format H.MM\n// ontime : is the time we want to trigger an event. Format HH:MM\n\n// This function will:\n// 1- Transform payload format H.MM to HH.MM\n// 2- Create ontime so as it is teh next moment in the future\n\nlet ontimeHour;\nlet ontimeMinute;\n\n// Calculate number of minutes from midnight until NOW\nlet d = new Date();\nlet timeNowHours = d.getHours();\nlet timeNowMinutes = d.getMinutes();\nlet minutesNow = timeNowMinutes + 60 * timeNowHours;\n\n\n// Change Hour and Minute to string (format HH.MM)\nontimeHour = timeNowHours.toString().padStart(2, \"0\");\nontimeMinute = timeNowMinutes.toString().padStart(2, \"0\");\n\nmsg.payload = {};\nmsg.payload.timestamp = `${timeNowHours}.${timeNowMinutes}`;\nflow.set(\"timestamp\", `${timeNowHours}.${timeNowMinutes}`);\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 390,
        "y": 100,
        "wires": [
            []
        ]
    },
    {
        "id": "8015134e.8588d",
        "type": "function",
        "z": "c5994873.184318",
        "name": "Make PM if needed",
        "func": "ontime = flow.get('ontime');\ntimestamp = flow.get('timestamp');\n\nif (ontime < timestamp && timestamp >19 && ontime < 7) {\n    extra = 36;\n}\nelse if (ontime < timestamp && timestamp >19 && ontime < 7) {\n    extra = 24;\n}\nelse if (ontime < timestamp) {\n    extra = 12;\n}\nelse {\n     extra = 0;\n}\nflow.set(\"ontime\", ontime)\nflow.set(\"extra\", extra)\nmsg.payload.ontime = ontime\nmsg.payload.extra = extra\nreturn msg;\n\n\n/* Ontime < timestamp && timestamp > 19 && ontime < 7 = ontime = ontime+36\nOntime < timestamp && timestamp > 19 && ontime > 7 = ontime = ontime+24\nOntime < timestamp = ontime = ontime+12\nOntime > timestamp = ontime = ontime */",
        "outputs": 1,
        "noerr": 0,
        "x": 350,
        "y": 220,
        "wires": [
            [
                "6fdf3ccc.9a5394"
            ]
        ]
    },
    {
        "id": "6fdf3ccc.9a5394",
        "type": "change",
        "z": "c5994873.184318",
        "name": "",
        "rules": [
            {
                "t": "change",
                "p": "ontime",
                "pt": "flow",
                "from": ".",
                "fromt": "str",
                "to": ":",
                "tot": "str"
            },
            {
                "t": "change",
                "p": "payload.ontime",
                "pt": "msg",
                "from": ".",
                "fromt": "str",
                "to": ":",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 240,
        "y": 320,
        "wires": [
            [
                "af88a4cc.908d68"
            ]
        ]
    },
    {
        "id": "7d92a025.77758",
        "type": "inject",
        "z": "c5994873.184318",
        "name": "23.11",
        "topic": "",
        "payload": "{\"type\":\"webhook\",\"meta\":{\"id\":\"815a9995-208e-44fd-be82-45ec2112dd6f\",\"bucked_id\":\"1ecec89d-b44e-4c53-87c5-f2abcc38392d\",\"bucket_name\":\"gactions\",\"input_id\":\"68922408-5099-4ab7-89b5-844210568a24\",\"input_name\":\"Default public endpoint\",\"output_name\":\"\",\"output_destination\":\"\"},\"headers\":{\"X-Newrelic-Id\":[\"VwAOU1RRGwAFUFZUAwQE\"],\"X-Newrelic-Transaction\":[\"PxQGUgNUClYGB1lWVgFSUUYdUFIOFQZOElMLBw8KUQRQXQ0AAQQEQEgUUQMDW1kEVQZDPw==\"],\"Content-Type\":[\"application/json\"],\"Content-Length\":[\"58\"]},\"query\":\"\",\"body\":\"{ \\\"time_google\\\": \\\"23.11\\\", \\\"message_google\\\": \\\"about potatoes\\\" }\",\"method\":\"POST\"}",
        "payloadType": "json",
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "x": 90,
        "y": 120,
        "wires": [
            [
                "f215d67.f2e1728",
                "a730ac16.b8b86"
            ]
        ]
    },
    {
        "id": "963aed3b.3f1d4",
        "type": "cast-to-client",
        "z": "c5994873.184318",
        "name": "",
        "url": "",
        "contentType": "",
        "message": "",
        "language": "en",
        "ip": "192.168.86.247",
        "port": "",
        "volume": "",
        "x": 400,
        "y": 480,
        "wires": [
            []
        ]
    },
    {
        "id": "e729438c.82eb2",
        "type": "schedex",
        "z": "c5994873.184318",
        "name": "alarm clock",
        "suspended": false,
        "lat": "51.16569",
        "lon": "10.45153",
        "ontime": "",
        "ontopic": "",
        "onpayload": "wake up!",
        "onoffset": 0,
        "onrandomoffset": 0,
        "offtime": "",
        "offtopic": "",
        "offpayload": "wake up!!",
        "offoffset": "",
        "offrandomoffset": 0,
        "mon": true,
        "tue": true,
        "wed": true,
        "thu": true,
        "fri": true,
        "sat": true,
        "sun": true,
        "x": 150,
        "y": 480,
        "wires": [
            [
                "963aed3b.3f1d4"
            ]
        ]
    },
    {
        "id": "af88a4cc.908d68",
        "type": "function",
        "z": "c5994873.184318",
        "name": "construct Alarm msg",
        "func": "ontime = flow.get(\"ontime\")\nextra = flow.get(\"extra\")\nmsg.payload = \"ontime \" + ontime + \" onoffset \" + extra;\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 240,
        "y": 380,
        "wires": [
            [
                "e729438c.82eb2"
            ]
        ]
    }
]
biddster commented 4 years ago

I'm not sure I understand the issue at the moment.

For example, let's say you were trying this at 2020-01-29 20:44 (an hour or so before you raised this issue)

If my msg.payload is "ontime 23:11 onoffset 0" I get: ON 2020-01-29 23:11 (which works) Correct: the next available ontime that can be scheduled is actually today. 23:11 is after 20:44 today so I'd expect 2020-01-29 23:11

If it's "ontime 10:45 onoffset 12" I get: ON 2020-01-30 10:57 (that's 24hrs and 12 minutes offset) It's already 20:44, that's after 10:45 today so I'd expect the next ontime to be tomorrow. i.e. 2020-01-30 10:45 + 12 minutes

If it's "ontime 2.30 onoffset 36" I get on 2020-01-30 03:06 (that's 24 hours, 36 minutes Again, it's already 20:44, that's after 2:30 today so I'd expect the next ontime to be tomorrow. i.e. 2020-01-30 02:30 + 36 minutes

Am I correct in assuming your schedule time?

slurpey commented 4 years ago

I am simply very stupid. I guess I was tired. I assume the offset was in hours... oups. insted of "ontime 10:45 onoffset 12" - using "ontime 10:45 onoffset 720" works....

Apologies for the bother.

On Wed, 29 Jan 2020 at 23:50, biddster notifications@github.com wrote:

I'm not sure I understand the issue at the moment.

For example, let's say you were trying this at 2020-01-29 20:44 (an hour or so before you raised this issue)

If my msg.payload is "ontime 23:11 onoffset 0" I get: ON 2020-01-29 23:11 (which works) Correct: the next available ontime that can be schedule is actually today. 23:11 is after 20:44 today so I'd expect 2020-01-29 23:11

If it's "ontime 10:45 onoffset 12" I get: ON 2020-01-30 10:57 (that's 24hrs and 12 minutes offset) It's already 20:44, that's after 10:45 today so I'd expect the next ontime to be tomorrow. i.e. 2020-01-30 10:45 + 12 minutes

If it's "ontime 2.30 onoffset 36" I get on 2020-01-30 03:06 (that's 24 hours, 36 minutes Again, it's already 20:44, that's after 2:30 today so I'd expect the next ontime to be tomorrow. i.e. 2020-01-30 02:30 + 36 minutes

Am I correct in assuming your schedule time?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/biddster/node-red-contrib-schedex/issues/71?email_source=notifications&email_token=ACMPYTGTIZP5WKD3A5VCEYLRAIB3HA5CNFSM4KNMAEX2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEKJBLJQ#issuecomment-579999142, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACMPYTHFZSF72SJTKVY26ODRAIB3HANCNFSM4KNMAEXQ .

biddster commented 4 years ago

It's never bother. Asking questions is a fundamental right. Glad it's all working for you.