Convert Wind direction in degrees to Cardinal points

This change node will convert a payload in decimal degrees to Cardinal points N, NNE, NE.......

It might be useful to others using online weather services to display wind directions in human readable form.

[{"id":"ab01324c.4824","type":"inject","z":"1d66267b.4c5caa","name":"","topic":"","payload":"270","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":100,"wires":[["3280794c.eb6396"]]},{"id":"3280794c.eb6396","type":"change","z":"1d66267b.4c5caa","name":"Convert Wind Degrees To Text","rules":[{"t":"set","p":"directions","pt":"flow","to":"$exists($flowContext(\"directions\")) ?  $flowContext(\"directions\") : [\"N\", \"NNE\", \"NE\", \"ENE\", \"E\", \"ESE\", \"SE\", \"SSE\", \"S\", \"SSW\", \"SW\", \"WSW\", \"W\", \"WNW\", \"NW\", \"NNW\"] ","tot":"jsonata"},{"t":"set","p":"index","pt":"msg","to":"$string($floor((payload / 22.5) + 0.5) % 16)","tot":"jsonata"},{"t":"set","p":"payload","pt":"msg","to":"$flowContext(\"directions[\"&index&\"]\")","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":410,"y":120,"wires":[["10a54176.da8daf"]]},{"id":"10a54176.da8daf","type":"debug","z":"1d66267b.4c5caa","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":690,"y":120,"wires":[]}]

EDIT: Wind direction is passed on as payload as suggested by @Colin

5 Likes

A very welcome addition Garry which would make a useful contrib-node, or at least an addition to the node-RED library as a flow.

Nice use of jsonata.

+1 for what @Paul-Reed said!

A minor quibble, but would it not be more conventional to pass the value on in msg.payload?

You are right @Colin , edited the code to do just that.

Thank you @Paul-Reed. Writing a contrib-node is beyond my skill level at the moment. I will however post it to the flow library.

If it were to be a node then adding translations would be useful
(so yes maybe leave it as a useful function for now)