Waardering:
  • 0 stemmen - gemiddelde waardering is 0
  • 1
  • 2
  • 3
  • 4
  • 5
Domoticz voorbeeld II (puur python)
#1
Hieronder een 2e voorbeeld van een Python script voor Domoticz. 
Als extra zal er nog een cron job moeten worden aangemaakt.
Met als regel:

*/5  * * * * python /home/pi/domoticz/scripts/beeclear.py

Sneller laten updaten naar domoticz heeft voor de logging geen zin, omdat dit niet wordt opgeslagen door Domoticz. 


//////////////////Begin script//////////////////////////////////////////////////////

#! /usr/bin/python
# --------------------------------------------------
# PREPARATION & SETTINGS
# --------------------------------------------------
import urllib2
import urllib
import base64
import subprocess

domoticz_P1 = "44"    # IDX for virtual P1-meter
domoticz_GAS = "43"    # IDX for virtual GAS-meter

# --------------------------------------------------
# READING BEECLEAR
# --------------------------------------------------

host = '192.168.xxx.xxx'
username = 'beeclear'
password = 'energie'

class beeclear:
   def __init__( self, hostname, user, passwd ):
   self.hostname = hostname
   self.user = user
   self.passwd = passwd
   self.cookie = None;
   def connect( self ):
   post_args = urllib.urlencode( { 'username':  base64.b64encode(self.user), 'password': base64.b64encode(self.passwd) } )
   url = 'http://' + self.hostname + '/bc_login?' + post_args;
   req1 = urllib2.Request(url)
   response = urllib2.urlopen(req1)
   self.cookie = response.headers.get('Set-Cookie')
   def send( self, command ):
   url = 'http://' + self.hostname + '/' + command
   req = urllib2.Request(url)
   req.add_header('cookie', self.cookie)
   f = urllib2.urlopen(req)
   data = f.read()
   f.close
   return data


a = beeclear( host, username, password )
a.connect()


Str_beeclear = a.send ( 'bc_current' )
Str_beeclear = str(Str_beeclear)


# "d":1429103361, // d: date unix time (time of most recent meter telegram data)
# "ul":1061049, // ul: used low (KWH * 1000) (meter total used low)
# "uh":883337, // uh: used high (KWH * 1000) (meter total used high)
# "gl":123574, // gl: generated low (KWH * 1000) (meter total generated low)
# "gh":334551, // gh: generated high (KWH * 1000) (meter total generated high)
# "u":0.000, // u: current usage in KW (current active electricity usage)
# "g":1.934, // g: current generating in KW (current active electricity generating)
# "gas":1142889 } //gas: gas usage in cubicle meters m3 * 1000 (meter total used gas)

ul= Str_beeclear[Str_beeclear.find("ul")+4:Str_beeclear.find("uh")-2]
uh= Str_beeclear[Str_beeclear.find("uh")+4:Str_beeclear.find("gl")-2]
gl= Str_beeclear[Str_beeclear.find("gl")+4:Str_beeclear.find("gh")-2]
gh= Str_beeclear[Str_beeclear.find("gh")+4:Str_beeclear.find('"u"')-1]
u= Str_beeclear[Str_beeclear.find('"u"')+4:Str_beeclear.find('"g"')-1]
g= Str_beeclear[Str_beeclear.find('"g"')+4:Str_beeclear.find('"gas"')-1]
gas= Str_beeclear[Str_beeclear.find("gas")+22:Str_beeclear.find("time")-2]

# --------------------------------------------------
# CHANGE VARIABLES NAMES INTO DOMOTICZ LANGUAGE
# --------------------------------------------------

USAGE1 = ul
USAGE2 = uh
RETURN1 = gl
RETURN2 = gh
CONS = u
PROD = g
USAGE = gas 

# --------------------------------------------------
# START OF UPLOAD TO DOMOTICZ
# --------------------------------------------------

#update P1 in Domoticz
httpresponse = urllib.urlopen("http://localhost:8080/json.htm?type=command&param=udevice&idx=" + str(domoticz_P1) + "&nvalue=0&svalue=" + str(USAGE1) + ";" + str(USAGE2) + ";" + str(RETURN1) + ";" + str(RETURN2) + ";" + str(CONS) + ";" + str(PROD) )

#update Gas in Domoticz
httpresponse = urllib.urlopen("http://localhost:8080/json.htm?type=command&param=udevice&idx=" + str(domoticz_GAS) + "&nvalue=0&svalue=" + str(USAGE) )


//////////////////Einde script//////////////////////////////////////////////////////
Antwoord


Berichten in dit topic
Domoticz voorbeeld II (puur python) - door forumadmin - 03-03-2019, 10:32 AM
RE: Domoticz voorbeeld II (puur python) - door Poedel - 22-10-2020, 22:31 PM

Ga naar locatie: