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