Thanks to the simple examples here, I made a python script to monitor my house plants. I got many of these sensors on sale but how to make it more useful.
I wanted a human readable list of the friendly names to use in TTS notifications, and also to get better notifications (only get out the watering can if 3+ plants need to be watered).
problemPlants = 0
allproblemPlants = []
waterPlants = []
numberWater = 0
fertilizePlants = []
numberFertilize = 0
whichIcon = "mdi:help-circle-outline"
for entity_id in hass.states.entity_ids('plant'):
state = hass.states.get(entity_id)
if state.state == 'problem':
problemPlants = problemPlants + 1
allproblemPlants.append(state.name)
problem = state.attributes.get('problem') or 'none'
if "conductivity low" in problem:
fertilizePlants.append(state.name)
numberFertilize = numberFertilize + 1
if "moisture low" in problem:
waterPlants.append(state.name)
numberWater = numberWater + 1
# Set icon
if problemPlants > 0:
whichIcon = "mdi:alert-circle-outline"
else:
whichIcon = "mdi:check-circle-outline"
# Set states
hass.states.set('sensor.water_plants', problemPlants, {
'unit_of_measurement': 'plants',
'friendly_name': 'Problem Plants',
'icon': whichIcon,
'problem_plants': allproblemPlants,
'water': waterPlants,
'water_number': numberWater,
'fertilize': fertilizePlants,
'fertilize_number': numberFertilize
})
My default settings are error, but would like to set it to warning or info for python_script. I can always use logger.error() during debugging, but would like to know the right approach.
Apologies for the noob questions, but I am new to python and scripts…
How would you expose the result of your python script in the front end? Would you need to create a input boolean button and then call the script with from the input boolean?
I have a plants group which is visible (25 plants - no I am not a weed farmer) and I would like to show this list in the group as default.
You can use the script to create a sensor that you can then display in the frontend. For example, in this case, a new sensor sensor.plant_problems is created that can be used elsewhere.
I have the script available to call in the services tab in developer tools. The automation is also showing up. IF I have my script woking correctly, should a new sensor appear or do I have to create a new sensor from the script?
I have been using these scripts for the last few months and haven’t killed a house plant since . I have written up the script and configuration info here:
I have extended this script. It now also lists plant sensors with empty batteries.
looks like this:
Code:
# Creates the following sensors:
#
# sensor.plants_water_number -> Amount of plants that need to be watered
# sensor.plants_water_friendly -> Name(s) of the plants that need water
# sensor.plants_battery_number -> Amount of plantsensors running out of battery
# sensor.plants_battery_friendly -> Name(s) of platsensors running out of battery
# sensor.plants_problems -> Amount of plants which either need water or have low battery
#
# INSPIRED BY THIS GREAT BLOGPOST
# http://www.diyfuturism.com/making-houseplants-talk
problemPlants = 0
allproblemPlants = []
waterPlants = []
numberWater = 0
deadBatteries = []
numberdeadBatteries = 0
whichIcon = "mdi:help-circle-outline"
for entity_id in hass.states.entity_ids('plant'):
state = hass.states.get(entity_id)
if state.state == 'problem':
problemPlants = problemPlants + 1
allproblemPlants.append(state.name)
problem = state.attributes.get('problem') or 'none'
if "moisture low" in problem:
waterPlants.append(state.name)
numberWater = numberWater + 1
if "battery low" in problem:
deadBatteries.append(state.name)
numberdeadBatteries = numberdeadBatteries + 1
# Set icon
if problemPlants > 0:
whichIcon = "mdi:alert-circle-outline"
else:
whichIcon = "mdi:check-circle-outline"
# Set states
hass.states.set('sensor.plants_problems', problemPlants, {
'unit_of_measurement': 'Pflanzen',
'friendly_name': 'Problempflanzen',
'icon': whichIcon,
'problem_plants': allproblemPlants,
'water': waterPlants,
'water_number': numberWater,
'battery_change': deadBatteries,
'battery_number': numberdeadBatteries
})
hass.states.set('sensor.plants_water_number', numberWater, {
'unit_of_measurement': 'Pflanzen',
'friendly_name': 'Anz. durstiger Pflanzen',
'icon': 'mdi:water'
})
hass.states.set('sensor.plants_battery_number', numberdeadBatteries, {
'unit_of_measurement': 'Sensoren',
'friendly_name': 'Anz. akkuschwacher Sensoren',
'icon': 'mdi:battery-30'
})
waterplantsList = ', '.join(waterPlants)
if waterplantsList == "":
waterplantsList = "Keine"
hass.states.set('sensor.plants_water_friendly', waterplantsList, {
'friendly_name': 'Durstige Pflanzen',
'icon': 'mdi:water'
})
batteryplantsList = ', '.join(deadBatteries)
if batteryplantsList == "":
batteryplantsList = "Keine"
hass.states.set('sensor.plants_battery_friendly', batteryplantsList, {
'friendly_name': 'Akkuschwache Sensoren',
'icon': 'mdi:battery-30'
})
I also have an Amazon Dashbutton running this script when pressed. It’s basically a spoken plantreport using Alexa as TTS. Like the code above it’s in german and probably doesn’t need so many elif-statements. German grammar is quite specific when it comes to singular and plural expressions Have fun:
alexa_plantreport:
alias: Alexa Pflanzenreport
sequence:
- service: notify.alexa
data_template:
message: >
{% if states.sensor.plants_water_number.state | int == 0 and states.sensor.plants_battery_number.state | int == 0 %}
Allen Pflanzen geht es hervorragend.
{% elif states.sensor.plants_water_number.state | int == 1 and states.sensor.plants_battery_number.state | int == 0 %}
Eine Pflanze braucht Wasser und zwar. {{ states.sensor.plants_water_friendly.state }}
{% elif states.sensor.plants_water_number.state | int == 0 and states.sensor.plants_battery_number.state | int == 1 %}
Ein Pflanzensensor benötigt eine neue Batterie und zwar. {{ states.sensor.plants_battery_friendly.state }}
{% elif states.sensor.plants_water_number.state | int == 1 and states.sensor.plants_battery_number.state | int == 1 %}
Eine Pflanze braucht Wasser und zwar {{ states.sensor.plants_water_friendly.state }} und ein Pflanzensensor benötigt eine neue Batterie und zwar {{ states.sensor.plants_battery_friendly.state }}.
{% elif states.sensor.plants_water_number.state | int > 1 and states.sensor.plants_battery_number.state | int == 0 %}
{{ states.sensor.plants_water_number.state }} Pflanzen brauchen Wasser und zwar {{ states.sensor.plants_water_friendly.state }}.
{% elif states.sensor.plants_water_number.state | int == 0 and states.sensor.plants_battery_number.state | int > 1 %}
{{ states.sensor.plants_battery_number.state }} Pflanzensensoren benötigen eine neue Batterie und zwar {{ states.sensor.plants_battery_friendly.state }}.
{% elif states.sensor.plants_water_number.state | int > 1 and states.sensor.plants_battery_number.state | int == 1 %}
{{ states.sensor.plants_water_number.state }} Pflanzen brauchen Wasser und zwar {{ states.sensor.plants_water_friendly.state }} und ein Pflanzensensor benötigt eine neue Batterie und zwar {{ states.sensor.plants_battery_friendly.state }}.
{% elif states.sensor.plants_water_number.state | int == 1 and states.sensor.plants_battery_number.state | int > 1 %}
Eine Pflanze braucht Wasser und zwar {{ states.sensor.plants_water_friendly.state }} und {{ states.sensor.plants_battery_number.state }} Pflanzensensoren benötigen eine neue Batterie und zwar {{ states.sensor.plants_battery_friendly.state }}.
{% else %}
{{ states.sensor.plants_water_number.state }} Pflanzen brauchen Wasser und zwar. {{ states.sensor.plants_water_friendly.state }} und {{ states.sensor.plants_battery_number.state }} Pflanzensensoren benötigen eine neue Batterie und zwar {{ states.sensor.plants_battery_friendly.state }}
{% endif %}
Good Question! The python script needs to be triggered to update/create the sensors. I have an automation that observes the state of all my plants and whenever there is change of a plant it runs the script and updates the sensors. you can also run the python script from the dev tools in the hass ui. But here is my automation: