Skip to content

Instantly share code, notes, and snippets.

@starbuck93
Last active July 4, 2018 20:51
Show Gist options
  • Save starbuck93/8e93de265df3e6e4f114383a14b1b38d to your computer and use it in GitHub Desktop.
Save starbuck93/8e93de265df3e6e4f114383a14b1b38d to your computer and use it in GitHub Desktop.
is the ac running based on if the temperature is rising or falling
import homeassistant.remote as remote
import time
import math
import sys
def updateState(previous,current):
settings = {"device_class": "cold", "friendly_name": "Is the AC Running"}
if previous.state != current.state and math.fabs(float(previous.state)-float(current.state)) > .2: #if the absolute value delta is between 0 and 0.2 then we don't want to update the state to prevent false positives
if previous.state > current.state: #air temp is falling
print(":: ",current.state,"AC is running")
remote.set_state(api, 'binary_sensor.ac_boolean', new_state=True, attributes=settings) #call API to update the state of the sensor
else : #air temp is rising
print(":: ",current.state,"AC is not running")
remote.set_state(api, 'binary_sensor.ac_boolean', new_state=False, attributes=settings) #call API to update the state of the sensor
else:
print(":: No update needed")
#setup
api = remote.API('127.0.0.1', 'super_secret_password') #set up HASS API handle
print(":: ",remote.validate_api(api)) #make sure it works
cold_ac_air_previous = remote.get_state(api, 'sensor.cold_ac_air') #get an initial reading
print(":: Initial reading:",cold_ac_air_previous.state) #print it out
if len(sys.argv) > 1: #check for user input, and if so, call the function for the first run
print(":: Got user input for previous:",sys.argv[1])
cold_ac_air = remote.get_state(api, 'sensor.cold_ac_air') #because it's easier to just call the API again for this variable
cold_ac_air_previous.state = sys.argv[1] #grab user input and name it something relevant
updateState(cold_ac_air_previous,cold_ac_air) #call deciding funciton to update AC state
cold_ac_air_previous = cold_ac_air #update that variable!
time.sleep(30) #wait 30 seconds to run the loop after gathering the first data point or updating based on user input
while True: #loop forever
cold_ac_air = remote.get_state(api, 'sensor.cold_ac_air') #get that data!
updateState(cold_ac_air_previous,cold_ac_air) #call deciding funciton to update AC state
cold_ac_air_previous = cold_ac_air #update that variable!
time.sleep(30) #wait 30 seconds and loop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment