fhem 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import time
  4. import datetime
  5. # Example for output from agent
  6. # ---------------------------------------------------------
  7. #<<<fhem>>>
  8. #Detected devices: Bad.Temp Dachboden.Temp Gaestezimmer.Temp Partyraum.Temp Schlafzimmer.Temp
  9. #Bad.Temp Bad.Temp TYPE LaCrosse
  10. # 2016-11-16 08:39:41 Bad.Temp battery ok
  11. # 2016-11-16 08:39:41 Bad.Temp dewpoint 15.1
  12. # 2016-11-16 08:39:41 Bad.Temp humdiff 0
  13. # 2016-11-16 08:39:41 Bad.Temp humidity 83
  14. #eg.bad.waschmaschine_pwr eg.bad.waschmaschine_pwr TYPE Revolt
  15. # 2016-11-15 16:54:48 eg.bad.waschmaschine_pwr avgpower 263.19
  16. # 2016-11-16 09:18:07 eg.bad.waschmaschine_pwr current 0
  17. # 2016-11-16 09:18:07 eg.bad.waschmaschine_pwr energy 9.27
  18. # 2016-11-16 09:18:07 eg.bad.waschmaschine_pwr frequency 50
  19. # 2016-11-16 09:18:07 eg.bad.waschmaschine_pwr pf 0
  20. # 2016-11-16 09:18:07 eg.bad.waschmaschine_pwr power 0
  21. # 2016-11-16 09:18:07 eg.bad.waschmaschine_pwr state P: 0.0 E: 9.27 V: 226 C: 0.00 F: 50 Pf: 0.00
  22. # 2016-11-16 09:18:07 eg.bad.waschmaschine_pwr voltage 226
  23. #eg.wz.heizung eg.wz.heizung TYPE CUL_HM
  24. # eg.wz.heizung model HM-CC-RT-DN
  25. # 2016-11-15 09:46:13 eg.wz.heizung Activity alive
  26. # 2016-11-15 23:03:10 eg.wz.heizung CommandAccepted yes
  27. # 2016-11-07 06:01:06 eg.wz.heizung D-firmware 1.4
  28. # 2016-11-07 06:01:06 eg.wz.heizung D-serialNr MEQ0252852
  29. # 2016-11-07 06:04:23 eg.wz.heizung PairedTo 0x5FF2BE
  30. # 2016-11-07 06:04:23 eg.wz.heizung R-backOnTime 10 s
  31. # 2016-11-07 06:04:23 eg.wz.heizung R-btnLock on
  32. ## ---------------------------------------------------------
  33. def inventory_fhem(info):
  34. # Detected devices: Bad.Temp Dachboden.Temp Gaestezimmer.Temp
  35. line = info[0]
  36. if len(line) > 2:
  37. for device in line[2:]:
  38. yield device, {}
  39. def check_fhem(item, params, info):
  40. # [[u'Detected', u'fhem:', u'Bad.Temp', u'Dachboden.Temp', u'Gaestezimmer.Temp', u'Partyraum.Temp', u'Schlafzimmer.Temp'], [u'Bad.Temp', u'TYPE', u'LaCrosse'], [u'2016-07-14', u'13:15:07', u'state', u'T:', u'21.4', u'H:', u'77'], [u'2016-07-14', u'13:15:07', u'dewpoint', u'17.2'], [u'Dachboden.Temp', u'TYPE', u'LaCrosse'], [u'2016-07-14', u'13:15:10', u'state', u'T:', u'23.5', u'H:', u'53'], [u'2016-07-14', u'13:15:10', u'dewpoint', u'13.4'], [u'Gaestezimmer.Temp', u'TYPE', u'LaCrosse'], [u'2016-07-14', u'13:15:10', u'state', u'T:', u'22.8', u'H:', u'66'], [u'2016-07-14', u'13:15:10', u'dewpoint', u'16.1'], [u'Partyraum.Temp', u'TYPE', u'LaCrosse'], [u'2016-07-14', u'13:15:12', u'state', u'T:', u'23', u'H:', u'59'], [u'2016-07-14', u'13:15:12', u'dewpoint', u'14.6'], [u'Schlafzimmer.Temp', u'TYPE', u'LaCrosse'], [u'2016-07-14', u'13:15:08', u'state', u'T:', u'22.8', u'H:', u'59'], [u'2016-07-14', u'13:15:08', u'dewpoint', u'14.4']]
  41. fwarn, fcrit = None, None
  42. if "level_data_age" in params:
  43. dage_warn, dage_crit = params["level_data_age"]
  44. if "level_temp_max" in params:
  45. tmax_warn, tmax_crit = params["level_temp_max"]
  46. if "level_temp_min" in params:
  47. tmin_warn, tmin_crit = params["level_temp_min"]
  48. # See if any checks are diabled (set to 0)
  49. # if dage_warn != None and dage_warn == 0:
  50. # dage_warn = None;
  51. # if dage_crit != None and dage_crit == 0:
  52. # dage_crit = None;
  53. # if cbwarn != None and cbwarn == 0:
  54. # cbwarn = None;
  55. # if cbcrit != None and cbcrit == 0:
  56. # cbcrit = None;
  57. errorlevel, warnlevel = 0, 0
  58. dev_model = ""
  59. dev_type = ""
  60. output = []
  61. perfdata = []
  62. data = {}
  63. in_device = 0
  64. # ## bugfix, at beginning all timestamp up-to-date
  65. # ## necessary because some devices don't send all data (weather-provider has no battery)
  66. # battery_timestamp, dewpoint_timespamp, state_timestamp = time.time(), time.time(), time.time()
  67. for line in info:
  68. if len(line) == 4 and line[2] == 'TYPE':
  69. if line[0] == item:
  70. dev_type = line[3]
  71. in_device = 1
  72. elif in_device == 1:
  73. break
  74. elif in_device == 1:
  75. if line[1] == 'model':
  76. dev_model = line[2]
  77. if len(line) > 3 and line[3] != 'null':
  78. data[line[3]] = {}
  79. data[line[3]]['value']=line[4]
  80. data[line[3]]['time']=( "%s %s" % (line[0], line[1]))
  81. data[line[3]]['channel'] = line[2]
  82. ## needed for traffic
  83. if len(line) > 6:
  84. if line[5] == 'hour':
  85. # convert hours to minutes
  86. data[line[3]]['value']=(int(line[4])*60)+int(line[6])
  87. # mostly config mistake or incorrect agent output
  88. if in_device == 0:
  89. return (3, "UNKNOWN - unable to parse %s (type: %s, model: %s)" % (item, dev_type, dev_model ))
  90. # ##################################################################
  91. ## key => variable_name
  92. ## title => readable output
  93. ## unit => °C, %, V., kmh, ... (for output)
  94. ## channel => filter for channel (HomeMatic)
  95. ## show => print if no error
  96. ## perfd => print perfdata
  97. for key, title, unit, channel, show, perfd in [
  98. ## key title unit channel show perfd
  99. ## ------- ------- ------- ------- ---- -----
  100. ('temperature', 'temperature', u'°C', '', 1, 1),
  101. ('desired-temp', 'desiredtemp', u'°C', '', 1, 1),
  102. ('humidity', 'humidity', u'%', '', 1, 1),
  103. ('dewpoint', 'dewpoint', u'°C', '', 1, 1),
  104. ('contact', 'contact', '', '', 1, 1),
  105. ('valveposition', 'valveposition', u'%', '', 1, 1),
  106. ('battery', 'battery', '', '', 1, 0),
  107. ('batteryLevel', 'batteryLevel', 'V', '', 1, 1),
  108. ('controlMode', 'controlMode', '', 'Clima', 0, 0),
  109. ('Activity', 'activity', '', '', 0, 1),
  110. ('power', 'power', 'kWh', '', 1, 1),
  111. ('energy', 'energy', 'W', '', 1, 1),
  112. ('voltage', 'voltage', 'V', '', 1, 1),
  113. ## HomeMatic
  114. ('measured-temp', 'temperature', u'°C', '', 1, 1),
  115. ('actuator', 'valveposition', u'%', '', 1, 1),
  116. ('R-btnLock', 'btnLock', '', '', 0, 0),
  117. ('R-globalBtnLock', 'globalBtnLock', '', '', 0, 0),
  118. ('R-modusBtnLock', 'modusBtnLock', '', '', 0, 0),
  119. ## MilightDevice, WifiLight, ...
  120. ('brightness', 'brightness', u'%', '', 1, 1),
  121. ('brightness_on', 'brightness_on', u'%', '', 0, 1),
  122. ('ct', 'ct', 'K', '', 1, 1),
  123. ('hsv', 'hsv', '', '', 0, 0),
  124. ('transitionInProgress','transitionInProgress', '', '', 0, 0),
  125. ('hue', 'hue', '', '', 0, 0),
  126. ('saturation', 'saturation', '', '', 0, 1),
  127. ('RGB', 'RGB', '', '', 1, 0),
  128. ## MiLightBridge
  129. ('protocol', 'protocol', '', '', 0, 0),
  130. ('checkInterval', 'checkInterval', 's', '', 0, 0),
  131. ('sendInterval', 'sendInterval', 's', '', 0, 0),
  132. ## Speedtest
  133. ('download', 'download', 'Mbit/s', '', 1, 1),
  134. ('ping', 'ping', 'ms', '', 1, 1),
  135. ('state', 'state', '', '', 1, 0),
  136. ('upload', 'upload', 'Mbit/s', '', 1, 1),
  137. # ('path', 'path', '', '', 0, 0),
  138. ## TRAFFIC
  139. ('delay', 'delay', 'min', '', 1, 1),
  140. ('distance', 'distance', 'km', '', 1, 1),
  141. ('duration', 'duration', 'mins', '', 1, 1),
  142. ('duration_in_traffic', 'duration_in_traffic', 'mins', '', 1, 1),
  143. ]:
  144. ## reset
  145. display_value = ""
  146. ignoreit = ""
  147. # var_data_age = ""
  148. ## check if device output data (temperature, humidity, ... )
  149. try:
  150. if data[key]['value']:
  151. value = data[key]['value']
  152. ## build dynamic variable names ...
  153. var_max = 'level_%s_max' % title
  154. var_min = 'level_%s_min' % title
  155. var_data_age = '%s_date' % title
  156. var_plain = 'var_%s' % title ## e.g. contact: open (var_plain => open)
  157. # print var_data_age
  158. # if params[eval("desiredtemp_date")]:
  159. # print desiredtemp_date
  160. # pass
  161. # except:
  162. # pass
  163. try:
  164. if params[eval("var_max")]:
  165. ## check the difference between dewpoint and temperature
  166. if key == 'dewpoint':
  167. max_warn_dewpoint, max_crit_dewpoint = params[eval("var_max")]
  168. max_warn = float(data['temperature']['value']) - max_warn_dewpoint
  169. max_crit = float( data['temperature']['value']) - max_crit_dewpoint
  170. else:
  171. max_warn, max_crit = params[eval("var_max")]
  172. pass
  173. except:
  174. max_warn, max_crit = 0, 0
  175. pass
  176. try:
  177. if params[eval("var_min")]:
  178. min_warn, min_crit = params[eval("var_min")]
  179. pass
  180. except:
  181. min_warn, min_crit = 0, 0
  182. pass
  183. try:
  184. if params[eval("var_plain")]:
  185. var_plain = params[eval("var_plain")]
  186. pass
  187. except:
  188. var_plain = None
  189. pass
  190. try:
  191. if eval(var_data_age) and eval(var_data_age) != "":
  192. local_timestamp = time.time()
  193. var_data_timestamp = time.mktime(datetime.datetime.strptime(eval(var_data_age) , "%Y-%m-%d %H:%M:%S").timetuple())
  194. data_age = int( ( local_timestamp - var_data_timestamp ) / 60 )
  195. pass
  196. except:
  197. ## bugfix, at beginning all timestamp up-to-date
  198. ## necessary because some devices don't send all data (weather-provider has no battery)
  199. data_age = 0
  200. pass
  201. # if data_age > dage_crit:
  202. ## print ("data_age = %s, dage_crit = %s" % (data_age, dage_crit))
  203. # display_value = ("data to old [" + str(data_age) + "min] (!!)")
  204. # errorlevel = 2
  205. # elif data_age > dage_crit:
  206. # display_value = ("data to old [" + str(data_age) + "min] (!)")
  207. # warnlevel = 1
  208. # else:
  209. if key == 'humidity' and params['var_dewpoint_override'] == 'true':
  210. ignoreit = 'true'
  211. ## there are sometime duplicate keys on channel like controlmode in '_Clima' and '_Climate'
  212. ## Just filter out some HomeMatic-channel
  213. if channel == '' or data[key]['channel'] == "%s_%s" % (item, channel):
  214. ## check critical level (max/min)
  215. if ( (max_crit != 0 and float(value) >= max_crit) or (min_crit != 0 and float(value) <= min_crit) ) and not ignoreit:
  216. if max_crit != 0 and float(value) >= max_crit:
  217. display_value = ('%s%s (warn/crit at %s%s/%s%s) (!!)' % ( value, unit, max_warn, unit, max_crit, unit))
  218. elif float(value) <= min_crit:
  219. display_value = ('%s%s (warn/crit at %s%s/%s%s) (!!)' % ( value, unit, min_warn, unit, min_crit, unit))
  220. errorlevel = 2
  221. ## check warning level (max/min)
  222. elif ( (max_warn != 0 and float(value) >= max_warn) or (min_warn != 0 and float(value) <= min_warn) ) and not ignoreit:
  223. if max_warn != 0 and float(value) >= max_warn:
  224. display_value = ('%s%s (warn/crit at %s%s/%s%s) (!)' % ( value, unit, max_warn, unit, max_crit, unit))
  225. elif float(value) <= min_warn:
  226. display_value = ('%s%s (warn/crit at %s%s/%s%s) (!)' % ( value, unit, min_warn, unit, min_crit, unit))
  227. warnlevel = 1
  228. elif var_plain:
  229. ## just alert if value not identic or ignored
  230. if value == var_plain or var_plain == "ignore":
  231. if show:
  232. display_value = ('%s%s' % ( value, unit ))
  233. if perfd:
  234. perfdata.append( ( key , 1, "" ) )
  235. else:
  236. display_value = ("%s (expected: %s) (!!)" % (value, var_plain))
  237. errorlevel = 2
  238. if perfd:
  239. perfdata.append( ( key, 0, "" ) )
  240. ## define output
  241. elif show:
  242. display_value = ('%s%s' % ( value, unit ))
  243. ## define perfdata (just add numeric values)
  244. if perfd and not var_plain:
  245. perfdata.append( ( title, value, max_warn, max_crit, "", "" ) )
  246. ## not used (yet)
  247. # if data_age > dage_crit:
  248. # display_value += (" (data obsolet: %s min) (warn/crit at %s/%smin) (!!)" % (str(data_age), dage_warn, dage_crit))
  249. # errorlevel = 2
  250. # elif data_age > dage_warn:
  251. # display_value += (" (data obsolet: %s min) (warn/crit at %s/%smin) (!)" % (str(data_age), dage_warn, dage_crit))
  252. # warnlevel = 1
  253. if display_value != "":
  254. output.append('%s: %s' % (title, display_value))
  255. pass
  256. except:
  257. pass
  258. # ##################################################################
  259. if errorlevel > 1:
  260. exitcode = 2
  261. elif warnlevel > 0:
  262. exitcode = 1
  263. else:
  264. exitcode = 0
  265. return exitcode, ', '.join(output), perfdata
  266. factory_settings["fhem_default_params"] = {
  267. "level_data_age" : (30, 90), # warn/crit for data age (min)
  268. "level_temperature_max" : (26, 30), # warn/crit for max. temperature
  269. "level_temperature_min" : (15, 12), # warn/crit for min. temperature
  270. "level_humidity_max" : (70, 80), # warn/crit for max. humidity
  271. "level_humidity_min" : (50, 45), # warn/crit for min. humidity
  272. "level_dewpoint_max" : (3, 1), # warn/crit for diff to temperatur (exp. 17°C (dewp) vs 20°C(temp))
  273. "level_batteryLevel_min" : (2.3, 2.1), # warn/crit for max. temperature
  274. "level_download_min" : (10, 8), # warn/crit for min download (speedtest)
  275. "level_upload_min" : (1.5, 1), # warn/crit for min upload (speedtest)
  276. "level_ping" : (100, 150), # warn/crit for ping (speedtest)
  277. "var_activity" : ("alive"), # default for alive
  278. "var_contact" : ("ignore"), # default for contact
  279. "var_dewpoint_override" : ("true"), # don't alert humidity if dewpoint given
  280. "var_btnLock" : ("ignore"), # simple button lock
  281. "var_globalBtnLock" : ("ignore"), # full button lock
  282. "var_modusBtnLock" : ("ignore"), # modus button lock
  283. "var_RGB_color" : ("ignore"), # RGB color hexcode
  284. "var_brightness" : ("ignore"), # color brightness
  285. }
  286. check_info['fhem'] = {
  287. "check_function" : check_fhem,
  288. "inventory_function" : inventory_fhem,
  289. "service_description" : "FHEM %s",
  290. "has_perfdata" : True,
  291. "group" : "fhem",
  292. "default_levels_variable" : "fhem_default_params",
  293. }