I have a function in Python that reads a ds18b20 temp sensor. The sensor gives me a faulty value (-0.062) about 5% of the time that I read it. This is not a problem but I do not want to log the value since it looks ugly in my graphs.
I can't manage to "catch" the value in an if-statement to replace it with "#error". The code below runs nicely but it appears that the if-statement is faulty and does not work - it just runs everything under the else.
I have tried everything, even "catching" all values between 1000 and 1500 (present temperature reading before dividing by 1000) to check if it would work with any temperature, but it does not.
Does anyone have any idea why my if-statement does not work?
def readtwo():
tfile = open("/sys/bus/w1/devices/28-0000040de8fc/w1_slave")
text = tfile.read()
tfile.close()
secondline = text.split("\n")[1]
temperaturedata = secondline.split(" ")[9]
temperature = float(temperaturedata[2:])
temperature = temperature / 1000
if temperature == -0.062:
return("#error")
else:
return(temperature)
floatcomparisons are inaccurate. You're better off comparing the integer value you get back before you convert it to a float (e.g.if temperaturedata[2:] == '-62': ...)textvariable that has the error you mention? I think then people can help you better.