Update PWS REST API via Node-RED

Hi,

I am currently updating a PWS (personal weather station) using home assistant, using the below REST command which is triggered every 5 minutes by an automation.

url: 'https://weatherstation.wunderground.com/weatherstation/updateweatherstation.php?ID=LOCATION&PASSWORD=PASSWORD&dateutc=now&tempf={{ states.sensor.external_temperature_f.state }}&baromin={{ states.sensor.external_air_pressure_inhg.state }}&humidity={{ states.sensor.external_humidity.state }}&dewptf={{ states.sensor.external_dew_point_f.state }}&winddir={{ states.sensor.external_wind_direction.state }}&windspeedmph={{ states.sensor.external_wind_speed.state }}&action=updateraw'

I want to move this to Node-RED but unsure where to start.

Any ideas?

Thanks.

Assuming that you have HA with all the relevant sensors, and the REST update is working, and assuming that you currently do not have Node-RED running…

The steps you need to take are probably as follows:

You need Node-RED running (as an addon in HA). See Settings > Addons > Addon store. Find Node-RED (community section). Follow the installation instructions in the documentation. Not difficult, just make sure to add the ‘credential-secret’ (and keep a record).

You need access to the Node-RED editor in HA. Easiest way is to use the ‘open web ui’ option in the Node-RED addon setup page. Also, make sure ‘show in sidebar’ is on, and an option for Node-RED (editor) will be in your HA sidebar.

Node-RED addon for HA comes with all the essentials preconfigured, so you should be ready to go. Node-RED should have the home assistant node (web socket nodes) at the top left in the node selection bar.

Then all you need is aa simple flow -

Here is my test example flow. There are many ways to acheive this, but just to get started, all we really need is the http request node (does all the work) and a few nodes to set up the parameters.

http request node is set to ‘GET’, and has the base url (without the parameters). If I read your notes correctly this is https://weatherstation.wunderground.com/weatherstation/updateweatherstation.php

the node is set up to use the msg.payload (which moves from node to node in the flow) and will add this as the parameters. We need a JSON object here, and for each key/value, the request node will add ‘?’ to the url to start the request, and then ‘key=object&’.

You can see that I have an inject node, which is just to manually trigger the flow, but can be changed to fire every 5 minutes. So after testing this flow can be set to repeat every five minutes.

Then I have a change node, in which I first set up the basic JSON object for the parameters.

This needs the first (and only) rule to be ‘set’ and ‘msg.payload’ to the value… Then we set the option list to {} JSON, and enter a JSON object as follows.

JSON objects can only contain strings and numbers, not variables, but this sets up the object and we can then use other nodes to replace the dummy values with real ones. Also good for testing - I cannot test this as I don’t have your password, but at this stage you can just fire it off and see if it works.

To test, we use the debug node, which can be used anywhere in the flow, and by default shows the msg.payload in the right hand debug window. Here you can see that I have tested the msg.payload value just before it goes to the http request node.

Then all I need is a string of HA current state nodes to individually pick up the current states of each sensor, placing the values into the object created earlier. Node-RED running in HA sets up a web socket between the two, and Node-RED has a complete copy of all HA entity states (and more), so the current state node can pick up state and other entity details.

Here are the settings I have used.
I am running this test on my separate Raspberry Pi, so the ‘server’ setting you would see is Home Assistant.
Then I add in the sensor - here I have used a test, you just have to type in (and the node will autocomplete as you type) for whatever you want.
State type should probably be string or number as required.

The key bit is the bottom output properties. Normally this node pushes the entity state to msg.payload (and entity detail to msg.data) but I have changed this slightly. I am writing to msg.payload.parameter, or in this example msg.payload.tempf which will place the state value from the sensor directly into the JSON object.
This is one reason I use an object here - each current state node normally writes to msg.payload, so in sequence like this each node overwrites the previous value. However, changing the output to not write to msg.payload but to msg.payload.key I keep the entire object, adding each each value as I go.

Repeat for as many sensor values as you required. Test with the debug node to check the msg.payload is correct, then test the full flow.

The request node waits for a reply, so as a minimum it would be good to see what comes back (success or error). Long term this flow needs more work, for example what to do if there is an error, how to manage sensor values that are unavailable, how to turn off the flow if not required, and so on, but this should get you started.

I hope that answers your original question!

I have not tested this against weatherstation url, but it does all work for me in my test environment.

2 Likes

Just wanted to say this is a fantastic write up and one of the most comprehensive i have seen for someone to get going into uploading their weather data

Give yourself a pat on the back - great work

Craig

This is an absolutely incredible response and so helpful.

I’ve been using NR for years, and slowly moving all automatons from HA’s engine.

The above explanation worked perfectly and I now have the PWS reporting to several cloud services.

Thanks, very much appreciated!

R.