DIY fertilizer mixer and plant watering machine https://www.xythobuz.de/giessomat.html
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

influx-graph.py 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #!/usr/bin/env python
  2. from influxdb import InfluxDBClient
  3. import matplotlib.pyplot as plt
  4. import matplotlib
  5. from datetime import datetime
  6. # config
  7. host = '10.23.42.14'
  8. port = 8086
  9. user = 'root'
  10. password = 'root'
  11. dbname = 'giessomat'
  12. # data
  13. client = InfluxDBClient(host, port, user, password, dbname)
  14. print("Querying DB " + dbname + " on " + host)
  15. result = client.query('SELECT "id", "duration" FROM "plant";')
  16. #print("Result: {0}".format(result))
  17. data = list(result.get_points())
  18. print("Got " + str(len(data)) + " datapoints")
  19. #print(data)
  20. ids = list(set([ d['id'] for d in data ]))
  21. ids.sort()
  22. #ids = ['1']
  23. print("IDs found: " + str(ids))
  24. values = []
  25. times = []
  26. durations = []
  27. for id in ids:
  28. values.append([d for d in data if d['id'] == id])
  29. times.append([datetime.strptime(d['time'], '%Y-%m-%dT%H:%M:%S.%fZ') for d in data if d['id'] == id])
  30. durations.append([d['duration'] for d in data if d['id'] == id])
  31. s1 = 0.0
  32. s2 = 0.0
  33. c2 = 0
  34. for i in range(len(ids)):
  35. s = 0.0
  36. for j in range(len(durations[i])):
  37. s += durations[i][j]
  38. s2 += durations[i][j]
  39. c2 += 1
  40. avg = s / len(durations[i])
  41. print("ID=" + ids[i] + " sum=" + str(s) + " len=" + str(len(durations[i])) + " avg=" + str(avg))
  42. s1 += avg
  43. avg1 = s1 / len(ids)
  44. avg2 = s2 / c2
  45. print("avg1=" + str(avg1) + " avg2=" + str(avg2))
  46. # plot results
  47. plt.ioff()
  48. # ---------------------------
  49. fig, ax = plt.subplots()
  50. for i in range(len(ids)):
  51. dates = matplotlib.dates.date2num(times[i])
  52. ax.plot_date(dates, durations[i], '-', label='id ' + ids[i])
  53. ax.set_xlabel('Time')
  54. ax.set_ylabel('Duration')
  55. ax.set_title('Watering Durations')
  56. ax.legend()
  57. # ---------------------------
  58. fig, ax = plt.subplots()
  59. for i in range(len(ids)):
  60. values = []
  61. for j in range(len(times[i])):
  62. if j == 0:
  63. continue
  64. delta = times[i][j] - times[i][j - 1]
  65. values.append(delta.days)
  66. ax.plot(range(len(values)), values, '-', label='id ' + ids[i])
  67. ax.set_xlabel('Watering No.')
  68. ax.set_ylabel('Time Difference')
  69. ax.set_title('Time between Waterings')
  70. ax.legend()
  71. # ---------------------------
  72. fig, ax = plt.subplots()
  73. for i in range(len(ids)):
  74. ax.plot(range(len(durations[i])), durations[i], '-', label='id ' + ids[i])
  75. ax.set_xlabel('Watering No.')
  76. ax.set_ylabel('Duration')
  77. ax.set_title('Duration per Watering')
  78. ax.legend()
  79. # ---------------------------
  80. fig, ax = plt.subplots()
  81. for i in range(len(ids)):
  82. values = []
  83. s = 0
  84. for j in range(len(times[i]) - 1):
  85. t_delta = times[i][j + 1] - times[i][j]
  86. dur = (durations[i][j] + durations[i][j + 1]) / 2.0
  87. #dur = durations[i][j + 1]
  88. #dur = durations[i][j]
  89. avg_per_sec = dur / t_delta.total_seconds()
  90. #if i == 2:
  91. # print()
  92. # print(dur)
  93. # print(t_delta.total_seconds())
  94. # print(avg_per_sec)
  95. avg_per_day = avg_per_sec * 60.0 * 60.0 * 24.0
  96. values.append(avg_per_day)
  97. s += avg_per_sec
  98. #print(s / (len(times[i]) - 1))
  99. ax.plot(range(len(values)), values, '-', label='id ' + ids[i])
  100. ax.set_xlabel('Watering No.')
  101. ax.set_ylabel('Duration per Day')
  102. ax.set_title('Watering Duration per Day')
  103. ax.legend()
  104. # ---------------------------
  105. plt.show()