Thanks to autolog, DaveL17, RogueProeliator and kw123 for your help.
(I am not very good at python, so there is probably room for improvement.)
- Code: Select all
#!/usr/bin/env python
#get energy monitor data from efergy engage http://www.efergy.com
#by lochnesz
#version 1.0
#create variables in indigo before running this script (use variable id's instead of names if you'd prefer):
#"efergy_dev1_age"
#"efergy_dev1_reading"
#"efergy_dev1_battery"
#"efergy_dev1_falseBattery"
#"efergy_dev1_mac"
#"efergy_dev1_last_reading_time"
#create variable "efergy_dev1_token" in indigo with correct token before running this script
#to get token: https://engage.efergy.com/mobile/get_token?username=*username*&password=*password*&device=*token_name*
#alternative api url (example): http://www.energyhive.com/mobile_proxy/getStatus?token=xxxxxxxxxx
import simplejson as json
import urllib2
import time
import datetime
timeout = 3
#import token value from indigo variable
token=indigo.variables["efergy_dev1_token"].value
#debug to indigo log
#localtime = time.asctime( time.localtime(time.time()) )
#indigo.server.log ("Efergy monitor collector script started: " + localtime)
#get data in json from api url
url1Returned = urllib2.urlopen('https://engage.efergy.com/mobile_proxy/getStatus?token=' + token)
url2Returned = urllib2.urlopen('https://engage.efergy.com/mobile_proxy/getInstant?token=' + token)
#format api returned data
data_getStatus = url1Returned.read()
data_getInstant = url2Returned.read()
#interpret json
dict1 = json.loads(data_getStatus)
dict2 = json.loads(data_getInstant)
#get json variables to python variables
battery=str(dict1['listOfMacs'][0]['listofchannels'][0]['type']['battery']) #battery level
falseBattery=str(dict1['listOfMacs'][0]['listofchannels'][0]['type']['falseBattery'])
last_reading_ts=float(dict2['last_reading_time']) #value of checked date and time in ticks
mac=str(dict1['listOfMacs'][0]['mac']) #device mac address
age=str(dict2['age']) #age of last reading in seconds
reading=str(round(float(dict2['reading']),1)) #round float value of reading to one decimal and convert to string
#convert last_reading_ts ticks to local time
last_reading_time = datetime.datetime.fromtimestamp(last_reading_ts/1000).strftime('%Y-%m-%d %H:%M:%S')
#update indigo variables
indigo.variable.updateValue("efergy_dev1_battery", value=battery)
indigo.variable.updateValue("efergy_dev1_falseBattery", value= falseBattery)
indigo.variable.updateValue("efergy_dev1_last_reading_time", value= last_reading_time)
indigo.variable.updateValue("efergy_dev1_mac", value= mac)
indigo.variable.updateValue("efergy_dev1_age", value= age)
indigo.variable.updateValue("efergy_dev1_reading", value= reading)
#debug to indigo log
#indigo.server.log ("Battery: " + battery)
#indigo.server.log ("False battery: " + falseBattery)
#indigo.server.log ("Device MAC: " + mac)
#indigo.server.log ("Reading age: " + age)
#indigo.server.log ("Reading value (watts): " + reading)
#indigo.server.log ("Time for reading (local time): " + last_reading_time)
#debug to indigo log
#localtime = time.asctime( time.localtime(time.time()) )
#indigo.server.log ("Efergy monitor collector script finished: " + localtime)