Advertisement
Guest User

Untitled

a guest
Feb 5th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1.  
  2. import sys
  3. import pymysql.cursors
  4. from datetime import timezone
  5.  
  6. entity_id = sys.argv[1]
  7.  
  8. mysql_host = '192.168.1.200'
  9. mysql_user = 'hass'
  10. mysql_pass = 'MySuperAwesomePassword'
  11. mysql_db = 'hassio_db'
  12.  
  13. def utc_to_local(utc_dt):
  14. return utc_dt.replace(tzinfo=timezone.utc).astimezone(tz=None)
  15.  
  16. db = pymysql.connect (host=mysql_host, user=mysql_user, passwd=mysql_pass, db=m$
  17. cursor = db.cursor(pymysql.cursors.DictCursor)
  18.  
  19. sql = """SELECT state,last_changed FROM states WHERE entity_id = '%s'
  20. ORDER BY created DESC LIMIT 100""" % (entity_id)
  21.  
  22. cursor.execute(sql)
  23. result = cursor.fetchall()
  24. db.close()
  25.  
  26. for idx, row in enumerate(result):
  27. state_act = row['state']
  28. date_act = row['last_changed']
  29. if idx == 0:
  30. state_last = state_act
  31. date_last = date_act
  32. if state_last != state_act:
  33. real_last_changed = date_last
  34. break
  35. else:
  36. state_last = state_act
  37. date_last = date_act
  38.  
  39. print(utc_to_local(real_last_changed).strftime('%Y-%m-%d %H:%M:%S'))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement