Access ZWaveAPI through Python script on Raspberry

Discussions about RaZberry - Z-Wave board for Raspberry computer
danilo
Posts: 11
Joined: 16 Feb 2013 00:33

Access ZWaveAPI through Python script on Raspberry

Post by danilo » 19 Mar 2013 02:59

Hi,

I manage to access my Z-Wave Sensors through the ZWaveAPI with JSON commands from my Webbrowser.

Now I would like to log data with a Python Script on the Raspberry directly.

How do I access the ZWaveAPI from a Python Script on the Raspberry?

(E.g in the browser I write something like this http://IP_OF_RASPBERRY:8083/ZWaveAPI/Run/devices[2].instances[0].commandClasses[0x31].data.val.valueOf() . How would that line have to look if I want to read the same data in a Python script right on the Raspberry?)

Any hints or sample code is most appreciated

Regards,

danilo

Tiptop
Posts: 56
Joined: 01 Mar 2013 13:50

Easy... here is a simplistic example...

Post by Tiptop » 19 Mar 2013 02:59


import httplib

class ZWay:
def __init__(self, address):
self.address = address

def __performGetRequest(self, requestString):
response = None
try:
self.conn = httplib.HTTPConnection(self.address)
self.conn.request('GET', requestString)
res = self.conn.getresponse()
except:
print 'ERROR: __performGetRequest'
res = None
if res:
if res.status == 200:
response = res.read()
else:
print 'ERROR:', res.status, res.reason
return response

def getSensorBinary(self, nodeID):
v = None
if type(v) != None:
nodeID, instanceNr = nodeID.split(':')
response = self.__performGetRequest('/ZWaveAPI/Run/devices[%s].instances[%s].commandClasses.SensorBinary.data.level.valueOf()' % (nodeID, instanceNr))
if type(response) == str:
if response == 'true':
v = True
elif response == 'false':
v = False
return v


z = ZWay('rpi1:8083')

nodeID = '21:0' # node 21, instance 0
print('NodeID=%s -> value=%s' % (nodeID, z.getSensorBinary(nodeID)))


Greets,

Stefan

danilo
Posts: 11
Joined: 16 Feb 2013 00:33

Thanks

Post by danilo » 19 Mar 2013 02:59

Many Thanks Stefan, really appreciate your help.
danilo

rabing
Posts: 31
Joined: 08 Mar 2013 03:00

Catch an event

Post by rabing » 19 Mar 2013 02:59

Hi,
is there a way to catch an event (like a move detector) with a python script?

Lutz

User avatar
PoltoS
Posts: 5103
Joined: 26 Jan 2011 19:36

Two ways:

Post by PoltoS » 19 Mar 2013 02:59

Two ways:using polling from python (bad way)using JS callback with system callExample:zway.devices[X].instances[0].commandClasses.SwitchBinary.data.level.bind(function() {
system("python scrypt.py", this.value);
});This assumes SwitchBinary report is received on motion event.Make sure to have the first argument to system in .syscommands file (the list of allowed commands in the shell).Please consult bind() and system() description.

rabing
Posts: 31
Joined: 08 Mar 2013 03:00

OK, but where is the best

Post by rabing » 19 Mar 2013 02:59

OK, but where is the best place to put the JS bind() to make "z-way-server" call
it in case of an event? Looks like a new module in the "automation" dir would be
a good place?

User avatar
PoltoS
Posts: 5103
Joined: 26 Jan 2011 19:36

Yes, it is the right place,

Post by PoltoS » 19 Mar 2013 02:59

Yes, it is the right place, but we are changing these modules right now.

Please use temporary .js file and place it directly in config.xml for startup. later on you will need to incorporate it in our engine (if you will want to use it)

michap
Posts: 343
Joined: 26 Mar 2013 10:35
Contact:

About what API documentation

Post by michap » 19 Mar 2013 02:59

About what API documentation do you write? Can you give me a link?
Was using your suggestion - and get this error:

[2013-04-02 16:18:28.872] SETDATA devices.10.instances.0.commandClasses.48.data.level = True
Failed to execute command: System commands are not configured

(simple echo command was used)

Any idea?

User avatar
PoltoS
Posts: 5103
Joined: 26 Jan 2011 19:36

Forgot to mention: create

Post by PoltoS » 19 Mar 2013 02:59

Forgot to mention: create .syscommands file and put exact file name on each line to allow execution

and the syntaxt of the call is a nit different: system(script, param1, param2, ...)

rabing
Posts: 31
Joined: 08 Mar 2013 03:00

to make a long story short ...

Post by rabing » 19 Mar 2013 02:59

It would be really nice to have a short working example. What is needed to have the binary sensor
with ID: 5 commandClass: 0x30 call the the script "/opt/test.sh " in case of an event?

Post Reply