Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Number logic problem
#1
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.
Reply
#2
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?
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#3
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 "======================================================================="
Reply
#4
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
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#5
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.
Reply
#6
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)
Reply
#7
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
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#8
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#9
(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?
Reply
#10
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.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problem in formulating append logic shantanu97 1 1,035 Jun-07-2022, 08:35 AM
Last Post: ibreeden
  Problem with "Number List" problem on HackerRank Pnerd 5 2,122 Apr-12-2022, 12:25 AM
Last Post: Pnerd
  number accuracy problem? roym 5 1,848 Dec-24-2021, 07:57 AM
Last Post: roym
  Problem : Count the number of Duplicates NeedHelpPython 3 4,392 Dec-16-2021, 06:53 AM
Last Post: Gribouillis
  P3, openpyxl, csv to xlsx, cell is not number, problem with colorize genderbee 1 2,163 Sep-29-2020, 03:20 PM
Last Post: Larz60+
  problem with complex number jodrickcolina 1 2,331 Apr-13-2019, 06:59 PM
Last Post: Yoriz
  Problem with and if() logic nikos 2 2,025 Feb-11-2019, 10:14 PM
Last Post: nikos
  can't figure out problem with number guess Galoxys 4 3,354 Oct-29-2018, 01:45 PM
Last Post: snippsat
  Prime number Script Problem Codezters 4 4,067 Jul-21-2017, 03:07 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020