Advertisement
jarekmor

Bayesian_binary_sensor

Oct 8th, 2021
500
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.27 KB | None | 0 0
  1. from requests import get
  2. import json
  3.  
  4. IP = '192.168.1.5:8123'  #IP address of your HA server
  5. END_POINT = "states"
  6. url = "http://{}/api/{}".format(IP ,END_POINT)
  7.  
  8. HA_API_KEY = "Bearer XXXXXXXXXXXXXXXXXXXXX"  #Long-Lived Access Tokens from HA
  9.  
  10. headers={"Authorization": HA_API_KEY,
  11.         "content-type": "application/json"
  12.         }
  13. response = get(url, headers=headers)
  14. response_json = response.json()
  15.  
  16. #search of HA's entities and their states or attributes for defined observations
  17. for i in range(len(list(response_json))):
  18.     #entity_id: sun.sun
  19.     if response_json[i]['entity_id'] == 'sun.sun':
  20.         output_sun = response_json[i]['state']
  21.     #entity_id: weather.home
  22.     if response_json[i]['entity_id'] == 'weather.home':
  23.         output_weather = response_json[i]['state']
  24.     #entity_id: sensor.airly_pressure_2
  25.     if response_json[i]['entity_id'] == "sensor.airly_pressure_2":
  26.         output_airly_pressure = float(response_json[i]['state'])
  27.     #entity_id: sensor.airly_humidity_2
  28.     if response_json[i]['entity_id'] == "sensor.airly_humidity_2":
  29.         output_airly_humid = float(response_json[i]['state'])
  30.  
  31. #main function based on Bayesian rule
  32. def update_probability(prior, prob_true, prob_false=0.):
  33.     numerator = prob_true * prior
  34.     denominator = numerator + prob_false * (1 - prior)
  35.     probability = numerator / denominator
  36.     return probability
  37.  
  38. #initial parameters for Bayesian function
  39. prior = 0.1
  40. threshold = 0.9
  41. posterior = 0
  42.  
  43. #observations for Bayesian function with prob_true and prob_false propabilities
  44. #entity_id: weather_home
  45. prob_true_1 = 0.6
  46. prob_false_1 = 0.2
  47. sensor_cloudy = output_weather
  48. try:
  49.     if sensor_cloudy == 'cloudy':
  50.         posterior = update_probability(prior, prob_true_1, prob_false_1)
  51.         prior = posterior
  52. except:
  53.     prior = prior
  54.  
  55. #entity_id: weather_home
  56. prob_true_2 = 0.6
  57. prob_false_2 = 0.1
  58. sensor_snowy = output_weather
  59. try:
  60.     if sensor_snowy == 'snowy':
  61.         posterior = update_probability(prior, prob_true_2, prob_false_2)
  62.         prior = posterior
  63. except:
  64.     prior = prior
  65.  
  66. #entiry_id: weather_home
  67. prob_true_3 = 0.9
  68. prob_false_3 = 0.1
  69. sensor_rainy = "rainy"
  70. try:
  71.     if sensor_rainy == 'rainy':
  72.         posterior = update_probability(prior, prob_true_3, prob_false_3)
  73.         prior = posterior
  74. except:
  75.     prior = prior
  76.  
  77. #entity_id: sun.sun
  78. prob_true_4 = 0.5
  79. prob_false_4 = 0.5
  80. sensor_sun_state = output_sun
  81. try:
  82.     if sensor_sun_state == 'above':
  83.         posterior = update_probability(prior, prob_true_4, prob_false_4)
  84.         prior = posterior
  85. except:
  86.     prior = prior    
  87.  
  88. #entity_id: sensor.airly_pressure
  89. prob_true_5 = 0.65
  90. prob_false_5 = 0.1
  91. sensor_pressure = output_airly_pressure
  92. try:
  93.     if sensor_pressure <= 1000:
  94.         posterior = update_probability(prior, prob_true_5)
  95.         prior = posterior
  96. except:
  97.     prior = prior
  98.  
  99. #entity_id: sensor.airly_humidity
  100. prob_true_6 = 0.5
  101. prob_false_6 = 0.1
  102. sensor_humid = output_airly_humid
  103. try:
  104.     if sensor_humid >= 70:
  105.         posterior = update_probability(prior, prob_true_6, prob_false_6)
  106.         prior = posterior
  107. except:
  108.     prior = prior
  109.  
  110. if posterior > 0 and posterior >= threshold:
  111.     print('Rain!', ' - probability: ', posterior, ' - threshold: ', threshold)
  112. else:
  113.     print('No rain')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement