Home Assistant: Making My Plants Talk with IoT Sensors and a Python Script

ConfigHardwareHome AssistantScriptsSmart HomeUseful Sensor

Written by:

Living in an urban city without a garden, I have been exercising my green thumb by accumulating more and more houseplants. I have them tucked away in every light-filled corner, hanging from every rafter in my tall ceilings. Which is a problem, because I have to get a ladder out to water most of them!

Using some cheap plant soil sensors and a simple Python script, I will have Home Assistant check all of my plants and make a list of which need my attention. Then when more than a few need to be watered, I can be notified or have the voice assistant give me an update.


HARDWARE

You could use the Xiaomi plant sensors I’ve linked here, they occasionally go on sale on aliexpress.com. A cheaper option would be to build them yourself (no soldering required) with an esp8266 board. Here’s a shopping list, you don’t need all the sensors but these are the sensors supported by the plant platform.

Building this is beyond this post, but it is fairly simple. Flash the Wemos D1 Mini with ESP Easy firmware, hook up the sensors, and configure them in the ESP Easy web menu to publish to MQTT. Home Assistant can then create sensors via the MQTT Sensor platform.

(Also if anyone knows of a DIY conductivity sensor, please comment, as I’m not aware of one).

PLANTS IN HASS

The plant platform allows us to combine these sensors into one plant entity & set thresholds for alerts for that plant. Here is an example YAML config

plants.yaml

  giant_pothos:
    sensors:
      moisture: sensor.giant_pothos_moisture
      battery: sensor.giant_pothos_battery
      temperature: sensor.giant_pothos_temperature
      conductivity: sensor.giant_pothos_conductivity
      brightness: sensor.giant_pothos_light_intensity
    min_moisture: 10
    max_moisture: 100
    min_battery: 15
    min_conductivity: 200
    min_temperature: 45

And here’s the resulting plant entity in the Home Assistant UI

Plant Entity

A plant entity in Home Assistant UI

This is wonderful, but I bought a dozen of these Xiaomi sensors when they were on sale. Wouldn’t it be great if, once I’m up on the ladder with a watering can, I could yell out to Alexa to list off which ones need it?

RELATED >>  My Smart Home: Hardware List


PYTHON SCRIPT

This script uses the python script platform in Home Assistant. I’m going to trigger it anytime my plants update their status, like so:

automations.yaml

- alias: 'Update Plant Problems'
  trigger:
    - platform: state
      entity_id: plant.giant_philodendron, plant.giant_pothos, plant.hanging_red_pothos, plant.hanging_spider_plant, plant.kitchen_pothos, plant.palm, plant.projector_pothos, plant.rubber_plant, plant.small_philodendron, plant.window_spider_plant
  action:
    - service: python_script.plant_problems

And now for the script itself. It’s going to create 3 new entities:

  • sensor.water_plants_number
  • sensor.fertilize_plants_number
  • sensor.problem_plants

The last one will contain a number of attributes that will be useful for automations, including lists in human readable form.

python_scripts/plant_problems.py

Pastebin link: https://pastebin.com/yCbbJ6uA

And here you have it:

The new sensor.problem_plants in the UI

The new sensor.problem_plants in the UI has a number of useful attributes.

TALK TO ME, PLANTS

This is a Home Assistant script, you will need to have a text-to-speech platform configured.

scripts.yaml

tts_plant_info:
  alias: Announce Plant Info
  sequence:
    - service: tts.amazon_polly_say
      data_template:
        entity_id: media_player.vlc_tts
        message: >
          {% if is_state_attr('sensor.plant_problems','water_number',0) %}
          No plants need watering.
          {% elif states.sensor.plant_problems.attributes.water_number | float > 0 %}
          {{states.sensor.plant_problems.attributes.water}} need to be watered.
          {% endif %}
          {% if is_state_attr('sensor.plant_problems','fertilize_number',0) %}
          No plants need fertilizer.
          {% elif states.sensor.plant_problems.attributes.fertilize_number | float > 0 %}
          These plants {{states.sensor.plant_problems.attributes.fertilize}} need fertilizing.
          {% endif %}

And now your plants talk to you. Welcome to the future. I have added some additional functionality to my set up with Node-Red: Mine also tweet to the world when I’ve ignored them too long. Follow at @diyfuturism.


5 Replies to “Home Assistant: Making My Plants Talk with IoT Sensors and a Python Script”

  1. JFo says:

    Brad, thanks. Your setup is inspiring!

    For me, I’m adding things here and there. I’m currently having trouble with my plant sensor in Hass.io. Works from the android app, but not visible to HA. I’m running .60 which broke a bunch of stuff so I am hoping that it comes to life once some of the .60 kinks are worked out. If not, then I think I might have a problem with BT on my PI.

    Thanks again for the blog…YOU ARE AMAZING!

  2. Andrew says:

    Love it! I just sumbled upon this. I am already using it and its working like a charm!

  3. Adam says:

    This is great! Thanks for sharing. Do you have a pointer to the node red flow to show how you’ve improved it?

  4. Ayamy says:

    great….
    i’m trying to use your script but running it from the terminal (in hassio) it shows:

    ➜ python_scripts python3 plant_problems.py
    Traceback (most recent call last):
    File “plant_problems.py”, line 23, in
    for entity_id in hass.states.entity_ids(‘plant’):
    NameError: name ‘hass’ is not defined

    • ayamy says:

      i have seen that it’s a common mistake running Py script “outside” hassio…my fault… but I’m trying to check if the script has some problems with my configuration because it run when the state of a plant changes… but i can’t find in the entity page the 3 new entities called :

      sensor.water_plants_number
      sensor.fertilize_plants_number
      sensor.problem_plants

Leave a Reply

Your email address will not be published. Required fields are marked *