Node-Red Scheduler

I’m Peter Scargill and this is a scheduler for Node-Red (well, more of a timer really but someone surely has already used the term timer) for Node-Red which includes days and months, dawn and dusk with optional offsets and a time-out mode, You may notice this has improved since the first version – I’m learning as I go along.

As you may know, I’m interested in using Node-Red on the Raspberry Pi (though for this purpose it could be any piece of kit) to control ESP8266 boards via a variety of mechanisms, one of them being MQTT.

Up until recently I would use a Node-Red standard Inject function to trigger an event, lets say once a day. The Trigger function however is very limited. It does not know about months and it has no idea about concepts such as “dusk” and “dawn” which you might reasonably want to use for a porch light.

So, I buckled down and learned how Node-Red nodes were made (with some help later from dceejay Dave and others in the googlegroups node-red group – for which I’m grateful) – and the first thing I did was to start off with the sunrise/sunset node (you’ll need that installed as I rely on one of their routines) to send the time, date, sunrise and sunset information to my boards based on longitude and latitude. In essence, in my case I use MQTT to tell boards about the time when they power up and at 12 hour intervals and in the meantime they maintain their own time in software.

However the more I get into Node-Red the more I see it doing the actual control… so the next thing I did was to sit down and wrote a node – again borrowing from the Sunrise calculations for dusk and dawn settings but gutted to actually send one of two messages (settable) to a message topic (settable and suitable for passing onto an MQTT node for example) at any given time with presets for dusk and dawn and allowing control over days and months as well as a time-out function (i.e. turn on at 10am for 5 minutes). So a typical example might allow us to turn something on every Wednesday at dusk until midnight but only from November to January. My node of course does not actually send an MQTT message – it just passed the topic and message onto the relevant node (the MQTT node shown in purple here). You could use that message for a variety of other purposes – like sending you a tweet at regular intervals. That is so easy in Node-Red.

The code handles summer time adjustments (well, it did this time, you might want to check that very carefully). You can even offset the dusk and dawn offset times in minutes to save, say, lighting energy, I find garden lights come on WAY before you need them – hence building in the offsets.

Here’s what it all looks like.

