Upgrading to zwave-js version 11

I've been converting my old flows over to version 11 of zwave.js. First of all I appreciate all the hard work that was put into this development. I figured most of what is needed in my code to make the conversion. One issue is really got me struggling, at least as far as trying to make it as simple as possible. Before when I would first start a flow I would always use a GET command to retrieve the initial value. The get command would go to the device node and then pass the information into the event filter (now the splitter) node. Now when I use the get command node it only retrieves the value and the splitter node cant do anything with the message coming from the device node. Below is an example of my old code;

This kept things simple, the function node just needed to determine which value to use, the current value or the new value (with the new splitter command it is now prevValue and newValue);

if (!isNaN(msg.payload.object.currentValue)) {
    flow.set("CurrentTemp", msg.payload.object.currentValue);
}
if (!isNaN(msg.payload.object.newValue)) {
    flow.set("CurrentTemp", msg.payload.object.newValue);
}

msg.payload = flow.get("CurrentTemp") + "°F";

node.status({ fill: "blue", shape: "ring", text: msg.payload });
return msg;

This all avoided the problem of waiting as long as 12 hours for a battery reading to update for the first time or until an change occurred to trigger an event.

I'm now trying to use the following injecting into the controller node to get all the global data

msg.payload = {
    cmd: {
        api: 'DRIVER',
        method: 'getValueDB'
    }
};
return msg;

This gets to be cumbersome to try and figure out what to extract out of the global message but it can be done. Would be a good feature if an option existed on the device node to just retrieve the current values for all variables on startup.
Does anyone know of a way to accomplish this? Maybe what I was doing before was just something I stumbled into and wasn't correct.

Welcome to the forums @ottsm.

I appreciate v11 changed things quite a bit, I needed to re-engineer a lot of the internals, as it got impossible to maintain.

The new architecture allows me to better keep up with new features when the underlying driver introduces them.

I have seen networks with 200+ nodes, that do initialisation routines, and they are always using the Global Value DB, so your switch to start using it, is the right one :+1:

So I will answer based on using getValueDB (there are other secret ways, but it means enabling a feature inside the Runtime Config Node, which might be overkill)

So...

getValueDB can be told to only pull back for certain nodes.

msg.payload = {
    cmd: {
        api: 'DRIVER',
        method: 'getValueDB',
        id:'START_UP'     /* so we can identify what this call was for */
    },
    cmdProperties: {
        args: [7, 5]      /* Nodes 7,5 */
    }
};

This gets us the below payload

  • You can, using the native Node RED switch node
    only carry on this routine if msg.payload.id is START_UP
{
    "id": "START_UP",
    "event": "API_RESPONSE",
    "requestedAPI": "DRIVER",
    "requestedMethod": "getValueDB",
    "eventSubject": "VALUE_DB",
    "timestamp": 1793255766700,
    "eventBody": [{...},{...},]
}

Where each object inside eventBody is

{
    "nodeId": 7,
    "nodeName": "MS.KT - DB521A636D77",
    "nodeLocation": "Kitchen",
    "values": [{
            "currentValue": false,
            "valueTimestamp": 1793257694032,
            "valueId": {
                "commandClassName": "Binary Sensor",
                "commandClass": 48,
                "endpoint": 0,
                "property": "Motion",
                "propertyName": "Motion"
            },
            "metadata": {
                "type": "boolean",
                "readable": true,
                "writeable": false,
                "label": "Sensor state (Motion)",
                "ccSpecific": {
                    "sensorType": 12
                },
                "stateful": true,
                "secret": false
            }
        }
    },
    {
        ...
    },
    {
        ...
    }]
}

So now, all you need to do is bend the the event body, and emulate a real native event
(so splitter can be tricked), we will do this using node.send inside the function node

On the bases the splitter has got entires for battery, temp, humidity

/* Only commandClass and property are needed here */
const VIDs = {
    BATT: { commandClass: 128, property: "level" },
    TMP: { commandClass: 49, property: "Air temperature" },
    HUM: { commandClass: 49, property: "Humidity" }
};

/* Match value IDs using commandClass and property */
const match = (source, target) =>
    source.valueId.commandClass === target.commandClass &&
    source.valueId.property === target.property;

/* Loop through nodes */
for (const zwnode of msg.payload.eventBody) {

    /* Send a simulated VALUE_UPDATED event for each required value */
    for (const vid of Object.values(VIDs)) {
        const value = zwnode.values.find(v => match(v, vid));

        // Skip if this node doesn't have the requested value
        if (!value) continue;

        const newMessage = {
            payload: {
                event: "VALUE_UPDATED",
                timestamp: msg.payload.timestamp,
                nodeId: zwnode.nodeId,
                nodeName: zwnode.nodeName,
                nodeLocation: zwnode.nodeLocation,
                eventBody: {
                    valueId: value.valueId,
                    newValue: value.currentValue,
                    prevValue: undefined
                }
            }
        };
        node.send(newMessage); // Send each message separately; don't return here
    }
}

You can add as many property identifiers as you need to VIDs, and split them using splitter on the other end.

In fact, this reduces the clutter, compared to using individual device nodes for the start up routine.

I hope this helps.

Marcus

Thanks for responding back and thanks for creating zwave-js. zwave-js has been a game changer for my home automation projects and has been the main reason I prefer zwave over zigbee.

I can fully understand the need to be able to keep something like this updated easily. So often I've seen really good applications eventually die off because of the effort it takes the developer to maintain them.

I actually started using debug to search in the global array for the initial value I was looking for. It was taking a little effort to dig through it manually but not impossible by any means. I was going to try and use a filter function to search the array based off the node ID to somehow automate finding the values, but with what you have just posted I'm going to see what that is like.

Other than this issue everything else should convert easily.

Once again, thanks.

Wow, what you have above worked perfectly, this is even easier than the original method and (as you said "cleaner"). I have a Honeywell zwave thermostat that has other parameters that will need initial values so I may add those to the VID list or just have it search the entire array. It's only done
on startup so its not a lot of processing time.

This is going to cleanup my thermostat logic a lot.

Thanks again.

Original thermostat code

Yup!

The Global Value DB is there to facilitate this kind of "warm-up" routine.

As in my example above, it's commonly used to mimic real-time events during start-up, allowing the existing plumbing that handles naturally occurring events to be reused.

I built the nodes as a general-purpose toolkit around Z-Wave, rather than designing them around any one particular requirement.

When adding entries to the splitter, matching is based on the following properties of the incoming event:

  • event is either VALUE_UPDATED, VALUE_ADDED or VALUE_NOTIFICATION
  • eventBody.valueId.commandClass
  • eventBody.valueId.property
  • eventBody.valueId.propertyKey (if the property has one)
  • eventBody.valueId.endpoint (if the property comes from a multi-channel device)
    • The entry must be added using Strict Endpoint Match.

Knowing this, you can effectively conjure up an event that looks just like a genuine real-time event, using data from the Global Value DB.

This means the existing splitter can process it without needing any special handling for start-up scenarios.

And just note : Whilst I would like to claim to have built ZWave JS,
I only built the Nodes around it. We are actually a small team.

We look after/maintain various components of the Zwave JS ecosystem.

Myself:

  • This set of Nodes
  • The official web interface of known "supported" devices (https://devices.zwave-js.io/)
  • A .NET C# wrapper for the Zwave JS Server

Daniel

  • The developer behind the ZWave JS GUI (An MQTT Variant)

And the brains of Zwave JS its self, AlCalzone

  • The crazy developer who built the Zwave JS Node JS Library

And thanks for encouragement :folded_hands: