Launching JavaScript Code for sensor notifications

Discussions about Z-Way software and Z-Wave technology in general
Post Reply
eeEE74
Posts: 2
Joined: 09 Apr 2017 15:40

Launching JavaScript Code for sensor notifications

Post by eeEE74 » 09 Apr 2017 15:49

Hi all,

I am attempting to make my system such that whenever my Fibaro multisensor's burglar alarm functionality activates, I receive an email notifying me of the occurrence. I am using a javascript file to trigger a python script to send the email.

So I'm trying to use the 'LaunchJavaScript' application available in the smarthome interface. Initially, when I tried to launch the code, I would get an 'undefined' error message in the events page on the smarthome browser interface. So I've changed the code to as follows:

The javascript code:

Code: Select all

this.bindFunc1 = function(zwayName) {
	if(zwayName != "zway")
		return; //binding the default zway instance
	
	var devices = global.ZWave[zwayName].zwave.devices;
	//sensor specific code
	zway.devices[3].instances[0].commandClasses[48].data[1].level.bind(function(){
		if(this.value == true)
			system('/usr/bin/python /opt/z-way-server/automation/trigger.py');
	});
};

//Process all active bindings
if(global.ZWave) {
	global.ZWave().forEach(this.bindFunc1);
}
	
//and listen for future ones
global.controller.on("ZWave.register", this.bindFunc1);
and the python script:

Code: Select all

import time
import datetime

ts = time.time()
st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')

def send_email(user, pwd, recipient , subject, body):
    import smtplib

    gmail_user = user
    gmail_pwd = pwd
    FROM = user
    TO = recipient if type(recipient) is list else [recipient]SUBJECT = subject
    TEXT = body

    #Prepare actual message
    message = """\From: %s\nTo: %s\nSubject: %s\n\n%s
    """ % (FROM, ", ".join(TO), SUBJECT, TEXT)

    try:
        server = smtplib.SMTP("smtp.gmail.com", 587)
        server.ehlo()
        server.starttls()
        server.login(gmail_user, gmail_pwd)
        server.sendmail(FROM, TO, message)
        server.close()
        print 'successfully sent the email'
    except:
        print 'failed to send the email'

send_email('email_im_sending_from', 'password', 'email_im_sending_to', 'Intrusion detected', 'Intrusion detected at ' +st)

f = open('/opt/z-way-server/automation/intrusions.log', 'a')
f.write(st+"\n")
f.close()

Now, however, nothing happens. I will trigger the multisensor, but no action occurs, and I don't see anything on the z-way-server log either.

Any ideas as to what the fixes could be?

Sidenote: I can't get the IP:8083/ZWaveAPI or IP:8083/ZAutomation to work either. I get a Bad ZWaveAPI request error message, could that somehow be related?

Post Reply

Who is online

Users browsing this forum: No registered users and 7 guests