# Cron-Plus and using dynamic offsets

**URL:** <https://discourse.nodered.org/t/cron-plus-and-using-dynamic-offsets/45550>\
**Category:** General\
**Created:** [12 May 2021 15:13 UTC](https://discourse.nodered.org/t/cron-plus-and-using-dynamic-offsets/45550 "2021-05-12T15:13:04Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![eddee54455](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/eddee54455/32/43350_2.png) [@eddee54455](https://discourse.nodered.org/u/eddee54455)\
**Post date:** [12 May 2021 15:13 UTC](https://discourse.nodered.org/t/cron-plus-and-using-dynamic-offsets/45550/1 "2021-05-12T15:13:04Z")

</div>

@Steve-Mcl

Hi Everybody, especially you Steve!

I am playing around with cron-plus with a fairly specific requirement....

What I would like to do is to be able to push a variable offset via a payload to a standard schedule, ie Sunrise turn on at "+Offset1" and Sunset turn off at "-Offset2" .. I am a little stumped as to how to do it...

Any hints?

Unfortunately, being somewhat "programatically challenged", the sending and setting up of schedules for cron-plus is a bit... no, a lot above me....

Any help would be awesome!

Cheerz  
Ed

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [12 May 2021 15:33 UTC](https://discourse.nodered.org/t/cron-plus-and-using-dynamic-offsets/45550/2 "2021-05-12T15:33:34Z")

</div>

> [@eddee54455](#):
>
> What I would like to do is to be able to push a variable offset via a payload to a standard schedule, ie Sunrise turn on at "+Offset1" and Sunset turn off at "-Offset2" .. I am a little stumped as to how to do it...

This is best solved wih dynamic schedules.

there are examples built in (CTRL+I --\> examples --\> cron-plus)

Here is an example a bit more tailored to your requirement...

```auto
[{"id":"ac468e85.8f167","type":"cronplus","z":"553814a2.1248ec","name":"","outputField":"payload","timeZone":"","persistDynamic":true,"commandResponseMsgOutput":"output2","outputs":2,"options":[],"x":940,"y":1900,"wires":[["501f4aee.05dff4"],["3d1e22c7.b5752e"]]},{"id":"501f4aee.05dff4","type":"debug","z":"553814a2.1248ec","name":"timed events","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1150,"y":1860,"wires":[]},{"id":"ff58a37b.bda06","type":"inject","z":"553814a2.1248ec","name":"set offset -10","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"-10","payloadType":"num","x":510,"y":1860,"wires":[["c971aa62.795af8"]]},{"id":"c971aa62.795af8","type":"function","z":"553814a2.1248ec","name":"update sunrise","func":"var sunriseCmd = { \n \"command\": \"add\",\n \"topic\": \"sunrise\", \n\n \"name\": \"schedule1\", \n \"payloadType\": \"default\", \n \"payload\": \"payload\", \n \"limit\": null, \n \"expressionType\": \"solar\", \n \"solarType\": \"selected\", \n \"solarEvents\": \"sunrise\", \n \"location\": \"54.99906669127324 -1.4171075820922852\", \n \"offset\": msg.payload \n}\n\n\nmsg.payload = sunriseCmd;\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":740,"y":1900,"wires":[["ac468e85.8f167"]]},{"id":"6577857a.76064c","type":"function","z":"553814a2.1248ec","name":"update sunset","func":"\nvar sunsetCmd = {\n \"command\": \"add\",\n \"topic\": \"sunset\",\n \"name\": \"schedule2\",\n \"payloadType\": \"default\",\n \"payload\": \"payload\",\n \"limit\": null,\n \"expressionType\": \"solar\",\n \"solarType\": \"selected\",\n \"solarEvents\": \"sunset\",\n \"location\": \"54.99906669127324 -1.4171075820922852\",\n \"offset\": msg.payload\n}\n\n\nmsg.payload = sunsetCmd;\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":740,"y":1940,"wires":[["ac468e85.8f167"]]},{"id":"3f03e799.ba88f8","type":"inject","z":"553814a2.1248ec","name":"set offset +30","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"30","payloadType":"num","x":510,"y":1900,"wires":[["c971aa62.795af8"]]},{"id":"162ae172.db6f5f","type":"inject","z":"553814a2.1248ec","name":"set offset +30","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"30","payloadType":"num","x":510,"y":1980,"wires":[["6577857a.76064c"]]},{"id":"3be8cfe3.d4a0a","type":"inject","z":"553814a2.1248ec","name":"set offset -10","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"-10","payloadType":"num","x":510,"y":1940,"wires":[["6577857a.76064c"]]},{"id":"3d1e22c7.b5752e","type":"debug","z":"553814a2.1248ec","name":"status result","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1150,"y":1900,"wires":[]},{"id":"4ea6d77e.04d418","type":"inject","z":"553814a2.1248ec","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"status-all","payload":"","payloadType":"str","x":940,"y":1840,"wires":[["ac468e85.8f167"]]}]

```

the inject payload value is what is used for the offset. Provide this value from wherever you need (i.e. dashboard input).  
You will need to set your coordinates in the functions.

---

<div class="post-metadata">

**Author:** ![eddee54455](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/eddee54455/32/43350_2.png) [@eddee54455](https://discourse.nodered.org/u/eddee54455)\
**Post date:** [12 May 2021 15:51 UTC](https://discourse.nodered.org/t/cron-plus-and-using-dynamic-offsets/45550/3 "2021-05-12T15:51:57Z")

</div>

@Steve-Mcl

I only have one gripe......

You make it look so easy!!

Thanks heaps!! Saves me a whole raft of trouble!!

Regds  
Ed

---

<div class="post-metadata">

**Author:** ![eddee54455](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/eddee54455/32/43350_2.png) [@eddee54455](https://discourse.nodered.org/u/eddee54455)\
**Post date:** [12 May 2021 17:22 UTC](https://discourse.nodered.org/t/cron-plus-and-using-dynamic-offsets/45550/4 "2021-05-12T17:22:18Z")

</div>

@Steve-Mcl

Works a treat.... but I think I have uncovered a "feature" ... (Bear in mind, the more foolproof you make something, the more ingenious the fools become.... I am one of those fools!)

If I edit the payload field in the command and put in a 0 (zero)... When the timer trips a send:  
`msg.payload.config.payload`  
returns the value  
`"payload"`

Am I screwing it up or am I just missing something?

Tia  
Ed

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [12 May 2021 17:33 UTC](https://discourse.nodered.org/t/cron-plus-and-using-dynamic-offsets/45550/5 "2021-05-12T17:33:13Z")

</div>

If I understand, you want the schedules payload to be number 0?

In the function, set "payloadType" to "num" and "payload" to `0`

---

<div class="post-metadata">

**Author:** ![eddee54455](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/eddee54455/32/43350_2.png) [@eddee54455](https://discourse.nodered.org/u/eddee54455)\
**Post date:** [12 May 2021 17:41 UTC](https://discourse.nodered.org/t/cron-plus-and-using-dynamic-offsets/45550/6 "2021-05-12T17:41:16Z")

</div>

@Steve-Mcl

Fantastic!!

Spot on, exactly what I need...

What are the various payload type abbreviations that are available - I tried "number" but of course that didn't work...

However, in the cmd, when I tried this:  
` "payloadType": "num",`  
`"payload": 0,`

the status still showed"

```auto
name: "schedule1"
payloadType: "num"
payload: "payload"

```

Did i break something again....?

Regds  
Ed

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [12 May 2021 17:58 UTC](https://discourse.nodered.org/t/cron-plus-and-using-dynamic-offsets/45550/7 "2021-05-12T17:58:28Z")

</div>

> [@eddee54455](#):
>
> What are the various payload type abbreviations that are available - I tried "number" but of course that didn't work...

I'm sure they are listed in the built in help.

Off the top of my head, the options are default, str, num, json, bool, and some more I forget.

---

<div class="post-metadata">

**Author:** ![eddee54455](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/eddee54455/32/43350_2.png) [@eddee54455](https://discourse.nodered.org/u/eddee54455)\
**Post date:** [12 May 2021 18:05 UTC](https://discourse.nodered.org/t/cron-plus-and-using-dynamic-offsets/45550/8 "2021-05-12T18:05:45Z")

</div>

@Steve-Mcl

Shotto... found the list in the help, after reading it about 20x, the 21st's a peach!!

`payloadType: (string|optional) The payload type (e.g. 'default', 'flow', 'global', 'str', 'num', 'bool', 'json', 'bin', 'date' or 'env')`

Still can't seem to get it to send a 0 though, even with setting it to type "num".... Although, if I put in a "fixed" event, the status does reflect an impending 0... As to why it doesn't work with the dynamic side of things, I am clueless....

Tia  
Ed

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [12 May 2021 18:21 UTC](https://discourse.nodered.org/t/cron-plus-and-using-dynamic-offsets/45550/9 "2021-05-12T18:21:44Z")

</div>

My bad, the payload must be in quotes.

```auto
...
payloadType: "num",
payload: "0"
...

```

PS: if you are unsure of what the config should be for a dynamic "add" command, simply setup a static schedule of your liking, inject a msg with a topic of `status-all` and look at (copy) the config it outputs.

---

<div class="post-metadata">

**Author:** ![eddee54455](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/eddee54455/32/43350_2.png) [@eddee54455](https://discourse.nodered.org/u/eddee54455)\
**Post date:** [12 May 2021 18:31 UTC](https://discourse.nodered.org/t/cron-plus-and-using-dynamic-offsets/45550/10 "2021-05-12T18:31:28Z")

</div>

@Steve-Mcl

Thanks heaps for the help!!

Got it going now!!

And thanks big time for the tip:

> [@Steve-Mcl](#):
>
> PS: if you are unsure of what the config should be for a dynamic "add" command, simply setup a static schedule of your liking, inject a msg with a topic of `status-all` and look at (copy) the config it outputs.

This will make it waaaaaaay easier to do!!

Cheerz  
E

---

<div class="post-metadata">

**Author:** ![eddee54455](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/eddee54455/32/43350_2.png) [@eddee54455](https://discourse.nodered.org/u/eddee54455)\
**Post date:** [16 May 2021 11:13 UTC](https://discourse.nodered.org/t/cron-plus-and-using-dynamic-offsets/45550/11 "2021-05-16T11:13:53Z")

</div>

@Steve-Mcl

Report Back: Working as it should (With a minor work-around)...  
On dynamic schedule: When setting the output to "num" and using a payload "0", the node sends the output as a text string... When any other number is set as a payload, it sends as a numeric..

Workaround for me was to set the output to boolean and then put a function node on the output searching for T/F and replacing with 1/0...

If I remember correctly though, when using "fixed schedules" the node sends a 0 or 1 as a payload, no problem...

I have run into another problem that I am scratching my head about... (as a programming jnr it is quite a puzzle for me): The basic layout is such - Cron-Plus output to a Trigger Node which resends the output every 15sec on to mqtt.... Now, the problem that I am trying to solve is, how do I get Cron-Plus to send an output on a restart of Node Red? (When node red restarts, the trigger node does not get an input until the next dynamic schedule from Cron Plus comes to term....The Trigger node sits there waiting for an input until this happens, then all is well....)

Any suggestions?

Regds  
Ed

---

<div class="post-metadata">

**Author:** ![eddee54455](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/eddee54455/32/43350_2.png) [@eddee54455](https://discourse.nodered.org/u/eddee54455)\
**Post date:** [16 May 2021 15:00 UTC](https://discourse.nodered.org/t/cron-plus-and-using-dynamic-offsets/45550/12 "2021-05-16T15:00:48Z")

</div>

Ok, so here's my kludge to solve my problem... Only prerequisite is that Node Red be configured for a hard storage named "file" ....

```auto
[
    {
        "id": "9d134f23.f85e1",
        "type": "tab",
        "label": "Persistent Sender",
        "disabled": false,
        "info": ""
    },
    {
        "id": "280f05b7.c7de22",
        "type": "function",
        "z": "9d134f23.f85e1",
        "name": "Resend Stored",
        "func": "if(msg.payload == \"Resend\"){\n msg = context.get(\"Data\",\"file\");\n}\nelse{\n context.set(\"Data\",msg,\"file\");\n}\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 420,
        "y": 120,
        "wires": [
            [
                "6b74b2d.b196a4c"
            ]
        ]
    },
    {
        "id": "e4b756c.22ef528",
        "type": "inject",
        "z": "9d134f23.f85e1",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "SomeData",
        "payload": "",
        "payloadType": "date",
        "x": 190,
        "y": 100,
        "wires": [
            [
                "280f05b7.c7de22"
            ]
        ]
    },
    {
        "id": "6b74b2d.b196a4c",
        "type": "debug",
        "z": "9d134f23.f85e1",
        "name": "",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 610,
        "y": 120,
        "wires": []
    },
    {
        "id": "5f3c1e63.c80df8",
        "type": "inject",
        "z": "9d134f23.f85e1",
        "name": "",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "3",
        "crontab": "",
        "once": true,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "Resend",
        "payloadType": "str",
        "x": 430,
        "y": 70,
        "wires": [
            [
                "280f05b7.c7de22"
            ]
        ]
    },
    {
        "id": "5d4a2b10.77697c",
        "type": "inject",
        "z": "9d134f23.f85e1",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "SomeData",
        "payload": "0",
        "payloadType": "num",
        "x": 220,
        "y": 140,
        "wires": [
            [
                "280f05b7.c7de22"
            ]
        ]
    },
    {
        "id": "d0d3811.ab1d28",
        "type": "inject",
        "z": "9d134f23.f85e1",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "OtherData",
        "payload": "1",
        "payloadType": "num",
        "x": 220,
        "y": 180,
        "wires": [
            [
                "280f05b7.c7de22"
            ]
        ]
    }
]

```

Hope other newbies like me can find it handy....

Regds  
Ed

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [30 May 2021 15:01 UTC](https://discourse.nodered.org/t/cron-plus-and-using-dynamic-offsets/45550/13 "2021-05-30T15:01:04Z")

</div>

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