Rounding and simplejson

Posted on
Mon Feb 23, 2015 11:39 am
Chockymonster offline
Posts: 84
Joined: Jul 19, 2014
Location: Sandhurst, UK

Rounding and simplejson

First warning - I'm not a coder, I can borrow other peoples code and hack it about to do what I want (normally in the most ineffective way possible, but hey!)

My heating is controlled by a tado intelligent thermostat, with a bit of digging (no public api available yet) I've found ways of interrogating the tado server to check basic status.
All I want to do is return that status into indigo as variables so I can add them to control pages and the like.

I've managed to do that by using this code:

Code: Select all
import urllib2
import simplejson
f = urllib2.urlopen('https://my.tado.com/mobile/1.4/getCurrentState?username=usernam,e&password=password')
json_string = f.read()
parsed_json = simplejson.loads(json_string)

tadooperation = parsed_json['autoOperation']
tadocontrol = parsed_json['controlPhase']
tadohousetemp = parsed_json['insideTemp']
tadosettemp = parsed_json['setPointTemp']
tadobox = parsed_json['boxConnected']
tadogateway = parsed_json['gwConnected']
tadots = parsed_json['tsConnected']

indigo.variable.updateValue(354149951, value=str(tadocontrol))
indigo.variable.updateValue(741265018, value=str(tadooperation))
indigo.variable.updateValue(187002669, value=str(tadohousetemp))
indigo.variable.updateValue(1159023434, value=str(tadosettemp))
indigo.variable.updateValue(1364699512, value=str(tadobox))
indigo.variable.updateValue(1132971864, value=str(tadogateway))
indigo.variable.updateValue(973513067, value=str(tadots))

f.close()


It works perfectly and I can update my control page, the only problem I'm having is the way tado returns the temperature.
I want to report it as 21.2 degrees but tado returns 21.1599998474 now I'm all for accuracy but that's a bit much!
I can't see a way to control the format of the numbers (easily!)

Can anyone offer a suggestion?

Posted on
Mon Feb 23, 2015 12:13 pm
DaveL17 offline
User avatar
Posts: 6756
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Rounding and simplejson

In this instance, I think this may be the simplest approach:

Code: Select all
a = 123.456
b = "%.1f" % a

'b' is your value to one decimal point. To get two decimal points, change the 1 to a 2.

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Mon Feb 23, 2015 2:48 pm
Chockymonster offline
Posts: 84
Joined: Jul 19, 2014
Location: Sandhurst, UK

Re: Rounding and simplejson

Thanks Dave.

Changed the variable section to:

Code: Select all
tadooperation = parsed_json['autoOperation']
tadocontrol = parsed_json['controlPhase']
tadohouselong = parsed_json['insideTemp']
tadohousetemp = "%.1f" % tadohouselong
tadosettemplong = parsed_json['setPointTemp']
tadosettemp = "%.1f" % tadosettemplong
tadobox = parsed_json['boxConnected']
tadogateway = parsed_json['gwConnected']
tadots = parsed_json['tsConnected']


Control page looks much tidier now!

Posted on
Mon Feb 23, 2015 2:58 pm
DaveL17 offline
User avatar
Posts: 6756
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Rounding and simplejson

Glad to help!

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Thu Dec 10, 2015 5:18 pm
Londonmark offline
Posts: 509
Joined: Feb 29, 2012

Re: Rounding and simplejson

Awesome. Just added this to my control pages too. Thank you!

Does anyone know the url for the Tado app? I would like to be able to tap on the set temp to open the app.

Posted on
Wed Dec 16, 2015 4:23 am
Chockymonster offline
Posts: 84
Joined: Jul 19, 2014
Location: Sandhurst, UK

Re: Rounding and simplejson

You don't need to! Setting the temp can be done from indigo!


Sent from my iPad using Tapatalk

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 13 guests