I'm new in python and I'm writing a simple script to work with Firebase but I'm stuck on a simple if statement that seems not working as expected:
## check for max values
if humidity > maxHumidity:
firebase.put("/Controls/Sensors", "/Humidity/max_inside", ""+humidity+"%")
print("Updating Humidity max_inside")
if temperature > maxTemperature:
firebase.put("/Controls/Sensors", "/Temperature/max_inside", ""+temperature+"C")
print("Updating Temperature max_inside")
## check for min values
if humidity < minHumidity:
firebase.put("/Controls/Sensors", "/Humidity/min_inside", ""+humidity+"%")
print("Updating Humidity min_inside")
if temperature < minTemperature:
firebase.put("/Controls/Sensors", "/Temperature/min_inside", ""+temperature+"C")
print("Updating Temperature min_inside")
The problem is that the first two if statements are working as expected, the last two not.. If the humidity < minHumidity, nothing happens.
The value that I'm using are double like 70.50..
Edit
##retrieve max & min humidity (remove the %)
maxHumidity = firebase.get("/Controls/Sensors/Humidity/max_inside", None)
maxHumidity = maxHumidity[:-1]
maxHumidity = float(maxHumidity)
minHumidity = firebase.get("/Controls/Sensors/Humidity/min_inside", None)
minHumidity = minHumidity[:-1]
minHumidity = float(minHumidity)
#retrieve max & min temperature (remove the C)
maxTemperature = firebase.get("/Controls/Sensors/Temperature/max_inside", None)
maxTemperature = maxTemperature[:-1]
maxTemperature = float(maxTemperature)
minTemperature = firebase.get("/Controls/Sensors/Temperature/min_inside", None)
minTemperature = minTemperature[:-1]
minTemperature = float(minTemperature)
#add current value
humidity, temperature = readDHT22()
firebase.put("/Controls/Sensors", "/Humidity/current_inside", ""+humidity+"%")
firebase.put("/Controls/Sensors", "/Temperature/current_inside", ""+temperature+"C")
##check for max values
if humidity > maxHumidity:
firebase.put("/Controls/Sensors", "/Humidity/max_inside", ""+humidity+"%")
print("Updating Humidity max_inside")
if temperature > maxTemperature:
firebase.put("/Controls/Sensors", "/Temperature/max_inside", ""+temperature+"C")
print("Updating Temperature max_inside")
## cehck for min values
if humidity < minHumidity:
firebase.put("/Controls/Sensors", "/Humidity/min_inside", ""+humidity+"%")
print("Updating Humidity min_inside")
if temperature < minTemperature:
firebase.put("/Controls/Sensors", "/Temperature/min_inside", ""+temperature+"C")
print("Updating Temperature min_inside")
Tried to use float but seems still not working! Is not going into the min ifs.
You are comparing strings with floating point numbers.
minTemperature
, maxTemperature
, minHumidity
and maxHumidity
are all float
objects because you converted them. But temperature
and humidity
are strings , because otherwise Python would have thrown an exception when you tried concatenating them with other strings.
Compare float
to float
by converting either in the test:
if float(humidity) > maxHumidity:
etc. or by converting humidity
and temperature
to floats and converting them back to strings when inserting into Firebase.
In Python 2, different types of objects are always consistently sorted, with numbers coming before other types. This means that <
and >
comparisons are true or false based on the sort order of the two operands, and since numbers in Python 2 are sorted first, any comparison with another type is done with the number considered smaller :
>>> 3.14 < "1.1"
True
Python 3 does away with trying to make everything orderable; comparing a float to a string gives you a TypeError
exception instead.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.