Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Number logic problem
#11
(Oct-23-2016, 08:39 PM)sparkz_alot Wrote: when you take a line like this

temperature = int(temperature)
this keeps it a numeric value, and a whole number.

LOL I'd just worked this out myself as I saw the email update come in ... NOW it's fully working as expected... 

if the "humi"  is between 56 and 64 it will do nothing and it should be off.  If the humidity goes hits 65 or higher then it comes on and stays on until it gets to 55 or lower.  

I think the real issue I had was that pesky string conversion.  I'm quite new to python and doing a lot of hacking about with it to make things work.  It's a learning curve.

Thanks very much folks .. I'll post the new fully updated script taking all your comments into it as well, especially about the inconsistent print statements, while they did work it as ugly to look at hah

Ok, here's the new full script and it's working as expected ...

#!/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:
		temp = '{:.0f}'.format(temperature)
	if humidity is not None:
		humi = int(humidity)
		
	if humi >= hithresh :
		print("Current time %s"  % now )
		print("Humidity is {} - too high. We can turn on the dehumidifier".format(humi))
		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 - {}".format(dehumidpwr))
		print("=======================================================================")
	elif humi <= lothresh:
		print("Current time %s"  % now )
		print("Humidity is {} - to low. We should turn off the dehumidifier".format(humi))
		print("Switching off 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 - {}".format(dehumidpwr))
		print("=======================================================================")
	else:
		print("Do nothing")
		print("Current time %s"  % now )
		print("Humidity is {}".format(humi))
		print("=======================================================================")
	
	sys.exit(0)
Reply
#12
(Oct-23-2016, 08:39 PM)sparkz_alot Wrote: this keeps it a numeric value, and a whole number.
round() can make more sense,to get temperature closer to what it is.
>>> n = 2.9999999
>>> int(n)
2
>>> round(n)
3
Reply
#13
While your cleaning up, you might want to move your 'hithresh', 'lothresh', and 'now' variables to after all your 'imports'.

Remember when you change the value or type of a variable, it changes it forever. So if at some future point you need that value again, it will be gone.  So as ichabod801 was saying, if you need to retain the original value, you should reassign to a new variable. For example

temperature = 65.1234
rnd_tempeture = round(temperature)    # See I read others comments :-D
This keeps the original float value of 'temperature' should you ever need it else where in your script

Also, take into account Yoriz's observation


Quote: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.

Good work sticking with it  Clap
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
#14
Thanks for the tips, I'll rework the script a bit  and put those changes in too.     This script is actually a standalone script that will eventually be merged into the actual datalogging script that runs every couple of mins and logs the output to charts on ThingSpeak https://thingspeak.com/channels/155954

I've got that part published on GitHub, but I will be reworking that a little to take into account some of the suggestions you guys made on this one.  Then, I'll merge the scripts into one for my own use .. there's no point having 2 scripts that run and check the sensors when I can just have one running .. it can do both, log the data and take action against the dehumidifier.

https://github.com/M1XZG/raspberry-pi-scripts-PUBLIC

Thanks again.. great info and I've already learned so much more.  I should have joined the forums sooner, my forehead was taking a serious beating on the wall trying to figure this out.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problem in formulating append logic shantanu97 1 1,032 Jun-07-2022, 08:35 AM
Last Post: ibreeden
  Problem with "Number List" problem on HackerRank Pnerd 5 2,094 Apr-12-2022, 12:25 AM
Last Post: Pnerd
  number accuracy problem? roym 5 1,840 Dec-24-2021, 07:57 AM
Last Post: roym
  Problem : Count the number of Duplicates NeedHelpPython 3 4,379 Dec-16-2021, 06:53 AM
Last Post: Gribouillis
  P3, openpyxl, csv to xlsx, cell is not number, problem with colorize genderbee 1 2,151 Sep-29-2020, 03:20 PM
Last Post: Larz60+
  problem with complex number jodrickcolina 1 2,326 Apr-13-2019, 06:59 PM
Last Post: Yoriz
  Problem with and if() logic nikos 2 2,018 Feb-11-2019, 10:14 PM
Last Post: nikos
  can't figure out problem with number guess Galoxys 4 3,348 Oct-29-2018, 01:45 PM
Last Post: snippsat
  Prime number Script Problem Codezters 4 4,058 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