Easy peasy – querying weather API in Node-RED and publishing the current temperature to a MQTT broker

Precondition: Have your lokal MQTT broker and Node-RED installed and setup or access to a remote one.

I recently added a http request node querying a weather API to Node-RED. After extracting, the current temperature is then published on the local MQTT and can be used by all devices connected to the broker.
In this example the used API can be found and the request configured here: https://open-meteo.com/en/docs. We will be using the geo coordinates of Berlin and just the current weather as parameters for the API: https://api.open-meteo.com/v1/forecast?latitude=52.52&longitude=13.41&current_weather=true. As usual the response is a JSON object containing the requested values.

For our use-case Node-RED has the required tools already built in:

  • the http request node to query the API,
  • the change node to extract the current temperature and build a new message,
  • the filter node to avoid updates with no changes
  • the mqtt out node to publish your new message and
  • the inject node to trigger the flow in the desired interval
The final flow
inject node

In the inject node you only set the interval. As the temperature values normally don’t change that often it is absolutely sufficient and fair for a free service to only update all 5 to 15 minutes.

http request node

The request node needs two changes: the URL with the values you configured on the above mentioned site and the return type needs to be set to ‘a parsed JSON object’ for easy access later in the change node.

change node: I use the topic ‘outside/temp’

In the change node you define the topic to which should be published on your MQTT broker and overwrite the message payload with the temperature in the JSON object which was the payload itself before this step.

The filter node requires no changes – default behavior is blocking new messages with the same topic when the payload (in our case the temperature) hasn’t changed.

mqtt out node: I chose to retain the published message on the broker so that you always get the last current value when connected

Last step is connecting the mqtt out node. Set your broker in there and decide if the published message should be persisted on it – if so check the option retain.

Deploy your changes in Node-RED and you are done! Congrats on probably your first flow querying an external API and integrating the data in your flows. From now on connected devices subscribed to the topic will receive updates in the set interval.

If you have problems or want to see what is happening then don’t hesitate to use the debug node. It is great – I love it! Just connect it to every node you want to see the output of and perhaps filter the debugging output by choosing the option selected nodes and only checking this one to avoid a spammed debug window.

Have fun!

Similar Posts

One Comment

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.