Better scratch your balls than your frozen windshield – MQTT warning for nightly low temperatures with Node-RED querying a weather API
I have a talent to forget to cover my car before a night with temperature below zero and the next morning I have to scratch away all the ice. As I really don’t like that I decided to build a warning mechanism for that.
I already had a Node-RED flow querying the weather API to publish the current outside temperature at my broker. This one required a little more data handling and configuring my MQTT app to show the content of the subscibed topic as notification. Nice bonus – my wrist band also vibrates and shows the message.
The flow should query the weather API once a day at 6 p.m. and check for temperatures below -1°C in the upcoming night (18 to 7 the next morning). If any value matches Node-RED generates a message with topic ‘warning’ and payload: ‘Temperatures down to {lowest nightly value}°C this night! Cover your car!’. The tile in the smartphone app MQTT Dashboard subscribed to that topic has a notification configured whoch extracts the payload.
The weather API used also offers the value for lowest temperature of the day, but this also included the values of the already passed early hours of the day. As I wanted only the values of the upcoming night I had to filter them myself.
The API also offers hourly values of temperature (and many others more) for the next seven days. The hourly values share the index of the hourly timestamp, so it was pretty easy to figure out which indeces of temperature I needed to grab. 18 to 31 reflect the time between 6 p.m. and 7 a.m.
With the help of some JavaScript methods it was easy to slice the array of temperatures to the values required, check if the temp is below -1 and if yes take the lowest value to include in the warning message.
We will be using the inject node to trigger the flow every mondy to thursday at 6 p.m. The http request will query the weather API with and return the requested values as JSON. The URL has to be configured here (for the weather API I use). Find your geo location and check temperature_2m under hourly weather variables. This will provide the above mentioned hourly temp values for the next seven days. The following function node selects the relevant values, checks them for below -1. If so it grabs the lowest one and adds the warning text that will be displayed. If the temperature doesn’t drop below -1 that night no message will be returned and therefore no warning published. Finally the mqtt out node sets the topic to ‘warning’ and published the msg to the broker.
If you have any problems checkout the debug node and the debug window to see what outputs your nodes are producing.
msg.payload.hourly.temperature_2 is an array which offers the method slice. The resulting array stored in nightly_temps will be from index 18 to 31. This array is then checked for values below -1 with the method some(n => n < -1). If this return true, the message payload is set to the provided string. In it we add the lowest number of the nightly_temps with Math.min(…nightly_temps)
Checkout the free app (without ads) MQTT Dashboard (or any other) which supports notification for subscribed topics. I created a simple tile which subscribes to the topic ‘warning’ and is setup to send a notification with title ‘Warning’ and message ‘<<value>>’ which will be replaced by the String built in the function node.
That’s it – have fun!