HTTP Request / trim response

If you have a question or need help, this is the place to be.
Post Reply
Medy
Posts: 42
Joined: Sat Nov 15, 2014 8:33 pm
Location: Germany

HTTP Request / trim response

Post by Medy » Fri Jul 27, 2018 4:23 pm

Hi guys ,

[SOLVED by myself lol]

I want to make a api call / http get to my phillips hue bridge & get the temperature "field" into a evenghost variable.
Im not familiar with python , I would need to select the " temperature field" from the response body to get the temperature value.

I would also need to trim the temperature value from 2659 to 26.

Any help apriciated.


-->

url :
http://192.168.1.105/api/apiKey/sensors/7

Json response from browser :

state
temperature 2659
lastupdated "2018-07-27T16:01:42"
swupdate
state "noupdates"
lastinstall null
config
on true
battery 100
reachable true
alert "none"
ledindication false
usertest false
pending []
name "Hue temperature sensor 2"
type "ZLLTemperature"
modelid "SML001"
manufacturername "Philips"
swversion "6.1.0.18912"
uniqueid "00:17:8"
capabilities
certified true
Last edited by Medy on Fri Jul 27, 2018 7:32 pm, edited 1 time in total.

Medy
Posts: 42
Joined: Sat Nov 15, 2014 8:33 pm
Location: Germany

Re: HTTP Request / trim response

Post by Medy » Fri Jul 27, 2018 6:26 pm

Ok i found out how to do the http request


import requests
r = requests.get('http://192.168.1.105/api/KEY/sensors/7')
r.json()


Also that I can print it via
print(r.json())


2 things still unclear :

1. Acces temperature value directly
2. How to turn that temperature into a variable.

Edit : I went through the documentation

https://www.geeksforgeeks.org/get-post- ... ng-python/
http://docs.python-requests.org/en/late ... se-content

Result :

import requests

r = requests.get('http://192.168.1.105/api/{YOURAPIKE}Y/sensors/7')
data = r.json()

temp = data['state']['temperature']
eg.result = temp

Medy
Posts: 42
Joined: Sat Nov 15, 2014 8:33 pm
Location: Germany

Re: HTTP Request / trim response

Post by Medy » Fri Jul 27, 2018 7:24 pm

all thats lef ist is how to trim the temp number from 2690 to 26

ok that solves it

https://stackoverflow.com/questions/442 ... -a-integer


import requests
import math

r = requests.get('http://192.168.1.105/api/APIKEY/sensors/7')
data = r.json()

number = data['state']['temperature']

# Convert into string , then remove 2 digits and vonvert back to int
number = int(str(number)[:-2])
eg.result = number

User avatar
kgschlosser
Site Admin
Posts: 4621
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: HTTP Request / trim response

Post by kgschlosser » Mon Jul 30, 2018 5:12 pm

here is a suggestion for ya. but also some information for anyone else who may want a little type conversion tutorial



The only issue with using string splicing lie that is what happens if the temperature is 0. there is no 000 number. so this can be handled very easily.


we are going to convert the int (integer) into a float (decimal number). we can use that decimal number for a more accurate readout. and I am also going to show you how to round it so that a more accurate representation of the temperature is given.


we will use the 2690 as given in your example. first off we are going to make sure that it is in fact an int.

Code: Select all

temperature = int(data['state']['temperature'])

next we are going to turn it into a float and have it put the "." where it belongs. This is the key step to handling the 0 problem

Code: Select all

temperature /= 100.0
this just turned our number into 26.900000000

so if you wanted to keep the number this way and you want to print it out.

Code: Select all

print '%0.2f' % temperature
we use the % string modifier for this particular case. because we really do not need to see 9 decimal places of resolution. the % symbol is the identifier that we are using a modifier. the 0.2 means 2 decimal places of precision and the f means that the input value is a float. the second % is what starts the modification followed by the input value.


now if we wanted to covert this number to an integer the best representation of it in fact would be 27 and not 26 due to rounding.

Code: Select all

temperature = int(round(temperature))
the code above will do this for us.


there ya have it. the safest way to get a 2 digit integer that represents the temperature from a 4 digit integer representation of a decimal temperature
If you like the work I have been doing then feel free to Image

Medy
Posts: 42
Joined: Sat Nov 15, 2014 8:33 pm
Location: Germany

Re: HTTP Request / trim response

Post by Medy » Tue Aug 14, 2018 10:35 am

thx . this is a intersting & better solution
I see how there may be possability for error for mine.

Idk how the hue response will look if temp is below 10° ( 0900 ) or (900) ?

I automated my ventilation with this :D ... it was really tiring moving around with that late heatwave :D .
Now my fan goes On / off by itself ;) . Love that little hue sensor :D .... so worth

User avatar
kgschlosser
Site Admin
Posts: 4621
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: HTTP Request / trim response

Post by kgschlosser » Tue Aug 14, 2018 6:30 pm

that is the beauty of the way i went about it. it does not matter if it is prefixed with a 0 or not. once you turn it into an int/float it is going to remove the prefixed 0's

The reason why they are probably not using a float or a decimal representation of the temperature is because if they use a UINT8 or an unsigned integer that consumes 8 bits or a single byte of memory where as a float consumes 32 bits or 4 bytes of data. when you are dealing with microcontrollers that may only have 8K of code storage space and maybe 2K of ram to run the program in every single byte matters. because it could be the difference in having to go up which is more cost in engineering and more dollars spent
If you like the work I have been doing then feel free to Image

Post Reply