GNU/Linux PYTHON3 Script to Read HUE Sensor DATA
A small python3 script I wrote to read sensor data and store in a logg file.
#!/usr/bin/env python3
import sys
import json
import urllib.request
import requests
import parsedatetime as pdt
# Constants...
cal = pdt.Calendar()
hue_url_sensors = "http://path.to.hue.bridge/api/{api-key}/sensors/"
hue_url_lights = "http://path.to.hue.bridge/api/{api-key}/lights/"
air_url_1 = "http://path.to.other.sensor-1:8082/sensors/"
air_url_2 = "http://path.to.other.sensor-3:8082/sensors/"
rel_url = "http://path.to.relay.raspi:8081/relays/"
light_on = json.dumps({'on': True})
light_off = json.dumps({'on': False})
# Switch function...
def switch(key, value):
action = 'N/A'
url = hue_url_lights + key + "/state/"
if value.lower() == 'off':
result = requests.put(url, data=light_off.encode('utf-8'))
elif value.lower() == 'on':
result = requests.put(url, data=light_on.encode('utf-8'))
print("INFO: ", "Light=", key, "Action=", value, "Result=", result)
# GetLights function
def getlights():
# Retrieve huedata (lights)...
output = ""
web_data_l = urllib.request.urlopen(hue_url_lights)
json_data_l = web_data_l.read()
json_enc_l = web_data_l.info().get_content_charset('utf-8')
json_lights = json.loads(json_data_l.decode(json_enc_l))
for key in json_lights:
if json_lights[key]['state']['reachable'] == True and json_lights[key]['state']['on'] == True:
state = "on"
else:
state = "off"
output = output + " " + str(key).zfill(2) + "\t" + json_lights[key]['name'].ljust(20) + str(key).ljust(2) + "\t" + state + "\n"
return output
# GetSensors function
def getSensors():
# Retreive Sensor Data
output = ""
web_data_l = urllib.request.urlopen(hue_url_sensors)
json_data_l = web_data_l.read()
json_enc_l = web_data_l.info().get_content_charset('utf-8')
json_sensors = json.loads(json_data_l.decode(json_enc_l))
for key in json_sensors:
isValid = True
if json_sensors[key]['type'] == 'ZLLLightLevel':
value = json_sensors[key]['state']['lightlevel']
elif json_sensors[key]['type'] == 'ZLLPresence':
value = json_sensors[key]['state']['presence']
elif json_sensors[key]['type'] == 'ZLLTemperature':
value = json_sensors[key]['state']['temperature'] /100
else:
isValid = False
if isValid == True:
output = output + " " + str(key).zfill(2) + "\t" + str(json_sensors[key]['name']).ljust(20) + "\t\t" + str(value) + ""
return output
# GetSensors function
def getAirinfo(Url, DeviceId):
# Retreive Sensor Data
format = '%Y-%m-%dT%H:%M:%S'
output = ""
web_data_l = urllib.request.urlopen(Url)
json_data_l = web_data_l.read()
json_enc_l = web_data_l.info().get_content_charset('utf-8')
json_sensors = json.loads(json_data_l.decode(json_enc_l))['sensors']
for key in json_sensors:
output = output + str(key) + ": " + str(json_sensors[key]['value']).ljust(5)
datetxt = str(cal.parseDT(datetimeString=json_sensors[key]['LastModified'][5:-4])[0].strftime(format))
return datetxt + " PS" + DeviceId + " " + output + "\n"
# GetRelayState function
def getRelayinfo(Url):
# Retreive Sensor Data
r1 = False
r2 = False
r8 = False
VENT = "NA"
output = ""
web_data_l = urllib.request.urlopen(Url)
json_data_l = web_data_l.read()
json_enc_l = web_data_l.info().get_content_charset('utf-8')
json_sensors = json.loads(json_data_l.decode(json_enc_l))['relays']
for key in json_sensors:
if key == '1':
if json_sensors[key]['state'] == 1:
r1=True
elif key == '2':
if json_sensors[key]['state'] == 1:
r2=True
elif key == '8':
if json_sensors[key]['state'] == 1:
r8=True
if r1 == False and r2 == True:
VENT = "HIGH"
if r1 == False and r2 == False:
VENT = "MEDIUM"
if r1 == True:
VENT = "LOW"
output = " RE\tVENT".ljust(20) + "\t\t" + VENT
return output
# Construct switch command...
with open("/var/log/ax.log", "a") as logfile:
logfile.write(getAirinfo(air_url_1, "01"))
logfile.write(getAirinfo(air_url_2, "02"))