fhem 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. ('R-tempOffset', 'tempOffset', 'K', 'Clima', 1, 0),
  120. ## MilightDevice, WifiLight, ...
  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, 0),
  126. ('hue', 'hue', '', '', 0, 0),
  127. ('saturation', 'saturation', '', '', 0, 1),
  128. ('RGB', 'RGB', '', '', 1, 0),
  129. ## MiLightBridge
  130. ('protocol', 'protocol', '', '', 0, 0),
  131. ('checkInterval', 'checkInterval', 's', '', 0, 0),
  132. ('sendInterval', 'sendInterval', 's', '', 0, 0),
  133. ## Speedtest
  134. ('download', 'download', 'Mbit/s', '', 1, 1),
  135. ('ping', 'ping', 'ms', '', 1, 1),
  136. ('state', 'state', '', '', 1, 0),
  137. ('upload', 'upload', 'Mbit/s', '', 1, 1),
  138. # ('path', 'path', '', '', 0, 0),
  139. ## TRAFFIC
  140. ('delay', 'delay', 'min', '', 1, 1),
  141. ('distance', 'distance', 'km', '', 1, 1),
  142. ('duration', 'duration', 'mins', '', 1, 1),
  143. ('duration_in_traffic', 'duration_in_traffic', 'mins', '', 1, 1),
  144. ## ESPEasy
  145. ('presence', 'presence', '', '', 1, 1),
  146. ]:
  147. ## reset
  148. display_value = ""
  149. ignoreit = ""
  150. # var_data_age = ""
  151. ## check if device output data (temperature, humidity, ... )
  152. try:
  153. if data[key]['value']:
  154. value = data[key]['value']
  155. ## build dynamic variable names ...
  156. var_max = 'level_%s_max' % title
  157. var_min = 'level_%s_min' % title
  158. var_data_age = '%s_date' % title
  159. var_plain = 'var_%s' % title ## e.g. contact: open (var_plain => open)
  160. # print var_data_age
  161. # if params[eval("desiredtemp_date")]:
  162. # print desiredtemp_date
  163. # pass
  164. # except:
  165. # pass
  166. try:
  167. if params[eval("var_max")]:
  168. ## check the difference between dewpoint and temperature
  169. if key == 'dewpoint':
  170. max_warn_dewpoint, max_crit_dewpoint = params[eval("var_max")]
  171. max_warn = float(data['temperature']['value']) - max_warn_dewpoint
  172. max_crit = float( data['temperature']['value']) - max_crit_dewpoint
  173. else:
  174. max_warn, max_crit = params[eval("var_max")]
  175. pass
  176. except:
  177. max_warn, max_crit = 0, 0
  178. pass
  179. try:
  180. if params[eval("var_min")]:
  181. min_warn, min_crit = params[eval("var_min")]
  182. pass
  183. except:
  184. min_warn, min_crit = 0, 0
  185. pass
  186. try:
  187. if params[eval("var_plain")]:
  188. var_plain = params[eval("var_plain")]
  189. pass
  190. except:
  191. var_plain = None
  192. pass
  193. try:
  194. if eval(var_data_age) and eval(var_data_age) != "":
  195. local_timestamp = time.time()
  196. var_data_timestamp = time.mktime(datetime.datetime.strptime(eval(var_data_age) , "%Y-%m-%d %H:%M:%S").timetuple())
  197. data_age = int( ( local_timestamp - var_data_timestamp ) / 60 )
  198. pass
  199. except:
  200. ## bugfix, at beginning all timestamp up-to-date
  201. ## necessary because some devices don't send all data (weather-provider has no battery)
  202. data_age = 0
  203. pass
  204. # if data_age > dage_crit:
  205. ## print ("data_age = %s, dage_crit = %s" % (data_age, dage_crit))
  206. # display_value = ("data to old [" + str(data_age) + "min] (!!)")
  207. # errorlevel = 2
  208. # elif data_age > dage_crit:
  209. # display_value = ("data to old [" + str(data_age) + "min] (!)")
  210. # warnlevel = 1
  211. # else:
  212. if key == 'humidity' and params['var_dewpoint_override'] == 'true':
  213. ignoreit = 'true'
  214. ## there are sometime duplicate keys on channel like controlmode in '_Clima' and '_Climate'
  215. ## Just filter out some HomeMatic-channel
  216. if channel == '' or data[key]['channel'] == "%s_%s" % (item, channel):
  217. ## check critical level (max/min)
  218. if ( (max_crit != 0 and float(value) >= max_crit) or (min_crit != 0 and float(value) <= min_crit) ) and not ignoreit:
  219. if max_crit != 0 and float(value) >= max_crit:
  220. display_value = ('%s%s (warn/crit at %s%s/%s%s) (!!)' % ( value, unit, max_warn, unit, max_crit, unit))
  221. elif float(value) <= min_crit:
  222. display_value = ('%s%s (warn/crit at %s%s/%s%s) (!!)' % ( value, unit, min_warn, unit, min_crit, unit))
  223. errorlevel = 2
  224. ## check warning level (max/min)
  225. elif ( (max_warn != 0 and float(value) >= max_warn) or (min_warn != 0 and float(value) <= min_warn) ) and not ignoreit:
  226. if max_warn != 0 and float(value) >= max_warn:
  227. display_value = ('%s%s (warn/crit at %s%s/%s%s) (!)' % ( value, unit, max_warn, unit, max_crit, unit))
  228. elif float(value) <= min_warn:
  229. display_value = ('%s%s (warn/crit at %s%s/%s%s) (!)' % ( value, unit, min_warn, unit, min_crit, unit))
  230. warnlevel = 1
  231. elif var_plain:
  232. ## just alert if value not identic or ignored
  233. if value == var_plain or var_plain == "ignore":
  234. if show:
  235. display_value = ('%s%s' % ( value, unit ))
  236. if perfd:
  237. perfdata.append( ( title, 1, "" ) )
  238. else:
  239. display_value = ("%s (expected: %s) (!!)" % (value, var_plain))
  240. errorlevel = 2
  241. if perfd:
  242. perfdata.append( ( title, 0, "" ) )
  243. ## define output
  244. elif show:
  245. display_value = ('%s%s' % ( value, unit ))
  246. ## define perfdata (just add numeric values)
  247. if perfd and not var_plain:
  248. perfdata.append( ( title, value, max_warn, max_crit, "", "" ) )
  249. ## not used (yet)
  250. # if data_age > dage_crit:
  251. # display_value += (" (data obsolet: %s min) (warn/crit at %s/%smin) (!!)" % (str(data_age), dage_warn, dage_crit))
  252. # errorlevel = 2
  253. # elif data_age > dage_warn:
  254. # display_value += (" (data obsolet: %s min) (warn/crit at %s/%smin) (!)" % (str(data_age), dage_warn, dage_crit))
  255. # warnlevel = 1
  256. if display_value != "":
  257. output.append('%s: %s' % (title, display_value))
  258. pass
  259. except:
  260. pass
  261. # ##################################################################
  262. if errorlevel > 1:
  263. exitcode = 2
  264. elif warnlevel > 0:
  265. exitcode = 1
  266. else:
  267. exitcode = 0
  268. return exitcode, ', '.join(output), perfdata
  269. factory_settings["fhem_default_params"] = {
  270. "level_data_age" : (30, 90), # warn/crit for data age (min)
  271. "level_temperature_max" : (26, 30), # warn/crit for max. temperature
  272. "level_temperature_min" : (15, 12), # warn/crit for min. temperature
  273. "level_humidity_max" : (70, 80), # warn/crit for max. humidity
  274. "level_humidity_min" : (50, 45), # warn/crit for min. humidity
  275. "level_dewpoint_max" : (3, 1), # warn/crit for diff to temperatur (exp. 17°C (dewp) vs 20°C(temp))
  276. "level_batteryLevel_min" : (2.3, 2.1), # warn/crit for max. temperature
  277. "level_download_min" : (10.0, 8.0), # warn/crit for min download (speedtest)
  278. "level_upload_min" : (1.5, 1.0), # warn/crit for min upload (speedtest)
  279. "level_ping" : (100, 150), # warn/crit for ping (speedtest)
  280. "var_activity" : ("alive"), # default for alive
  281. "var_presence" : ("present"), # default for presence
  282. "var_contact" : ("ignore"), # default for contact
  283. "var_battery" : ("ok"), # default for battery
  284. "var_dewpoint_override" : ("true"), # don't alert humidity if dewpoint given
  285. "var_btnLock" : ("ignore"), # simple button lock
  286. "var_globalBtnLock" : ("ignore"), # full button lock
  287. "var_modusBtnLock" : ("ignore"), # modus button lock
  288. "var_RGB_color" : ("ignore"), # RGB color hexcode
  289. "var_brightness" : ("ignore"), # color brightness
  290. }
  291. check_info['fhem'] = {
  292. "check_function" : check_fhem,
  293. "inventory_function" : inventory_fhem,
  294. "service_description" : "FHEM %s",
  295. "has_perfdata" : True,
  296. "group" : "fhem",
  297. "default_levels_variable" : "fhem_default_params",
  298. }