![]() |
Number logic problem - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Number logic problem (/thread-613.html) Pages:
1
2
|
Number logic problem - m1xzg - Oct-23-2016 Hello all, I'm having a bit of a problem with some number logic in a python script I'm working on. The back story is this... I have a raspberry Pi with a DHT22 temperature/humidity sensor attached, the script that reads and records the data works very well. I wanted to progress this by adding in some logic that will allow the script to turn on a dehumidifier when the humidity is high. The dehumidifier is connected using a TP-LINK HS110 wifi switch. So, the problem I have with the script .. I've declared my high and low threshold numbers hithresh = 65 lothresh = 55 The routine to query the DHT22 works fine, so at this point it runs and gets a humidity value. This is where I have an issue deciding if the dehumidifier should be turned on... (not python, just my logic as I see it) if HUMIDITY is greater than or equal to HITHRESH TURN ON dehumidifier if HUMIDITY is lower than or equal to LOTHRESH TURN OFF dehumidifier I hope that makes some sense .. basically if the humidity is 65 or higher, turn it on, if it's 55 or lower it can be switched off. If anyone can help me with this it would be greatly appreciated. RE: Number logic problem - sparkz_alot - Oct-23-2016 The easiest way is to write it in what ever scripting language you're using, run it and see if it works. Are you asking for someone to write the script for you? RE: Number logic problem - m1xzg - Oct-23-2016 I have the bulk of the script in python already .. it's just the logic of the if statement or whatever is the issue... hithresh = 65 lothresh = 55 # some code here to get the humidity is called and returns the humidity as a variable 'humidity' if humidity < hithresh : print ("Current time %s" % now ) print "Humidity is ", humidity, ". We can turn off the dehumidifier" print "Switching off the dehumidifier" subprocess.check_output("/usr/local/bin/hs100.sh 172.16.29.146 9999 off", shell=True) time.sleep(3) dehumidpwr = subprocess.check_output("/usr/local/bin/hs100.sh 172.16.29.146 9999 check", shell=True) print "Dehumidifier is ", dehumidpwr print "=======================================================================" elif hithresh > humidity: print ("Current time %s" % now ) print "Humidity is ", humidity, ". We should turn on the dehumidifier" print "Switching on the dehumidifier" subprocess.check_output("/usr/local/bin/hs100.sh 172.16.29.146 9999 on", shell=True) time.sleep(3) dehumidpwr = subprocess.check_output("/usr/local/bin/hs100.sh 172.16.29.146 9999 check", shell=True) print "Dehumidifier is ", dehumidpwr print "=======================================================================" RE: Number logic problem - sparkz_alot - Oct-23-2016 A simple example would be: humidity = 60 if humidity <= 55: print("humidity to low") elif humidity >= 65: print("humidity to high") else: print("humidity is Supercalifragilisticexpialidocious")humidity of course will come from else where in your script. Here it's just hard coded for testing purposes. also watch you indentation, particularly at the end RE: Number logic problem - Yoriz - Oct-23-2016 Also consider the need to turn it on or off when already in that state, you may want a variable to keep an eye on that and only change its state if not already in that state. RE: Number logic problem - m1xzg - Oct-23-2016 I'd tried that but something doesn't quite work right .. here is the code now: hithresh = 65 lothresh = 55 # code goes and gets the humidity, returns number to 'humidity' if humidity <= lothresh: print "Humidity is ", humidity print("humidity to low") elif humidity >= hithresh: print "Humidity is ", humidity print("humidity to high") else: print("humidity is Supercalifragilisticexpialidocious")The result of the above is this .. but with the threshold of 65 this means the answer below is wrong. It should be "Supercalifragilisticexpialidocious" correct? root@pi1: ./humdity-check.py Humidity is 61 humidity to high Here the entire script .. perhaps it'll help better .. #!/usr/bin/python # HEADER ################################################################################################### # # Author : Robert McKenzie <[email protected]> # Script Name : humidy-check.py # ################################################################################################### hithresh = 65 lothresh = 55 import time now = time.strftime("%c") import sys import Adafruit_DHT import RPi.GPIO as GPIO import subprocess sensor = Adafruit_DHT.DHT22 pin = 4 GPIO.setmode(GPIO.BCM) while 1: # This gets the temp and humidity from the sensor and sets the result to a whole number instead of 15 decimal places humidity, temperature = Adafruit_DHT.read_retry(sensor, pin) if temperature is not None: temperature = '{:.0f}'.format(temperature) if humidity is not None: humidity = '{:.0f}'.format(humidity) # if humidity <= 55: # print "Humidity is ", humidity # print("humidity to low") # elif humidity >= 65: # print "Humidity is ", humidity # print("humidity to high") # else: # print("humidity is Supercalifragilisticexpialidocious") # sys.exit(0) if humidity >= hithresh : print ("Current time %s" % now ) print "Humidity is ", humidity, ". We can turn off the dehumidifier" print "Switching off the dehumidifier" #subprocess.check_output("/usr/local/bin/hs100.sh 172.16.29.146 9999 off", shell=True) #time.sleep(3) #dehumidpwr = subprocess.check_output("/usr/local/bin/hs100.sh 172.16.29.146 9999 check", shell=True) #print "Dehumidifier is ", dehumidpwr print "=======================================================================" elif humidity <= lothresh: print ("Current time %s" % now ) print "Humidity is ", humidity, ". We should turn on the dehumidifier" print "Switching on the dehumidifier" #subprocess.check_output("/usr/local/bin/hs100.sh 172.16.29.146 9999 on", shell=True) #time.sleep(3) #dehumidpwr = subprocess.check_output("/usr/local/bin/hs100.sh 172.16.29.146 9999 check", shell=True) #print "Dehumidifier is ", dehumidpwr print "=======================================================================" else: print "Do nothing" print ("Current time %s" % now ) print "Humidity is ", humidity print "=======================================================================" sys.exit(0) RE: Number logic problem - sparkz_alot - Oct-23-2016 Your first example works fine for me. Your second example (the full script) is missing a bunch of parenthesis with the print statements, for example line 45 print "Humidity is ", humidity, ". We can turn off the dehumidifier"which, btw can be better written as print("Humidity is {}. We can turn off the dehumidifier".format(dehumidifier))unless you're using python 2.x RE: Number logic problem - ichabod801 - Oct-23-2016 Your first set of code works fine for me. Your second set of code fails as you explain. It does so because you convert temperature and humidity into strings, and then compare humidity to hithresh and lothresh, which are integers. This is causing the conditionals to work incorrectly. If you want to convert temperature and humidity to strings for output, put the converted values into different variables. It would be better to use some sort of string formatting for that, either the % formatting you are already using for the time format, or the newer format method of strings. RE: Number logic problem - m1xzg - Oct-23-2016 (Oct-23-2016, 08:01 PM)ichabod801 Wrote: If you want to convert temperature and humidity to strings for output, put the converted values into different variables. It would be better to use some sort of string formatting for that, either the % formatting you are already using for the time format, or the newer format method of strings. Can you give me an example of what you mean? Can you tell my python skills are rubbish yet? haha humidity, temperature = Adafruit_DHT.read_retry(sensor, pin) if temperature is not None: temperature = '{:.0f}'.format(temperature) if humidity is not None: humidity = '{:.0f}'.format(humidity)This is what you mean should be re-done? The reason for this was to convert change the 15 decimal output to a whole number .. what is the correct way? RE: Number logic problem - sparkz_alot - Oct-23-2016 when you take a line like this temperature = '{:.0f}'.format(temperature)you are changing the variable 'temperature' from a 'numeric' value to a 'string' value, and you can't compare the two. Only if they are the same type, ie both strings or both numeric. if your goal was just to make them 'whole' numbers, you could simply do this temperature = int(temperature)this keeps it a numeric value, and a whole number. |