-
Notifications
You must be signed in to change notification settings - Fork 0
/
MinutesOpen.py
25 lines (22 loc) · 1.13 KB
/
MinutesOpen.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/usr/bin/python
import pymysql.cursors
import urllib2
# Connect to the database (Change Variables to Match Your Instance)
connection = pymysql.connect(host='localhost',
user='root',
password='',
db='GarageDoor',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
# Calculates Time in Minutes Since the Last Time the Door Was Closed
try:
with connection.cursor() as cursor:
sql = "select TIMESTAMPDIFF(MINUTE,MAX(TS),iq.LastOpen) AS `MinutesOpen` from `DoorStatus` INNER JOIN (SELECT MAX(TS) as `LastOpen` FROM `DoorStatus` WHERE `Status` = 'Open') iq WHERE `Status` = 'Closed';"
cursor.execute(sql)
result = cursor.fetchone()
Minutes = int(result['MinutesOpen'])
finally:
connection.close()
# If the Door Was Open 10, 20, or 30 Minutes (or longer), Trigger IFTTT SMS Text Message via Webhook Request
if Minutes in (10,20,30,60,180,360,1440):
urllib2.urlopen('https://maker.ifttt.com/trigger/Garage_Left_Open/with/key/YOUR-IFTTT-INCOMING-WEBHOOK').read()