Limit program to send only one email
<p>I have a program to control lots of the things about my hen house including opening and closing the door at set times. Occasionally something happens and the door doesn't open or close and I have now got it to send an email when this happens. The problem is it will send 6 or more emails and I have been trying to work out how to limit it to send only one which is possible using while or if - but then I need to re-set it so that if happens again on another day it will send another email. This is the loop that I have</p>

<pre><code>def loop():
# retrieve current datetime
now = datetime.time(datetime.datetime.now().hour, datetime.datetime.now().minute)

# Open door on all days at the correct time
if ((now.hour == HOUR_ON.hour) and (now.minute == HOUR_ON.minute)):
  if (gpio.digitalRead(17) == 0):
    openplay()

# Close door on all days at the correct time
if ((now.hour == HOUR_OFF.hour) and (now.minute == HOUR_OFF.minute)):
  if (gpio.digitalRead(22) == 1):
    closeplay()

# check if door is open, 2 minutes after set time
if ((now.hour == HOUR_ON.hour) and (now.minute == HOUR_ON.minute + 120) and (now.second == 0) and (gpio.digitalRead(25) == 0)):
  # send email
  sendemail()

# check if door is closed, 2 minutes after set time
if ((now.hour == HOUR_OFF.hour) and (now.minute == HOUR_OFF.minute + 120) and (now.second == 0) and (gpio.digitalRead(25) == 1)):
  # send email
  sendemail()

# gives CPU some time before looping again
webiopi.sleep(1)
</code></pre>

<p>This is just a hobby and I put together things from mostly searching but can't crack this so would appreciate any help with it</p>

<p>Assuming <code>sendemail()</code> is a function you defined, you could rewrite it to store the time it last sent an email, and if it hasn't been long enough, don't send it.</p>

<p>The <code>now.minute == HOUR_ON.minute + 120</code> doesn't make sense to meâit looks like you're saying 2 hours, not 2 minutes, unless now.minute returns a value in seconds?</p>

<p>Another possibility: do you have multiple instances of this python program running at the same time? <code>pgrep python3</code> to see how many instances of python are running.</p>

python raspberry-pi