So every minute, my node (in blue) self-triggers (my thanks to dcjay for showing me how to add JS timers) to do it’s calculations and decide whether or not to send a message to the MQTT output node in purple (you could easily alter the code to do something other than send an MQTT output. You will notice in the image above that there are now TWO outputs.. the top output sends a message on change – i.e. when it wants to turn something on or off.. the lower output sends a simple 1 or 0 message every minute regardless – think of it as a regular status output just like the little text and icon indicator under the node itself in the picture above. In this case each output is also sending out a debug message to the debug window in green – that’s just for testing purposes.

If you double-click on my scheduler node, here’s what you get…

Simple enough, give it a name (any name), on-time and off-time which includes every 15 minutes of the day and dusk and dawn dropdown options as well as timeout options for off time… then set your longitude and latitude (from Google Maps or memory if you are like that), the topic you want to send and the two messages – one to turn something on and another to turn something off. In my case I have an MQTT message system going to ESP8266 units – I’ve detailed this elsewhere in the blog.

Tick or un-tick boxes for the weekday/month combination you want. The code is checked once every minute but the top output only sends something out at power up or when the state changes.

If this isn’t powerful enough for you? – let’s say you want something to happen twice a day? – put two of them in there.

So – to make this work – i.e. to make your own node there are two files – and you just create a directory in your node-red/node-red-nodes folder called scheduler, dump these two files in and restart Node-Red.

The two files are the HTML file and the .JS file called scheduler.html and scheduler.js respectively.

If you make good use of this mode, a credit would be encourage me to support it.

Also if you spot any mistakes especially in the timing – or have ideas for improvement – do let me know.

The purpose for the timeout to the end time options – is so you can say for example – turn a sprinkler on every night for 5 minutes after dusk. And that’s exactly what I plan to use it for – but only in the summer months!

Attach for example an MQTT node onto the first output and put your broker details in there. The node will pick up the topic and message from my node.

And finally – this code is deliberately NOT constantly sending out the ON instruction every minute but only when the state changes. At any time you can press the button on the left of the node to force an output of the currently relevant message.

This node is relatively simple but I think that’s the point. It still took me several days to get it right and I’d appreciate any testing reports. My next job is a thermostat node and that’s coming along nicely.

It’s down to time – it is a learning to use Github and I’m spending what time I have improving the code. As of 15 mins ago I’ve fixed a timing problem – did it completely the wrong way before…and to be honest right now having just a few people playing with this works for me. So – get the latest, read the blog – it now shows you how to add a test injector – and in timer mode the status is updated underneath the node itself. This is now a very useful tool. Just needs testing to make sure it will work utterly reliably 🙂 Thanks for comments and fixes so far – keep them coming. Like – do we want two on-off times or are we happy to use 2 modes for that…

I can’t seem to get these to work. I’m using the latest NR release on a Pi2. Putting these files in my user nodes folder gives me an error that scheduler.js cannot be found. If I put them in the node-red/nodes folder, I get the error:

In fact, the whole things was a mess, I had to correct 3 different sets of quotes and a load of formatting issues.

I took the liberty of rationalising some of your block statement formatting too – the use of if and other blocks without {} is generally frowned upon as it can lead to logic errors that are hard to spot. Especially as you have a lot of nested if’s some in different styles.

Drat! It’s no good, there is something else wrong that I cannot spot I’m afraid. I managed to get rid of the obvious errors but it will only load from the node-red/nodes location (rather than user nodes locations) and when I try, it hangs the admin interface.

Any chance you could send me copies of the working files? Or post them to the Google group?

Well of course it was a mess – I’m still developing it! If you’d care to send your update I’ll go through it as I’m busy making changes right now. I suggest at this point it would not be sensible to put it on a github for the same reason I have not. It’s new, I’m making it up as I go along and getting feedback. At some point in the future I’ll stick it up on NPM. Thanks. Oh, my email address… pete at scargill.org if you’d care to send your updates.

Hi Peter, thanks for doing that. The email address is valid by the way, I just use a different address on every site! Helps manage spam issues.

As you’ve probably realised, you don’t need to worry about putting something half finished on github, most of the stuff there NEVER gets finished! Including most of mine – I’m TotallyInformation on github btw. In fact, the only fully finished repo I have is the NR moment node.

I’ll grab a new copy from github and get that to work first then I may send you some suggested reformatting if you are happy.

Hah, well yes I had gathered that a lot of stuff there never gets finished.. Thankfully I’ve found a few. Well I have 2 items up there (the first one I did so long ago I could not remember how to use GitHub. However now I have the Github desktop for Windows it is all coming back. I’ve added the icon up there as well and yes I did a little reformatting of the code. I’m new to node-red but I’ve been programming for some time and I do not like the way automatic formatters work I’m afraid. I generally use NotePad++ who’s formatting plug-ins are not that clever. I eventually formatted the JS file on Eclipse (Windows) but for reasons beyond me if I try to edit an HTML file it simply tries to display it in an internal browser so that was by hand in Notepad++

If I have my timings right this should work a treat now and I can concentrate on my thermostat node (which I urgently need as we’re moving house shortly!!)

I’ve just bought a couple of these: http://www.ebay.co.uk/itm/271491679485. We are in Spain and not on mains water so we have to watch every drop. I’m hoping to water only when needed. Sun, wind, temperature and rain all affect the water requirement so maybe testing soil humidity is the answer?

Ah now I’ve always wanted to talk to someone who has bought one of these. Is this going to simply corrode in the soil? I thought you had to use stainless steel? That is exactly what I want too. I could read the analog value from a sensor via MQTT and then apply a timer accordingly… I have a watering system which runs on a 12vc solenoid (or will when I take the replacement 12v power supply over there in May – the last one died due to rubbish electricity). I’ve looked at these before and just though… corrosion… Have you done anyhing with these?

We will have to wait and see. There is a very similar device available that has a relay built in to open a water valve so they must be intended for permanent installation. If it does corrode then I’ll try different metals. The copper plated steel earth spike for the power system doesn’t corrode and, I guess as the currents are miniscule then galvanic erosion shouldn’t be an issue. I’m going to use one of the 24v irrigation valves that are very common in the merchants here.
On another issue, I’ve just ordered a ready built unit from Sophia, Bulgaria. http://www.ebay.co.uk/itm/251890956930. It looks nicely made, mains powered and perfect for a job I have. He has managed to set it up as a webserver on first power up to allow configuration from a browser which is clever. It then switches over to MQTT but (I think) still allows browser access as well. There are masses of code on GitHub for it, follow the WiKi link.

Yes, your point about galvanic current – that’s what I was thinking about – corroded battery compartments etc.. but if the current is tiny that’s not such an issue. I note the copper is tin-plated.. I don’t know whether that’s a good or a bad thing in a plantpot – do let me know how that turns out. I would have thought stainless steel was the answer but then you can’t plate that onto a board.

We’ve been working on VERY similar software. I’ve developed the MQTT software (TUAN’s code) to do all sorts of IO magic on the ESP-12 and right now we’re trying to integrate the power-up-web-server part but it’s messy right now and throwing out all sorts of warnings.. I really want to end up simply with a subroutine call at the start of my code that if a button is pressed goes off and creates a temporary website. The code that is out there involves web page compression that won’t compile under Eclipse/windows etc… (there is no need for images on a simple setup web page so I’d really like to find a much simpler version). That board looks good – not cheap though.

Hi Peter, you probably saw that I cloned your repo to my own (TotallyInformation) any changes I’ve made have been committed back to there. Mainly just sorting out {} brackets and other JSHint issues.

I used to use NP++ but switched to SublimeText and then to Adobe Brackets, about the only good thing they produce! It is actually written using node.js. Has many of the features of Sublime but is a lot faster. With JSHint instead of the default JSLint and with the interactive linter plugin, you can immediately spot any issues.

For some strange reason I can only run your node if I put it under the main node-red/nodes folder not in the userDir/nodes folder. Odd but at least it works for now. I need to rethink my strategy for scheduling now I’ve moved everything onto my new Pi2. But I’m doing some work on email integration first. I want to be able to control NR by email and to be able to send info out on request. The last part is easy, the former not so much.

Also – I tried Brackets. Looks good but if you take the scheduler.html file, dump that in there and mess up the formatting of the options for example – changing the indents on individuals – I could not find a beautifier for Brackets that would fix this for you? Ideas?

Hi again, I can simply update from your repo on a different branch and merge if I need to. Or if you let me know if you take the changes you want (if any) back into your code, I’ll just blitz it.

Re Brackets. There is Beautify (https://github.com/drewhamlett/brackets-beautify) which I’ve used to reformat HTML. But generally I don’t do that to JavaScript as I tend to want to understand the code as I go so I tend to reformat as I’m reading. Rather like I did with your code. In any case there are, of course, a number of different “house styles” and my style wont suite everyone (I hate having opening curly brackets on their own line for example as, to me, it breaks the pattern of reading – probably because I started life with COBOL, PL/1, APL and other high-level languages).

Thanks for the info Julian. I’m VERY new to GIT but as I understand it you can indeed make a copy and do your stuff. I would not mess about though as I’m about to make more changes:-) Right now I’m setting up a second Raspberry Pi2 with knowledge I’ve gained recently so as to have a “clean” version – as against the current one which is full of abandoned experiments 🙂

Peter,
Since your post about node-red I’ve been getting deeper into it and am finding it really useful, I’ve just come across your scheduler node and have been trying to get it to work on my RPi, first model, but can’t seem to get it to appear in the list of nodes.
I’ve tried copying it to various folders but it just doesn’t seem to be recognised, will it work OK on the earlier RPi? and any suggestions as to where it should go?

Thanks for your job on scheduler. Just a question… I don’t answer the folowing sentence :
“The purpose for the timeout to the end time options – is so you can say for example – turn a sprinkler on every night for 5 minutes after dusk. And that’s exactly what I plan to use it for – but only in the summer months!”
How do you specify the timeout and where?
Additonnaly, would you please explain the usage of {out1,1} option? Is it possible to add an other term ( like {out1,1};TRUE )? What separator have to be used?

Hello Peter,
Been testing your scheduler for a while and it is working fine. I looked deeper into it, actually wanted to extend the options to include also 2359. Then I saw something, for start value options starting from 1290 it looks like this:
21:30
21:45
22:00
22:15
22:30
22:45
23:00
23:15
23:30
23:45
Dawn
Dusk

Dear Pete,
I am about testing TheThingBox distro a bit and I wanted to install your Scheduler there. The thing is, I hardly remember how I did it earlier…just something tells me it was done manually…or was it possible with npm?

NO guarantees but here’s what we use in a script – so assuming you’re in the node-red directory in a terminal – I now use WINSCP but not that horrible terminal – I installed putty and there’s an option in WINSCP to use putty – makes life easy as the right mouse button does a paste from windows..

I’ve no idea – my code is not in any registry and I’ve no great desire to ensure it is – a bit of a cheap the authors of that who are also volunteers… however… here without anything missing is what we actually have in our script – bearing in mind that you should be in your node-red directory when you do this – or if you set it up as root, in something like /usr/lib/node_modules (don’t ask me why – logically I’d have expected the node-red directory to be in the root folder if a root user set it up – but then I’m only a Linux relative newby .

Move the newly created ‘node-red-contrib…’ folders from userdir to /node_modules
Navigate to /node_modules
Open the folder ‘node-red-node-suncalc’
Copy the subfolder ‘node_modules’ to a disk (to the left side of WinSCC gui)
Navigate now to /node_modules/node-red-contrib-scheduler
Copy the ‘node_modules’ folder to here
Repeat this for node-red-contrib-duskdawn

5) In WinSCP

Navigate back to /userdir and clean up downloaded files if needed

6) In Putty

Reboot

7) Once rebooted, you should find nodes in ‘advanced’ section for sunrise, scheduler and duskdawn

