

To get Home Assistant to subscribe we need to add an integration: Anyone / anything subscribed to that topic will receive a copy of the information and can then process it accordingly. MQTT allows our sensor to publish its data to a message topic.

I want to get a reading from the sensor every five minutes, but rather than listing every 5 minute combination we use some maths to tell cron to run our task every five minutes instead. For example, a * in the day-of-week field means everyday and a * in the hour field means every hour etc. In the second line you can see some asterisks * and these mean "every value for this field". Essentially as soon as cron starts running. The first line, starting with causes our script to run following system start up. Making the sensor script run on schedule. */5 * * * * python3 /home/pi/kitchenSensors/humidityTemperatureMQTT.py # minute hour day-of-month month day-of-week commandįor this I'm going to assume you've stored your script at /home/pi/kitchenSensors/humidityTemperatureMQTT.py and we want to add these two lines to the file: python3 /home/pi/kitchenSensors/humidityTemperatureMQTT.py Importantly for us is the template line that shows us the syntax for the scheduler: # m h dom mon dow command

Depending on your Linux distribution, the crontab will have a number of comments at the top, lines starting with a #, that explain how things work - worth a read. Once connected we'll run crontab -e to edit the user's crontab - the file that tells the scheduling daemon what commands to run and how often. We'll also want to get a reading as soon as the Pi Zero boots (or reboots), something we can also do with cron.Ĭonnect to the Pi Zero using ssh and login as the pi user. The easiest solution is to use cron to run the script every 5 minutes. That's not very practical given we want to get a regular stream of data across to Home Assistant. In the last part of this series we wrote the script that queries the sensors, but it was only running on demand (when we ran python3 humidityTemperatureMQTT.py). Next we have to get this information to Home Assistant so we can do something useful - that's the topic of this post. I've already shown how to assemble the hardware and put the scripts together to publish the data from the sensors. After quite a few weeks off to spend time with my family, and studying for my CISSP I'm back with part three.
