Archives
Trending
Support
Login
clear text
XML
Django
JavaScript
MATLAB
C
C++
C#
Python
SQL
Shell
Bash
Markdown
YAML
JSON
HTML
CSS
PHP
Java
Ruby
Go
Rust
Swift
Kotlin
Arduino
TypeScript
Perl
Autohotkey
Lua
SQF
R
Scala
Haskell
Groovy
Dart
Clojure
VB.NET
Objective-C
PowerShell
Bash
CoffeeScript
Verilog
from requests import get import json IP = '192.168.1.5:8123' #IP address of your HA server END_POINT = "states" url = "http://{}/api/{}".format(IP ,END_POINT) HA_API_KEY = "Bearer XXXXXXXXXXXXXXXXXXXXX" #Long-Lived Access Tokens from HA headers={"Authorization": HA_API_KEY, "content-type": "application/json" } response = get(url, headers=headers) response_json = response.json() #search of HA's entities and their states or attributes for defined observations for i in range(len(list(response_json))): #entity_id: sun.sun if response_json[i]['entity_id'] == 'sun.sun': output_sun = response_json[i]['state'] #entity_id: weather.home if response_json[i]['entity_id'] == 'weather.home': output_weather = response_json[i]['state'] #entity_id: sensor.airly_pressure_2 if response_json[i]['entity_id'] == "sensor.airly_pressure_2": output_airly_pressure = float(response_json[i]['state']) #entity_id: sensor.airly_humidity_2 if response_json[i]['entity_id'] == "sensor.airly_humidity_2": output_airly_humid = float(response_json[i]['state']) #main function based on Bayesian rule def update_probability(prior, prob_true, prob_false=0.): numerator = prob_true * prior denominator = numerator + prob_false * (1 - prior) probability = numerator / denominator return probability #initial parameters for Bayesian function prior = 0.1 threshold = 0.9 posterior = 0 #observations for Bayesian function with prob_true and prob_false propabilities #entity_id: weather_home prob_true_1 = 0.6 prob_false_1 = 0.2 sensor_cloudy = output_weather try: if sensor_cloudy == 'cloudy': posterior = update_probability(prior, prob_true_1, prob_false_1) prior = posterior except: prior = prior #entity_id: weather_home prob_true_2 = 0.6 prob_false_2 = 0.1 sensor_snowy = output_weather try: if sensor_snowy == 'snowy': posterior = update_probability(prior, prob_true_2, prob_false_2) prior = posterior except: prior = prior #entiry_id: weather_home prob_true_3 = 0.9 prob_false_3 = 0.1 sensor_rainy = "rainy" try: if sensor_rainy == 'rainy': posterior = update_probability(prior, prob_true_3, prob_false_3) prior = posterior except: prior = prior #entity_id: sun.sun prob_true_4 = 0.5 prob_false_4 = 0.5 sensor_sun_state = output_sun try: if sensor_sun_state == 'above': posterior = update_probability(prior, prob_true_4, prob_false_4) prior = posterior except: prior = prior #entity_id: sensor.airly_pressure prob_true_5 = 0.65 prob_false_5 = 0.1 sensor_pressure = output_airly_pressure try: if sensor_pressure <= 1000: posterior = update_probability(prior, prob_true_5) prior = posterior except: prior = prior #entity_id: sensor.airly_humidity prob_true_6 = 0.5 prob_false_6 = 0.1 sensor_humid = output_airly_humid try: if sensor_humid >= 70: posterior = update_probability(prior, prob_true_6, prob_false_6) prior = posterior except: prior = prior if posterior > 0 and posterior >= threshold: print('Rain!', ' - probability: ', posterior, ' - threshold: ', threshold) else: print('No rain')
Mark as private (unlisted)
for 30 minutes
for 6 hours
for 1 day
for 1 week
for 1 month
for 1 year
everlasting (like CCCP)