Python script to store Youless values in mysql database

Python and python framework

Moderator: leecollings

Post Reply
Amsterdam020
Posts: 55
Joined: Saturday 05 March 2016 21:42
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.5629
Contact:

Python script to store Youless values in mysql database

Post by Amsterdam020 »

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 ======================================

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))
============== Cron job - e ======================================

Code: Select all

*/2 * * * * /home/pi/scriptjes/insertz.py
Rp3, youlesss, rfxcom, philips hue, dsb1820
Post Reply

Who is online

Users browsing this forum: RNEE and 1 guest