daily pastebin goal
6%
SHARE
TWEET

DIYFuturism.com - Tracking House Plants in Home Assistant

diyfuturism Dec 18th, 2017 (edited) 98 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #
  2. # DIYFUTURISM.COM
  3. # PLANT_PROBLEMS.PY
  4. # Make a list of all plants that need to be water and fertilized and combine into readable lists.
  5. #
  6. # Creates:
  7. # sensor.water_plants_number
  8. # sensor.fertilize_plants_number
  9. # sensor.plant_problems
  10. #
  11. #  http://www.diyfuturism.com/making-houseplants-talk
  12.  
  13. problemPlants = 0
  14. allproblemPlants = []
  15. waterPlants = []
  16. numberWater = 0
  17. fertilizePlants = []
  18. numberFertilize = 0
  19. deadBatteries = []
  20. numberdeadBatteries = 0
  21. whichIcon = "mdi:help-circle-outline"
  22.  
  23. for entity_id in hass.states.entity_ids('plant'):
  24.     state = hass.states.get(entity_id)
  25.     if state.state == 'problem':
  26.         problemPlants = problemPlants + 1
  27.         allproblemPlants.append(state.name)
  28.         problem = state.attributes.get('problem') or 'none'
  29.         if "conductivity low" in problem:
  30.           fertilizePlants.append(state.name)
  31.           numberFertilize = numberFertilize + 1
  32.         if "moisture low" in problem:
  33.           waterPlants.append(state.name)
  34.           numberWater = numberWater + 1
  35.         if "battery low" in problem:
  36.           deadBatteries.append(state.name)
  37.           numberdeadBatteries = numberdeadBatteries + 1
  38.  
  39. # Set icon
  40. if problemPlants > 0:
  41.   whichIcon = "mdi:alert-circle-outline"
  42. else:
  43.   whichIcon = "mdi:check-circle-outline"
  44.  
  45. # Set states
  46. hass.states.set('sensor.plant_problems', problemPlants, {
  47.     'unit_of_measurement': 'plants',
  48.     'friendly_name': 'Problem Plants',
  49.     'icon': whichIcon,
  50.     'problem_plants': allproblemPlants,
  51.     'water': waterPlants,
  52.     'water_number': numberWater,
  53.     'fertilize': fertilizePlants,
  54.     'fertilize_number': numberFertilize,
  55.     'battery_change': deadBatteries,
  56.     'battery_number': numberdeadBatteries
  57. })
  58.  
  59. hass.states.set('sensor.water_plants_number', numberWater, {
  60.     'unit_of_measurement': 'plants',
  61.     'friendly_name': 'Water Plants Number',
  62.     'icon': 'mdi:water'
  63. })
  64.  
  65. waterplantsList = ', '.join(waterPlants)
  66. if waterplantsList == "":
  67.   waterplantsList = "None"
  68. hass.states.set('sensor.water_plants_friendly', waterplantsList, {
  69.     'friendly_name': 'Water Plants',
  70.     'icon': 'mdi:water'
  71. })
  72.  
  73. hass.states.set('sensor.fertilize_plants_number', numberFertilize, {
  74.     'unit_of_measurement': 'plants',
  75.     'friendly_name': 'Fertilize Plants Number',
  76.     'icon': 'mdi:emoticon-poop'
  77. })
  78.  
  79. fertilizeplantsList = ', '.join(fertilizePlants)
  80. if waterplantsList == "":
  81.   waterplantsList = "None"
  82. hass.states.set('sensor.fertilize_plants_friendly', fertilizeplantsList, {
  83.     'unit_of_measurement': 'plants',
  84.     'friendly_name': 'Fertilize Plants',
  85.     'icon': 'mdi:emoticon-poop'
  86. })
RAW Paste Data
Top