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
Notice: This forum has been recovered from an old backup, so some content, links, and dates may be outdated. The forum is currently read-only while we restore sign-in and registration functionality. Details
If you find this forum valuable and would like to help keep it online, donations to help cover hosting and domain costs are greatly appreciated, but never expected. You can support the forum through Buy Me a Coffee or Ko-fi. Thank you for helping preserve the EventGhost community.
If you find this forum valuable and would like to help keep it online, donations to help cover hosting and domain costs are greatly appreciated, but never expected. You can support the forum through Buy Me a Coffee or Ko-fi. Thank you for helping preserve the EventGhost community.
HTTP Request / trim response
HTTP Request / trim response
Last edited by Medy on Fri Jul 27, 2018 7:32 pm, edited 1 time in total.
Re: HTTP Request / trim response
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
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
Re: HTTP Request / trim response
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
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
- kgschlosser
- Site Admin
- Posts: 5190
- Joined: Fri Jun 05, 2015 5:43 am
- Location: Rocky Mountains, Colorado USA
Re: HTTP Request / trim response
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.
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
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.
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.
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
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
so if you wanted to keep the number this way and you want to print it out.
Code: Select all
print '%0.2f' % temperature
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))
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
Re: HTTP Request / trim response
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
... it was really tiring moving around with that late heatwave
.
Now my fan goes On / off by itself
. Love that little hue sensor
.... so worth
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
Now my fan goes On / off by itself
- kgschlosser
- Site Admin
- Posts: 5190
- Joined: Fri Jun 05, 2015 5:43 am
- Location: Rocky Mountains, Colorado USA
Re: HTTP Request / trim response
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
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
