fhem 18 KB

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