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>This seems to do it. </p>

<pre><code>def loop():
  global emailsent1
  global emailsent2

  # 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, 10 minutes after set time and send email if not already sent
  if ((now.hour == HOUR_ON.hour) and (now.minute == HOUR_ON.minute + 10) and (now.second == 0) and (gpio.digitalRead(25) == 1) and not emailsent1):
    # send email
    a = 'opened'
    sendemail(a)
    emailsent1 = True

  if ((now.hour == HOUR_ON.hour) and (now.minute == HOUR_ON.minute + 11) and emailsent1): # reset if email has been sent
emailsent1 = False

  # check if door is closed, 10 minutes after set time and send email if not already sent
  if ((now.hour == HOUR_OFF.hour) and (now.minute == HOUR_OFF.minute + 10) and (now.second == 0) and (gpio.digitalRead(25) == 0) and not emailsent2):
    # send email
    a = 'closed'
    sendemail(a)
    emailsent2 = True

    if ((now.hour == HOUR_OFF.hour) and (now.minute == HOUR_OFF.minute + 11) and emailsent2): # resset if email has been sent
    emailsent2 = False

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

<p>10 minutes after closing time it checks if door closed and sendmail(a) is called and emailsent1 is set to True - after a minute it is set to false again.</p>

<p>Do I need emailsent1 and emailsent2 as global?</p>

python raspberry-pi