Categories
Connected Farm

farmOS sensors with Home Assistant and Node-Red.

farmOS provides an API to import sensor data and since I’m already building a farm wide sensor network this looks like a feature I’d like to implement.

The farmOS guide https://farmos.org/guide/assets/sensors/ says: “The Farm Sensor Listener module is a general-purpose sensor sub-module that provides a simple “Listener” sensor type. Each sensor asset that is denoted as a listener receives a unique URL with a public and private key that data can be pushed to using standard HTTP requests with JSON-encoded data. Data is stored in the database and is displayed in graphs and in table format in the sensor asset within farmOS.”

That is actually simpler than it sounds as the required HTTP URL is generated for you when you create a sensor in farmOS, and the JSON format is simply formatted like{ “value”: 76.5 }.

In farmOS navigate to farm/assets/sensors and click add sensor.
Give it a name.  
Select "Listener" under sensor configuration\sensor type. 
Open developer Information and copy the URL to notepad or similar for later use with Node-Red.  
Make sure to click save.

Since Home Assistant is already managing sensors on the farm via WiFi, LoRa and 4G connections it can be used to post sensor data to farmOS. Node-Red provides a relatively simple means of listening for Home Assistant sensors, reformatting the data and posting it to external apps such as farmOS and is available in the Home Assistant Addon Store.

The addon comes preconfigured with Home Assistant nodes, only a credential secret needs to be set in the configuration tab. A simple password of your chosing.

Node-Red can then be launched from the info screen by clicking on Open Web UI, it can also be added to the side bar if desired.

Only 3 nodes are required to pass the sensor data from Home Assistant to farmOS. The Poll State Node can read sensor data at a defined interval, the Function Node reformats the numeric value to the required JSON and the WWW Request Node posts the formatted data to the farmOS URL. An optional Debug Node can be used to confirm the data has been formatted correctly.

Double click the Poll State Node, enter a name, leave the server as Home Assistant and enter the required sensor in entity. After typing sensor. in the entity box a dropdown list of the sensors configured in Home Assistant will display.

Next double click the function node and paste the following code:

var level = msg.payload;
msg.payload = {percentage: level}
return msg;Code language: JavaScript (javascript)

Finally double click the WWW Request Node, method should be Post and the URL can be copied directly from your Sensor in farmOS or the text pad it was saved to earlier.

That is it, click Deploy and farmOS should start receiving sensor data from Home Assistant.

Facebook Comments