I’ve been diving into the world of Node-RED lately, and I must say, it’s been an incredible journey! For those who might not be familiar, Node-RED is this amazing tool that allows you to wire together hardware devices, APIs, and online services in new and interesting ways. It’s like LEGO for the internet of things!
So, my latest project has been integrating weather data into my smart home setup. I wanted to automate my garden watering system based on weather forecasts. Sounds simple enough, right? Well, let me walk you through my adventure.
First off, I set up a Node-RED flow to fetch weather data using the OpenWeatherMap API. The data comes in as a JSON object, and I was particularly interested in the precipitation values for the next two days. The payload looked something like this:
msg.payload : {
“weather.openweathermap”: {
“forecast”: [
{
“datetime”: “2024-07-16T12:00:00+00:00”,
“precipitation”: 3.76
},
{
“datetime”: “2024-07-17T12:00:00+00:00”,
“precipitation_probability”: 100
}
]
}
}
My goal was to extract precipitation and precipitation_probability from this data. Initially, I was a bit stuck on how to parse this nested JSON structure. After some research and trial and error, I figured out that using the change node in Node-RED with a JSONata expression was the way to go. Here’s what I did:
-
Extracting Precipitation Data:
-
I used the
changenode to create a new payload that extracts the required fields. -
The JSONata expression looked like this:
{
“today”: $.weather.openweathermap.forecast[0].precipitation,
“tomorrow”: $.weather.openweathermap.forecast[1].precipitation_probability
} -
This transformed the payload into something more manageable:
{
“today”: 3.76,
“tomorrow”: 100
}
-
-
Setting Up Helper Variables:
- I then used the
setnode to store these values in global variables namedtoday_precipitationandtomorrow_precipitation_probability. - This allows me to reference these values in other parts of my flow.
- I then used the
-
Automating Garden Watering:
- The final piece was setting up an automation rule. I used a
switchnode to check iftoday_precipitationis above a certain threshold or iftomorrow_precipitation_probabilityis high. - If either condition is met, the flow sends a command to my smart sprinkler system to adjust watering schedules accordingly.
- The final piece was setting up an automation rule. I used a
This setup has been a game-changer for my garden. It ensures that my plants get the right amount of water without overwatering, especially when the weather forecast predicts rain.
If you’re looking to integrate weather data into your smart home, I highly recommend exploring Node-RED. It’s incredibly versatile and has a vibrant community that’s always ready to help. Happy coding!
Let me know if you have any questions or if you’ve tried something similar!
Cheers,
[Your Name]