Binding to Level Change in vDev

Discussions about Z-Way software and Z-Wave technology in general
Post Reply
markolbert
Posts: 64
Joined: 16 Oct 2014 06:09

Binding to Level Change in vDev

Post by markolbert »

Thanx to maros I finally managed to make some progress in my attempt to write a module which will transmit level-change events to a nodejs app I've written.

It turns out that in addition to writing a module you have to instantiate it through the ZWay web UI by adding an instance of the app to the server. This is obvious in hindsight, but I don't recall seeing any mention anywhere in the online docs about needing to do this.

So I now have a userModule which loads and initializes itself.

Unfortunately, it isn't recognizing level change events. Here's the code for init():

Code: Select all

UDPEventRaiser.prototype.init = function( config ) {
    // Next line is predefined by Z-Way
    UDPEventRaiser.super_.prototype.init.call( this, config );
    
    this.log( 'initializing UDPEventRaiser...' );

    var self = this;
    var underNook;

    if ( this.controller.devices ) {
        this.log( 'this.controller.devices defined' );

        this.controller.devices.each( function ( vDev ) {
            self.log( vDev.id );
        } );

        underNook = this.controller.devices.get( 'ZWayVDev_zway_16-0-38' );
        if ( underNook ) {
            self.log( 'found underNook' );

            underNook.on( 'change:metrics:level', function ( vDev ) {
                self.log( 'got underNook level change event' );

                var level = vDev.get( 'metrics:level' );
                if ( !level ) level = 'undefined';
                self.log( vDev.id + ' level changed to ' + level );
            } );

            self.log( 'set level change listener on underNook' );
        }
        else self.log( 'underNook is NOT defined' );
    }
    else this.log( 'this.controller.devices NOT defined' );
}
Apologies for all the log statements, but I needed to see the program execution, and I don't know how to set up a debug environment to walk through the code.

The problem appears to be in the underNook.on(... statement. The log shows that underNook is found, and that the call to underNook.on() completes. But no events are ever caught (i.e., the log statements inside the callback never get called).

Have I defined the event name wrong? There isn't any documentation I could find on event names, so I simply extended a generic example from the docs.
markolbert
Posts: 64
Joined: 16 Oct 2014 06:09

Re: Binding to Level Change in vDev

Post by markolbert »

Some additional info...

I noticed that maros' BaseModule, from which my UDPEventRaiser derives, already has some logic in it that attempts to capture level change events. I went back and looked in the log, though, and couldn't find any logged level change events after changing the level on one of my dimmers.

I thought maybe there's some conflict between what BaseModule is doing capturing level change events and my own attempt to capture level change events. So I modified UDPEventRaiser to derive from AutomationModule rather than BaseModule.

The revised call to bind to level change events is now:

Code: Select all

 this.controller.devices.on( 'change:metrics:level', this.levelChanged )
where levelChanged() is defined as:

Code: Select all

UDPEventRaiser.prototype.levelChanged = function (vDev) {
    this.log( 'level changed for device ' + vDev.id );
}
But, once again, nothing shows up in the log.

Has anyone been able to bind to level change events in a vDev? If so, how do you do it?
maros
Posts: 103
Joined: 05 Apr 2014 11:21

Re: Binding to Level Change in vDev

Post by maros »

Can't find anything wrong with your first example. Maybe try to increase the log-level to see what's going on (eg. if the level really gets changed) ?! However, as a general precaution, I'd also advise you to set up the event listeners a couple seconds after init. The reason for that is that during init not all virtual devices are there yet - this especially applies to devices that are being created by other automation modules (eg. weather forecast)

The second example should bind to change events of ALL devices. You will then need to do some event filtering based on the vDev in your callback code. In the second example there is a slight flaw: 'this' is not your UDPEventRaiser instance but a virtual device object, Make sure to bind the callback correctly, or declare it as a closure.
Z-Way 2.2.4 on Raspi B | My Zway GitHub repos | Amazon Wishlist to support module development
markolbert
Posts: 64
Joined: 16 Oct 2014 06:09

Re: Binding to Level Change in vDev

Post by markolbert »

maros wrote:Maybe try to increase the log-level to see what's going on (eg. if the level really gets changed)
I'm not sure how to do that. Is there a setting someplace in the UI which controls logging level?

Also, is there a core event I should watch for to see when all the virtual devices have been created?
markolbert
Posts: 64
Joined: 16 Oct 2014 06:09

Re: Binding to Level Change in vDev

Post by markolbert »

Never mind, I found the log level setting. However, it's already set at 0 ("debug") so I don't think I can make it any more verbose.
Post Reply