Using NodeRED To Create A Weather Watchdog

If you own a Tempest weather station by Weatherflow

You can use NodeRED to create customized alerts and send them to any destination you like. I had created python scripts to do this, but though it worked ok, it was not ideal. I hadn’t used NodeRED before and I always wanted to learn how I could use it to do something useful. Certainly it is way more capable than this simple tutorial, but this should give you an idea to jumpstart your thinking.

Setup NodeRED

I opted to run nodered as a docker container just for quick and easy setup. The process is pretty easy and straightforward

docker run -it -p 1880:1880 -v myNodeREDdata:/data --name nodered nodered/node-red

What this command does in detail:

docker run              - run this container
    -it                     - attach a terminal session so we can see what is going on
    -p 1880:1880            - connect local port 1880 to the exposed internal port 1880
    -v node_red_data:/data  - mount a docker named volume called `node_red_data` to the container /data directory so any changes made to flows are persisted
    --name nodered        - give this machine a friendly local name
    nodered/node-red        - the image to base it on - currently Node-RED v1.2.0

Once the command completes, you should be able to browse to http://{host-ip}:1880 and the nodered dashboard should display where you can start building your first flow. My weather watchdog flow looks like this:

A short description of what is going on here: on the left the purple nodes are the weatherflow nodes which I installed (more on that later) and setup with my weatherflow API token. The beige nodes are the processing functions, and each sends a copy of its output to the BB Alerts Pushover notification node and the Data Debug node. The data debug node helps you see what your flow is doing and also for troubleshooting, as you’ll be able to see exactly what your processing functions are outputting.

Setting up a weatherflow API token is not covered in this article, you can get that info HERE. To install the Weatherflow nodes, you go to the menu (the hamburger icon in the top right) and select “manage palette”

You will then click “Install” and search for “weatherflow” and install the two items as shown:

Once installed, you’ll see these new nodes on the left of the dashboard to choose from:

Select (drag) the nodes you wish to use to the build area and double click them to configure them using your API token. Once configured, you’ll need to create functions to generate and pipeline the alerts you want to process and send. For example, if I want to alert when the UV index exceeds a value of 8.0 and generate a warning, and then another message when it falls to a safe level, this is what my UV function looks like: (copy and past to your favorite text editor for more sensible reading)

var lastSentValue = context.get('lastSentValue') || { value: 0, time: 0 };
var currentValue = msg.payload.uvIndex;
var currentTime = Date.now();

if (currentValue >= 8 && (!lastSentValue || lastSentValue.value < 8)) {
    // Send the high UV index message and update the last sent value
    context.set('lastSentValue', { value: currentValue, time: currentTime });
    var now = new Date();
    var options = { timeZone: 'America/New_York' };
    var localTime = now.toLocaleString('en-US', options);
    msg.payload = "UV Index is now: " + currentValue + ". Recommending precautions to limit sun exposure.\n" +
                    "For realtime current conditions in Bridgeberry: https://tempestwx.com/station/73221/grid";
    msg.topic = "High UV Index Alert (" + localTime + ")";
    return msg;
} else if (currentValue < 3 && lastSentValue.value >= 8 && (currentTime - lastSentValue.time) > (2 * 60 * 60 * 1000)) {
    // Send the low UV index message and update the last sent value
    context.set('lastSentValue', { value: currentValue, time: currentTime });
    var now = new Date();
    var options = { timeZone: 'America/New_York' };
    var localTime = now.toLocaleString('en-US', options);
    msg.payload = "UV Index has dropped to: " + currentValue + ". Yay! it is now safe to enjoy the outdoors.\n" +
                    "For realtime current conditions in Bridgeberry: https://tempestwx.com/station/73221/grid";
    msg.topic = "Low UV Index Alert (" + localTime + ")";
    return msg;
} else {
    return null;
}

Once the data flows through this function is is evaluated and an action is taken. The generated alert is then pipelined to a Pushover node. I chose pushover because the experience is great on all platforms and if you have a smartwatch, the alerts render nicely. Alerts on Pushover are sent as push notifications and when using their API, there is little if any delay. You could choose a different messaging destination or feed the data into some other process based on your needs. Nodered makes it as simple as “wiring” the nodes together as you see in the example above. If you want to use Pushover, you’ll need to install the Pushover nodes in the same manner as the weatherflow ones. You will want the “node-red-contrib-pushover” node package and you will need to create an API user token with pushover (see their documentation).

Leave a Reply

Your email address will not be published. Required fields are marked *