fhem 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. model = ""
  59. output = []
  60. data = {}
  61. # ## bugfix, at beginning all timestamp up-to-date
  62. # ## necessary because some devices don't send all data (weather-provider has no battery)
  63. # battery_timestamp, dewpoint_timespamp, state_timestamp = time.time(), time.time(), time.time()
  64. errorlevel = 0
  65. ourstatus = 0
  66. device = ""
  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. ourstatus = 1
  72. elif ourstatus == 1:
  73. break
  74. elif ourstatus == 1:
  75. # if line[0] == model:
  76. # model = line[0]
  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. # hour2min=
  86. data[line[3]]['value']=(int(line[4])*60)+int(line[6])
  87. # mostly config mistake or incorrect agent output
  88. if ourstatus == 0:
  89. return (3, "UNKNOWN - unable to parse %s" % (item))
  90. perfdata = []
  91. # ##################################################################
  92. ## key => variable_name
  93. ## title => readable output
  94. ## unit => °C, &, V., kmh, ... (for output)
  95. ## channel => filter for channel (HomeMatic)
  96. ## show => print if no error
  97. ## perfd => print perfdata
  98. for key, title, unit, channel, show, perfd in [
  99. ## key title unit channel show perfd
  100. ## ------- ------- ------- ------- ---- -----
  101. ('temperature', 'temperature', u'°C', '', 1, 1),
  102. ('desired-temp', 'desiredtemp', u'°C', '', 1, 1),
  103. ('humidity', 'humidity', u'%', '', 1, 1),
  104. ('dewpoint', 'dewpoint', u'°C', '', 1, 1),
  105. ('contact', 'contact', '', '', 1, 1),
  106. ('valveposition', 'valveposition', u'%', '', 1, 1),
  107. ('battery', 'battery', '', '', 1, 0),
  108. ('batteryLevel', 'batteryLevel', 'V', '', 1, 1),
  109. ('controlMode', 'controlMode', '', 'Clima', 0, 0),
  110. ('Activity', 'activity', '', '', 0, 1),
  111. ('power', 'power', 'kWh', '', 1, 1),
  112. ('energy', 'energy', 'W', '', 1, 1),
  113. ('voltage', 'voltage', 'V', '', 1, 1),
  114. ## HomeMatic
  115. ('measured-temp', 'temperature', u'°C', '', 1, 1),
  116. ('actuator', 'valveposition', u'%', '', 1, 1),
  117. ('R-btnLock', 'btnLock', '', '', 0, 0),
  118. ('R-globalBtnLock', 'globalBtnLock', '', '', 0, 0),
  119. ('R-modusBtnLock', 'modusBtnLock', '', '', 0, 0),
  120. ## MilightDevice (and others?)
  121. ('brightness', 'brightness', u'%', '', 1, 1),
  122. ('brightness_on', 'brightness_on', u'%', '', 0, 1),
  123. ('ct', 'ct', 'K', '', 1, 1),
  124. ('hsv', 'hsv', '', '', 0, 0),
  125. ('transitionInProgress','transitionInProgress', '', '', 0, 1),
  126. ## MiLightBridge
  127. ('protocol', 'protocol', '', '', 0, 0),
  128. ('checkInterval', 'checkInterval', 's', '', 0, 0),
  129. ('sendInterval', 'sendInterval', 's', '', 0, 0),
  130. ## Speedtest
  131. ('download', 'download', 'Mbit/s', '', 1, 1),
  132. ('ping', 'ping', 'ms', '', 1, 1),
  133. ('state', 'state', '', '', 1, 0),
  134. ('upload', 'upload', 'Mbit/s', '', 1, 1),
  135. # ('path', 'path', '', '', 0, 0),
  136. ## TRAFFIC
  137. ('delay', 'delay', 'min', '', 1, 1),
  138. ('distance', 'distance', 'km', '', 1, 1),
  139. ('duration', 'duration', 'mins', '', 1, 1),
  140. ('duration_in_traffic', 'duration_in_traffic', 'mins', '', 1, 1),
  141. ]:
  142. ## reset
  143. display_value = ""
  144. ignoreit = ""
  145. # var_data_age = ""
  146. ## check if device output data (temperature, humidity, ... )
  147. try:
  148. if data[key]['value']:
  149. value = data[key]['value']
  150. ## build dynamic variable names ...
  151. var_max = 'level_%s_max' % title
  152. var_min = 'level_%s_min' % title
  153. var_data_age = '%s_date' % title
  154. var_plain = 'var_%s' % title ## e.g. contact: open (var_plain => open)
  155. # print var_data_age
  156. # if params[eval("desiredtemp_date")]:
  157. # print desiredtemp_date
  158. # pass
  159. # except:
  160. # pass
  161. try:
  162. if params[eval("var_max")]:
  163. ## check the difference between dewpoint and temperature
  164. if key == 'dewpoint':
  165. max_warn_dewpoint, max_crit_dewpoint = params[eval("var_max")]
  166. max_warn = float(data['temperature']['value']) - max_warn_dewpoint
  167. max_crit = float( data['temperature']['value']) - max_crit_dewpoint
  168. else:
  169. max_warn, max_crit = params[eval("var_max")]
  170. pass
  171. except:
  172. max_warn, max_crit = 0, 0
  173. pass
  174. try:
  175. if params[eval("var_min")]:
  176. min_warn, min_crit = params[eval("var_min")]
  177. pass
  178. except:
  179. min_warn, min_crit = 0, 0
  180. pass
  181. try:
  182. if params[eval("var_plain")]:
  183. var_plain = params[eval("var_plain")]
  184. pass
  185. except:
  186. var_plain = None
  187. pass
  188. try:
  189. if eval(var_data_age) and eval(var_data_age) != "":
  190. local_timestamp = time.time()
  191. var_data_timestamp = time.mktime(datetime.datetime.strptime(eval(var_data_age) , "%Y-%m-%d %H:%M:%S").timetuple())
  192. data_age = int( ( local_timestamp - var_data_timestamp ) / 60 )
  193. pass
  194. except:
  195. ## bugfix, at beginning all timestamp up-to-date
  196. ## necessary because some devices don't send all data (weather-provider has no battery)
  197. data_age = 0
  198. pass
  199. # if data_age > dage_crit:
  200. ## print ("data_age = %s, dage_crit = %s" % (data_age, dage_crit))
  201. # display_value = ("data to old [" + str(data_age) + "min] (!!)")
  202. # errorlevel = 2
  203. # elif data_age > dage_crit:
  204. # display_value = ("data to old [" + str(data_age) + "min] (!)")
  205. # warnlevel = 1
  206. # else:
  207. if key == 'humidity' and params['var_dewpoint_override'] == 'true':
  208. ignoreit = 'true'
  209. ## there are sometime duplicate keys on channel like controlmode in '_Clima' and '_Climate'
  210. ## Just filter out some HomeMatic-channel
  211. if channel == '' or data[key]['channel'] == "%s_%s" % (item, channel):
  212. ## check critical level (max/min)
  213. if ( (max_crit != 0 and float(value) >= max_crit) or (min_crit != 0 and float(value) <= min_crit) ) and not ignoreit:
  214. if max_crit != 0 and float(value) >= max_crit:
  215. display_value = ('%s%s (warn/crit at %s%s/%s%s) (!!)' % ( value, unit, max_warn, unit, max_crit, unit))
  216. elif float(value) <= min_crit:
  217. display_value = ('%s%s (warn/crit at %s%s/%s%s) (!!)' % ( value, unit, min_warn, unit, min_crit, unit))
  218. errorlevel = 2
  219. ## check warning level (max/min)
  220. elif ( (max_warn != 0 and float(value) >= max_warn) or (min_warn != 0 and float(value) <= min_warn) ) and not ignoreit:
  221. if max_warn != 0 and float(value) >= max_warn:
  222. display_value = ('%s%s (warn/crit at %s%s/%s%s) (!)' % ( value, unit, max_warn, unit, max_crit, unit))
  223. elif float(value) <= min_warn:
  224. display_value = ('%s%s (warn/crit at %s%s/%s%s) (!)' % ( value, unit, min_warn, unit, min_crit, unit))
  225. warnlevel = 1
  226. elif var_plain:
  227. ## just alert if value not identic or ignored
  228. if value == var_plain or var_plain == "ignore":
  229. if show:
  230. display_value = ('%s%s' % ( value, unit ))
  231. if perfd:
  232. perfdata.append( ( key , 1, "" ) )
  233. else:
  234. display_value = ("%s (expected: %s) (!!)" % (value, var_plain))
  235. errorlevel = 2
  236. if perfd:
  237. perfdata.append( ( key, 0, "" ) )
  238. ## define output
  239. elif show:
  240. display_value = ('%s%s' % ( value, unit ))
  241. ## define perfdata (just add numeric values)
  242. if perfd and not var_plain:
  243. perfdata.append( ( title, value, max_warn, max_crit, "", "" ) )
  244. ## not used (yet)
  245. # if data_age > dage_crit:
  246. # display_value += (" (data obsolet: %s min) (warn/crit at %s/%smin) (!!)" % (str(data_age), dage_warn, dage_crit))
  247. # errorlevel = 2
  248. # elif data_age > dage_warn:
  249. # display_value += (" (data obsolet: %s min) (warn/crit at %s/%smin) (!)" % (str(data_age), dage_warn, dage_crit))
  250. # warnlevel = 1
  251. if display_value != "":
  252. output.append('%s: %s' % (title, display_value))
  253. pass
  254. except:
  255. pass
  256. # ##################################################################
  257. if errorlevel > 1:
  258. exitcode = 2
  259. elif warnlevel > 0:
  260. exitcode = 1
  261. else:
  262. exitcode = 0
  263. return exitcode, ', '.join(output), perfdata
  264. factory_settings["fhem_default_params"] = {
  265. "level_data_age" : (30, 90), # warn/crit for data age (min)
  266. "level_temperature_max" : (26, 30), # warn/crit for max. temperature
  267. "level_temperature_min" : (15, 12), # warn/crit for min. temperature
  268. "level_humidity_max" : (70, 80), # warn/crit for max. humidity
  269. "level_humidity_min" : (50, 45), # warn/crit for min. humidity
  270. "level_dewpoint_max" : (3, 1), # warn/crit for diff to temperatur (exp. 17°C (dewp) vs 20°C(temp))
  271. "level_batteryLevel_min" : (2.1, 2.3), # warn/crit for max. temperature
  272. "level_download_min" : (10, 8), # warn/crit for min download (speedtest)
  273. "level_upload_min" : (1.5, 1), # warn/crit for min upload (speedtest)
  274. "level_ping" : (100, 150), # warn/crit for ping (speedtest)
  275. "var_activity" : ("alive"), # default for alive
  276. "var_contact" : ("ignore"), # default for contact
  277. "var_dewpoint_override" : ("true"), # don't alert humidity if dewpoint given
  278. "var_btnLock" : ("ignore"), # simple button lock
  279. "var_globalBtnLock" : ("ignore"), # full button lock
  280. "var_modusBtnLock" : ("ignore"), # modus button lock
  281. }
  282. check_info['fhem'] = {
  283. "check_function" : check_fhem,
  284. "inventory_function" : inventory_fhem,
  285. "service_description" : "FHEM %s",
  286. "has_perfdata" : True,
  287. "group" : "fhem",
  288. "default_levels_variable" : "fhem_default_params",
  289. }