Automating actions based on sensor data?

I’m thinking of setting up with FarmOS, but first am interested to know how easy it is to use sensor data to automate actions. I understand it is possible to send an email when sensor data are outside of a specific range, but can other actions be triggered in response to sensor data? For example, is it possible to:

a) add a task to a todo list when a sensor reaches a certain range/value?
b) execute a program (e.g. in Python) when a sensor reaches a certain range? For example, I could send a command to turn on an irrigation system in a polytunnel when the temperature gets too high.

3 Likes

Hi @CaptainProg - welcome to the farmOS forum!

Yes! There are a few ways to achieve this (all require programming however)…

can other actions be triggered in response to sensor data

In farmOS 1.x, there is a hook_farm_sensor_listener_data() (which is a Drupal hook) that you can implement in a custom module to perform logic whenever data is received/processed by a given sensor.

However, we do not have a comparable hook in the farmOS 2.x branch (yet). It may make more sense to take an approach outside of farmOS to keep things more separated, which brings me to the next approach…

execute a program (e.g. in Python) when a sensor reaches a certain range?

There is an API for the sensors (in both 1.x and 2.x). You could write a Python script that runs on a different computer (not on the farmOS server) automatically on a schedule, reads the latest data point(s) from the sensor API, and performs whatever logic you want.

add a task to a todo list when a sensor reaches a certain range/value?

This can be done via the API, or in PHP via the hook described above.

I could send a command to turn on an irrigation system in a polytunnel when the temperature gets too high.

This would make the most sense to implement in a Python script that reads from the sensor API (or directly from the sensor itself!), in my opinion.

Another caution I would add here: if your farmOS server is on a different computer (eg: in the cloud) than your Python script, be sure to write your Python script in a way that it will work even if the network connection to farmOS becomes unavailable (eg: if your internet goes down).

Always best to have a resilient local-first design when dealing with mission-critical sensors and actuators! You wouldn’t want your irrigation to fail if the internet goes out!

So: I always recommend that most of this logic reside locally, and use farmOS primarily as a data store. It can certainly be used to trigger things as well, but depending on that could add fragility that you don’t want.

2 Likes