Thanks for the info. I just get loads of errors when I try it.
I think I’m going to start from scratch, I updated to the latest Node red and I have node red folders all over the place and I’ve lost the plot, some things install OK and others don’t ??

Don’t knock yourself – it can be a pain to set up…. one option is – if you look at the home control page…. my friend made a script (which I can’t get to work on a PI yet) for those DROPLETS – explained in “Home Control 2015 or at least linked to – you get a month’s free trial – it’s basically linux online….. and the script sets up pretty much everything we do without any manual intervention – including Node-Red and all the various nodes… costs nothing but time if you want to give that a go.

Thanks Walter and Pete,
Followed the instructions and now got it working, good tip on using Putty from within WinSCP it made navigating around the folders a lot easier with copy the path from WinSCP to putty.

Now to get the S20 socket working from scheduler, I think that maybe a tad more difficult!

I’m new to node-red and love this module,
I have a home automation set up, using your module and XRF (radio) light and temp sensors.
I’m just wondering where the module gets its time from as in the UK we move our clocks back and fore twice a year.
I’ like to get the time from a local server.
Thanks again for a great module

Raspberry Pi, running Thingbox. Pi configured to local time + DST. Debug window shows correct (DST) time.
Scheduler installed via npm Dusk dawn seems to work fine.
However I need to select a time 1 hour earlier than required to switch on / off lights. easy enough.
DST finishes at midnight tonight, so I’ll see what happens then.
Once again thanks for a great module.
Tony

