title: Raspberry Pi Home Monitor
body: <p>I have been banging my head on the wall way too long on this one. I have pieced together this program and can't get it going. It is supposed to takes input from pipins.conf to select which pins are input and what the name is of the pin. It would be ex. "2 door" next line "15 window" and so on. I am trying to take that file and make a 2d array with the text ex. [[2,door],[15,window]]. I have also done the same for the email addresses. The program should take that information and watch the pins that are input into the file then if the pin is triggered (basically pin to ground is closed and open circuit is open) the program is supposed to send an email with the pin name and state of the pin with date and time only on a state change then take that same information and append it to the log.txt file. Where am I going wrong here. I am VERY new to programming and basically need this dumb down for me to grasp at the moment.</p>
  
  <pre><code>#!/usr/bin/env python
  from gpiozero import Button
  from signal import pause
  import time
  from datetime import datetime
  import subprocess
  from string import Template
  
  SLEEP_TIME  = 0.01
  
  NOTIFY_LIST = []
  with open('email.conf','r') as notify:
    for word in notify.read().split():
      NOTIFY_LIST.append(word)
  
  ACTPINS = {}
  with open('pipins.conf','r') as my_file:
     for line in my_file:
       l_split = line.split()
       ACTPINS[int(l_split[0])] = l_split[1]
  
  def current_date (fmt="%a %d-%m-%Y @ %H:%M:%S"):
      return datetime.strftime(datetime.now(), fmt)
  
  NOTIFY_CMD = {pin: Template("""echo "$date $sensor $state" | mail -s "Pi: $sensor $state" $email""") for pin in ACTPINS}
  
  def notify (id, state, sensor_name):
      """Send each of the email addresses in NOTIFY_LIST a message"""
      for email in NOTIFY_LIST:
          shell_cmd = NOTIFY_CMD[id].substitute(date=current_date(),
                                    state=state, sensor=sensor_name, email=email)
          proc = subprocess.Popen(shell_cmd, shell=True,
                                  stdout=subprocess.PIPE, stderr=subprocess.PIPE)
          stdout_value, stderr_value = proc.communicate()
          with open('log.txt', 'a') as f:
            f.write('{}\n'.format(shell_cmd))
  
  def do_action(button):
    # function to send email etc will only run when opened or closed
    state = 'closed' if button.is_pressed else 'opened'
    notify(button.pin, state, ACTPINS[button.pin])
    print(state)
  
  buttons = {}
  for id in ACTPINS:
    buttons[id] = Button(id)
    buttons[id].when_pressed = do_action
    buttons[id].when_released = do_action
    print(id)
    print(buttons)
    print(ACTPINS)
    print(NOTIFY_LIST)
  
  pause()
  </code></pre>