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 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. cumulative = []
  52. for j in range(len(durations[i])):
  53. if j == 0:
  54. cumulative.append(durations[i][j])
  55. else:
  56. cumulative.append(cumulative[j - 1] + durations[i][j])
  57. dates = matplotlib.dates.date2num(times[i])
  58. ax.plot_date(dates, cumulative, '-', label='id ' + ids[i])
  59. ax.set_xlabel('Time')
  60. ax.set_ylabel('Duration')
  61. ax.set_title('Cumulative Watering Durations')
  62. ax.legend()
  63. # ---------------------------
  64. smoothing_value = 3.0
  65. fig, ax = plt.subplots()
  66. for i in range(len(ids)):
  67. cumulative = []
  68. for j in range(len(durations[i])):
  69. if j == 0:
  70. cumulative.append(durations[i][j])
  71. else:
  72. cumulative.append(cumulative[j - 1] + durations[i][j])
  73. cumulative_clean = []
  74. time_clean = []
  75. xr = iter(range(len(durations[i]) - 1))
  76. try:
  77. for j in xr:
  78. dx_dt = (times[i][j + 1] - times[i][j])
  79. dx = dx_dt.seconds / 24 / 60 / 60 + dx_dt.days
  80. if dx <= smoothing_value:
  81. #print("combining diff=" + str(dx))
  82. time_clean.append(times[i][j + 1])
  83. cumulative_clean.append(cumulative[j + 1])
  84. next(xr)
  85. else:
  86. time_clean.append(times[i][j])
  87. cumulative_clean.append(cumulative[j])
  88. except:
  89. pass
  90. cumulative_clean_2 = []
  91. time_clean_2 = []
  92. xr = iter(range(len(cumulative_clean) - 1))
  93. try:
  94. for j in xr:
  95. dx_dt = (time_clean[j + 1] - time_clean[j])
  96. dx = dx_dt.seconds / 24 / 60 / 60 + dx_dt.days
  97. if dx <= smoothing_value:
  98. #print("combining diff=" + str(dx))
  99. time_clean_2.append(time_clean[j + 1])
  100. cumulative_clean_2.append(cumulative_clean[j + 1])
  101. next(xr)
  102. else:
  103. time_clean_2.append(time_clean[j])
  104. cumulative_clean_2.append(cumulative_clean[j])
  105. except:
  106. pass
  107. dates = matplotlib.dates.date2num(time_clean_2)
  108. ax.plot_date(dates, cumulative_clean_2, '-', label='id ' + ids[i])
  109. ax.set_xlabel('Time')
  110. ax.set_ylabel('Duration')
  111. ax.set_title('Smoothed Cumulative Watering Durations')
  112. ax.legend()
  113. # ---------------------------
  114. fig, ax = plt.subplots()
  115. for i in range(len(ids)):
  116. cumulative = []
  117. for j in range(len(durations[i])):
  118. if j == 0:
  119. cumulative.append(durations[i][j])
  120. else:
  121. cumulative.append(cumulative[j - 1] + durations[i][j])
  122. cumulative_clean = []
  123. time_clean = []
  124. xr = iter(range(len(durations[i]) - 1))
  125. try:
  126. for j in xr:
  127. dx_dt = (times[i][j + 1] - times[i][j])
  128. dx = dx_dt.seconds / 24 / 60 / 60 + dx_dt.days
  129. if dx <= smoothing_value:
  130. #print("combining diff=" + str(dx))
  131. time_clean.append(times[i][j + 1])
  132. cumulative_clean.append(cumulative[j + 1])
  133. next(xr)
  134. else:
  135. time_clean.append(times[i][j])
  136. cumulative_clean.append(cumulative[j])
  137. except:
  138. pass
  139. cumulative_clean_2 = []
  140. time_clean_2 = []
  141. xr = iter(range(len(cumulative_clean) - 1))
  142. try:
  143. for j in xr:
  144. dx_dt = (time_clean[j + 1] - time_clean[j])
  145. dx = dx_dt.seconds / 24 / 60 / 60 + dx_dt.days
  146. if dx <= smoothing_value:
  147. #print("combining diff=" + str(dx))
  148. time_clean_2.append(time_clean[j + 1])
  149. cumulative_clean_2.append(cumulative_clean[j + 1])
  150. next(xr)
  151. else:
  152. time_clean_2.append(time_clean[j])
  153. cumulative_clean_2.append(cumulative_clean[j])
  154. except:
  155. pass
  156. rate_of_change = []
  157. for j in range(len(cumulative_clean_2)):
  158. if j < len(cumulative_clean_2) - 1:
  159. dy = cumulative_clean_2[j + 1] - cumulative_clean_2[j]
  160. dx_dt = (time_clean_2[j + 1] - time_clean_2[j])
  161. dx = dx_dt.seconds / 24 / 60 / 60 + dx_dt.days
  162. roc = dy / dx
  163. #print(str(time_clean_2[j]) + " " + str(dy) + " / " + str(dx) + " = " + str(roc))
  164. rate_of_change.append(roc)
  165. dates = matplotlib.dates.date2num(time_clean_2)
  166. ax.plot_date(dates[:-1], rate_of_change, '-', label='id ' + ids[i])
  167. ax.set_xlabel('Time')
  168. ax.set_ylabel('Rate of Change')
  169. ax.set_title('RoC of Cumulative Watering Durations')
  170. ax.legend()
  171. # ---------------------------
  172. plt.show()