Python Forum
Running an action only if time condition is met
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Running an action only if time condition is met
#1
Question 
Hi everyone! Newbie here, I am using the below script to generate a push notification when I get a delivery at home / someone walks into my garden, using input from some outdoor infrared beams.

This has been working really well with code running on a Raspberry Pi, but I wanted to expand the script to also switch on some smart lights. I am doing this with requests.post via IFTT, but am trying to find a solution to only action this command when it is dark, between sunset and sunrise.

I have done some searching and found Projects like Astral, but with my limited coding knowledge I am a little confused how I would use Astral to do this, so thought I might try to source current time via datetime, specify sunset and sunrise times manually and only action the post request if the event time falls between these times. Would really appreciate any pointers about how I can integrate this.

from pushbullet import Pushbullet
import RPi.GPIO as GPIO
import requests
from time import sleep
from gpiozero import Button
button = Button(2)
pb = Pushbullet("my_account_api")
pbv = Pushbullet("my_account_api_1")
print(pb.devices)
print(pbv.devices)

while True:
    if button.is_pressed:
        print("Detect")
        push = pb.push_note("IR Detect", "Beams Detect")
        push = pbv.push_note("IR Detect", "Beams Detect")
        requests.post('https://maker.ifttt.com/trigger/beamstrigger/json/with/key/xxxxx')
        sleep(21)

else:
    print("Released")
Reply
#2
I'd use a Photocell, rather than sunrise/sunset times, which would be far less complicated and possibly more reliable.
alexbca likes this post
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#3
Might look at a PE cell at a later point! I've got it working with this code - but it requires times to be entered manually.

from pushbullet import Pushbullet
import RPi.GPIO as GPIO
import requests
from time import sleep
from gpiozero import Button
from datetime import datetime

def lights():
    now = datetime.now()
    current_time = now.strftime("%H:%M:%S")
    start = '20:15:00'
    end = '7:00:00'
    if current_time > start and current_time < end:
	    requests.post('https://maker.ifttt.com/trigger/xxx/json/with/key/xxxxxxx')
	    requests.post('https://maker.ifttt.com/trigger/xxx/json/with/key/xxxxxxx')
	    requests.post('https://maker.ifttt.com/trigger/xxx/json/with/key/xxxxxxx')	

        
   

button = Button(2)
pb = Pushbullet("o.xxxxxxx")
pbv = Pushbullet("o.xxxxxxx")
print(pb.devices)
print(pbv.devices)

while True:
    if button.is_pressed:
        print("Trigger")
        push = pb.push_note("IR Detect", "Beams Detect")
        push = pbv.push_note("IR Detect", "Beams Detect")
        lights()
        
        sleep(21)

else:
    print("NoTrigger")
Reply
#4
(Oct-21-2022, 10:12 AM)alexbca Wrote: Might look at a PE cell at a later point! I've got it working with this code - but it requires times to be entered manually.

I've just remembered a website that I've not been to for years: https://www.heavens-above.com/

Under 'Astronomy', there's a link to 'sun'. Maybe you can use the sunrise/sunset data from there?

I don't see an API, but perhaps there's some way around that?

Just casing out thoughts here.
alexbca likes this post
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#5
Or you could calculate.

If you want to implment the algorithm:
https://math.stackexchange.com/questions...nset-times
gml.noaa.gov/grad/solcalc/solareqns.PDF

Or let someone else do the work:
https://pypi.org/project/suntime/
alexbca likes this post
Reply
#6
Thanks crew, will check these out 😊
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pixel color and action Sartre 4 2,105 Apr-13-2023, 03:26 AM
Last Post: Sartre
Question Running an action only between certain times alexbca 9 1,733 Mar-15-2023, 04:21 PM
Last Post: deanhystad
  Checkbox itens with a button to run action Woogmoog 3 957 Dec-19-2022, 11:54 AM
Last Post: Woogmoog
  If condition not met but still running CreeperBob16 4 1,965 Aug-28-2021, 08:49 PM
Last Post: snippsat
  Assistance with running a few lines of code at an EXACT time nethatar 5 3,270 Feb-24-2021, 10:43 PM
Last Post: nilamo
  else condition not called when if condition is false Sandz1286 10 5,913 Jun-05-2020, 05:01 PM
Last Post: ebolisa
  [HELP] Nested conditional? double condition followed by another condition. penahuse 25 8,014 Jun-01-2020, 06:00 PM
Last Post: penahuse
  running 2 loops at the same time julio2000 7 4,554 Mar-21-2020, 05:21 PM
Last Post: ndc85430
  Running multiple script at the same time LoganSulpizio 1 7,047 Dec-07-2019, 04:30 PM
Last Post: Clunk_Head
  Error when running the second time metro17 3 3,764 Aug-30-2019, 12:09 PM
Last Post: ThomasL

Forum Jump:

User Panel Messages

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