hi. I just can’t get this recognised by node-red. when you say “install” do you mean just copy the files across to the required directory? i’ve done this and also issued the npm command as per their site but neither has resulted it in the node appearing in my menu bar. please can you let me know if i’m doing something wrong? i’m using thethingbox and have placed the files in root@thethingbox ~/thethingbox/node_modules/node-red/node_modules Others in that folder are in folders called ttb-node-NAME so I’ve tried that too but no joy.thanks, don

Hi,
I am a beginner in Raspberry pi and Node-red. I actually have a Raspberry Pi 3 I need to install the Scheduler Node but it doesn’t work.
I used that command to install the Scheduler Node : npm install node-red-contrib-scheduler
I don’t know if it isn’t compatible with the latest version of RPi or i need to install something else ?
Please help me !!

So.. sitting in your raspberry pi home page go to the .node-red folder

i.e. cd .node-red

From there (assuming you are in there as user PI) the command you gave should work – you do, when finished have to restart node-red – or reboot as you prefer. This applies to all node installs – they don’t normally show up until you restart node-red. If you do this from the right directory – you should find scheduler at /home/pi/.node-red/node_modules

Yes things have changed a little and I’m updating my info – I have mine running in /home/pi/.node-red/node_modules now but if that is working for you – that is fine… suggest you scrap scheduler because it may have bugs – I stopped developing it some time ago – BIGTIMER is much, much better and you can use as many of them as you like in a project without any problems.

Comments are closed.

Search for:

Welcome

Hi - I hope the all-new top banner is now more descripive. Let me know if I missed anything important. I hope you like it. I'm Pete and this is my (big) technology blog.

Use the search box or check out the archives and other links below - be sure to SHARE what you like using the social media buttons and please subscribe to my Youtube Video Channel

Please remember to register or log in - the top menu changes a lot when you are part of the party. Please note that registrations with no name or suspicious looking email dresses will be considered to be spammers and will be deleted. Don't forget to tick the box so you will get email follow-ups to comments. We've a great community in here. Spammers and "guest post" companies beware - you have NO chance of success and a good chance of getting spammed badly in return.

To buy me a coffee or help fuel my gadget habit, please use the Paypal donate link below.

Disclaimer: Because I have no idea of your level of technical skill or the requirements of your country laws in terms of electricity supply etc, I accept no responsibility for any damage attributed to following advice in these pages. When dealing with wall outlet (high) voltages you should satisfy yourself that whatever you are doing is safe and if unsure, seek advice from someone who is sure.

Notice: I'm always happy to offer advice on stuff I've written - and indeed take advice. If I can help in any way just let me know but PLEASE don't ask me how to program in C/PASCAL/NODE/etc. There are many resources out there - if you want to program and can't - there's always Google.

Email Newsletter

Enter a name and your real email address if you would like to receive occasional summary emails - sorry but invalid-looking or automated names and email addresses will be removed to help protect others.

Links

EE Times | Electronic Engineering TimesEE Times connects the global electronics community through news, analysis, education, and peer-to-peer discussion around technology, business, products and design