# Cronplus - trigger based on schedule condition?

**URL:** <https://discourse.nodered.org/t/cronplus-trigger-based-on-schedule-condition/58215>\
**Category:** General\
**Created:** [12 February 2022 11:57 UTC](https://discourse.nodered.org/t/cronplus-trigger-based-on-schedule-condition/58215 "2022-02-12T11:57:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![hazymat](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hazymat/32/108159_2.png) [@hazymat](https://discourse.nodered.org/u/hazymat)\
**Post date:** [12 February 2022 11:57 UTC](https://discourse.nodered.org/t/cronplus-trigger-based-on-schedule-condition/58215/1 "2022-02-12T11:57:56Z")

</div>

My apologies if I missed this within the excellent docs I have read so far.

Can cronplus node act as a condition for incoming triggers? For example, I have a door sensor which I want to trigger a light to come on, but only when it's dark (i.e. within a particular schedule as defined by cronplus?)

It's very easy to do with the gate node, but I just wondered if this was built in?

A reason why it might not be so good using a gate node is as follows: I have to wait for the cron condition to set that gate. For example, I may restart Node-RED and then nothing will work until the next cron event.

---

<div class="post-metadata">

**Author:** ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)\
**Post date:** [12 February 2022 12:04 UTC](https://discourse.nodered.org/t/cronplus-trigger-based-on-schedule-condition/58215/2 "2022-02-12T12:04:45Z")

</div>

Yes you can use cron-tab to set a flow variable. In combination with a switch, node you can steer if a motion event will pass the switch node or not.

With an inject node you can initialize this flow variable as well, to have a defined condition.

You can even use only a switch node which can decide directly if the motion event will pass without any scheduling event, just by testing the daytime when the event occurs. In this case not even an initialization is necessary.

---

<div class="post-metadata">

**Author:** ![hazymat](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hazymat/32/108159_2.png) [@hazymat](https://discourse.nodered.org/u/hazymat)\
**Post date:** [12 February 2022 12:16 UTC](https://discourse.nodered.org/t/cronplus-trigger-based-on-schedule-condition/58215/3 "2022-02-12T12:16:29Z")

</div>

> [@mickym2](#):
>
> set a flow variable

I like the idea of using an environment variable like a gate node, it just shows there are so many different ways to achieve the same thing in Node-RED.

> [@mickym2](#):
>
> With an inject node you can initialize this flow variable as well, to have a defined condition.

Yes this is exactly what I'm asking, how to do this? How to set the state of a variable based on a _condition_ (i.e. open if dark, closed if light), not just setting it manually (i.e. open). How do I inject into the cronplus node, to achieve an outcome based on the current time of day?

I understand that the cronplus node will send a single message when it changes from light to dark (based on sunrise / sunset), which can set up a variable / gate status / etc. However I still don't understand how to dynamically test for those conditions, e.g. if the variable / gate status has not yet been set?

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [12 February 2022 12:36 UTC](https://discourse.nodered.org/t/cronplus-trigger-based-on-schedule-condition/58215/4 "2022-02-12T12:36:11Z")

</div>

> [@hazymat](#):
>
> How do I inject into the cronplus node, to achieve an outcome based on the current time of day?

You dont have to inject anything to Cron plus .. the opposite ..  
as @mickym2 said configure Cron plus (solar events) to inject and set a flow context variable that defines whether its day or night (a variable that can be later used in other parts of your flow to block execution)

**For Example:**

```auto
[{"id":"f58f72032eb7bd50","type":"cronplus","z":"54efb553244c241f","name":"","outputField":"payload","timeZone":"","persistDynamic":false,"commandResponseMsgOutput":"output1","outputs":1,"options":[{"name":"day","topic":"day","payloadType":"bool","payload":"true","expressionType":"solar","expression":"0 * * * * * *","location":"35.36038458458532 32.87109375","offset":"0","solarType":"selected","solarEvents":"sunrise"},{"name":"night","topic":"night","payloadType":"bool","payload":"false","expressionType":"solar","expression":"0 * * * * * *","location":"34.9291775968022 33.046875","offset":"0","solarType":"selected","solarEvents":"sunset"}],"x":540,"y":1260,"wires":[["b3f1ab45e814b0b8"]]},{"id":"b3f1ab45e814b0b8","type":"change","z":"54efb553244c241f","name":"","rules":[{"t":"set","p":"isDay","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":720,"y":1260,"wires":[[]]}]

```

If its day .. `flow.isDay` will be true  
if its night .. `flow.isDay` will false

---

<div class="post-metadata">

**Author:** ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)\
**Post date:** [12 February 2022 12:48 UTC](https://discourse.nodered.org/t/cronplus-trigger-based-on-schedule-condition/58215/5 "2022-02-12T12:48:16Z")

</div>

So you can directly ask the status of the cronplus node if it is day or night.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/0/00617cea0fb7e9ba35573fde76c7eb761d219174.png)

The result is in

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/9/f93fcf84306140df0155b260c1a0ea34116f92ea.png)

```auto
msg.payload.result.status.solarState.state

```

In a switch Node you can directly ask the content of the flow variable.

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/1/9199d2fb8a0f83c45be2e1ceabb71b1ed52b7549.png)

And the inject Node will initialize the flow variable.

You can also use directly bool- variables as the result of the crontab node is very detailed:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/1/41ffb0e7f618c109e1ead535902f40492b365947.png)

---

<div class="post-metadata">

**Author:** ![hazymat](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hazymat/32/108159_2.png) [@hazymat](https://discourse.nodered.org/u/hazymat)\
**Post date:** [12 February 2022 12:56 UTC](https://discourse.nodered.org/t/cronplus-trigger-based-on-schedule-condition/58215/6 "2022-02-12T12:56:15Z")

</div>

> [@mickym2](#):
>
> directly ask the status of the cronplus node if it is day or night

Ahhh - that's exactly what I wanted to know. I just didn't know how to query the cronplus node for its current status, but that explains it.

To be honest, I thought sending "status" to the cronplus node would confirm the status of the schedule, i.e. whether that schedule was enabled or disabled, and other metadata. I didn't try it, because I didn't even think it would "trigger" a query to see whether that schedule was currently active.

Thanks!

---

<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:** [26 February 2022 13:05 UTC](https://discourse.nodered.org/t/cronplus-trigger-based-on-schedule-condition/58215/8 "2022-02-26T13:05:41Z")

</div>

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