Python script to store Youless values in mysql database
Posted: Saturday 25 March 2017 13:23
Youless nor domoticz save high resolution data. Therefore I created this python script to extract the values from a Domoticz Json to store them in a mysql database. With a cronjob the script is triggered every two minutes.
On this forum I could not find a ready to use example which i could just implement, so it took me some time to get it working.
============== File insertz.py ======================================
============== Cron job - e ======================================
On this forum I could not find a ready to use example which i could just implement, so it took me some time to get it working.
============== File insertz.py ======================================
Code: Select all
#!/usr/bin/python
# -*- coding: utf-8 -*-
import MySQLdb as mdb
idx = 11
con = mdb.connect('localhost', 'user', 'password', 'domoticz');
import simplejson
import urllib2
response = urllib2.urlopen("http://192.168.1.66:8080/json.htm?type=devices&rid=65").read()
result = simplejson.loads(response)
ActTime = result.get('ActTime')
ServerTime = result.get('ServerTime')
Counter = result['result'][0]['Counter']
HardwareID = result['result'][0]['HardwareID']
LastUpdate = result['result'][0]['LastUpdate']
idx = result['result'][0]['idx']
str_a = Counter
c = float(str_a) + 0
with con:
cur = con.cursor()
cur.execute('''INSERT into youless (ActTime, ServerTime, Counter, HardwareID, LastUpdate, idx)
values (%s, %s, %s, %s, %s, %s)''',
(ActTime, ServerTime, c, HardwareID, LastUpdate, idx))
Code: Select all
*/2 * * * * /home/pi/scriptjes/insertz.py