Python Forum
calling strftime does not display current time
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
calling strftime does not display current time
#1
Hello,

I have a working automatic top off system for my fish tank with the code below. It works great and replenishes water successfully. The problem I'm having is that everytime my timelog() function is called the printed timestamp on the console displays the execution timestamp of the script. For example. If i run the script at 22:00:00. Lets assume water evaporates and triggers the float switch 2 hours later on the configured pull_up_down pin (22). The timelog() function is called in the process and the timestamp displayed is still 22:00:00. I started my script at 22:16:00 last night and after calling the function over 20x times due to evaporation I only see 22:16:00 in the console. Is there a way to call time to display current system time?




#!/usr/bin/python3
import RPi.GPIO as GPIO
import time
import schedule
from time import sleep

#DISABLE WARNINGS AND SET GPIO TO BCM MODE
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)


######################################
#relay channels
####################################

#ASSIGN RELAY CHANNELS TO BCM PINS
Relay_Ch1 = 26



#INITIALIZE RELAY CHANNELS

GPIO.setup(Relay_Ch1,GPIO.OUT)




####################################
#float switch: GPIO, 22
#LED for visual confirmation in testing: gpio, 18
####################################

GPIO.setup(22, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(18, GPIO.OUT)

#################################

timestamp = time.strftime("%H:%M:%S") + str('# ')

def timelog():
    print('Timestamp for action' + timestamp)


def floatTriggered(channel):
    if GPIO.input(22):
        print(timestamp + 'WATER LEVEL ADAQUATE - PUMP OFF')
        timelog()
        GPIO.output(18, 0)
        GPIO.output(Relay_Ch1,GPIO.HIGH)

    else:
        print(timestamp + 'WATER LEVEL LOW - PUMP ON')
        timelog()
        GPIO.output(18, 1)
        GPIO.output(Relay_Ch1,GPIO.LOW)
        sleep(3)


GPIO.output(Relay_Ch1,GPIO.HIGH)        
try:
    GPIO.add_event_detect(22, GPIO.BOTH, callback=floatTriggered, bouncetime=600)

    while True:
        sleep(0.1)

finally:
        GPIO.cleanup()
Reply
#2
You only ever read the time once (line 38). Why would you expect the value of the timestamp variable to change?
Reply
#3
Ahhhhhh.... I didn't think about that!!! OMG!!!!!

Thank you
My timestamp for action is now accurate!

21:49:37# WATER LEVEL ADAQUATE - PUMP OFF
Timestamp for action = 21:52:22#


(May-05-2020, 04:44 AM)ndc85430 Wrote: You only ever read the time once (line 38). Why would you expect the value of the timestamp variable to change?

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to display <IPython.core.display.HTML object>? pythopen 3 45,876 May-06-2023, 08:14 AM
Last Post: pramod08728
  Real time Detection and Display Gilush 0 1,781 Feb-05-2022, 08:28 PM
Last Post: Gilush
Information Unable to display joystick's value from Python onto display box MelfoyGray 2 2,211 Nov-11-2020, 02:23 AM
Last Post: MelfoyGray
  How to print the current time in color julio2000 3 2,727 Apr-16-2020, 10:21 AM
Last Post: julio2000
  Time SQL Statement & Display Total anelliaf 0 1,587 Feb-28-2020, 05:55 PM
Last Post: anelliaf
  How to Display Multiple Time Tables With While Loop ZQ12 2 2,153 Nov-10-2019, 04:15 AM
Last Post: ZQ12
  export file and display cmd prompt same time jacklee26 1 2,021 Jul-24-2019, 05:15 AM
Last Post: Larz60+
  Display 20 records at a time,data structure or loop pythonds 1 2,451 Mar-29-2018, 11:09 AM
Last Post: DeaD_EyE
  time. strftime jor 1 2,491 Mar-13-2018, 02:51 PM
Last Post: Larz60+
  looping through every strftime in HHMMSS format yatinydalvi 5 4,705 Sep-28-2017, 09:33 PM
Last Post: Yoda

Forum Jump:

User Panel Messages

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