roblans / ZWave4Net

ZWave4Net is a .NET library that interfaces with the Aeotec / Aeon Labs Z-Stick.
MIT License
41 stars 34 forks source link

Get door sensor and battery status #56

Closed SteveGxr closed 3 years ago

SteveGxr commented 3 years ago

Hi

I'm really struggling getting the current door sensor status and the battery status. I've been at this a few hours now and the solution is alluding me. Essentially I want to loop through the attached devices and report back their current status and battery level at the click of a button.

From what I can gather I need to send a command to the sensor initially to "wake it up", however, I cannot figure out how to achieve this. I have found a "getBatteryStatus()" (sp?) method, however, when called it just reports back "Waiting for Activation".

The sensors are currently working fine and will raise an event when activated. I just cannot figure out how to tell if the sensor is open or closed.

Does anyone have any code snippets that my help me here ?

roblans commented 3 years ago

You can't just request the battery level. Sensors with a battery are asleep by default and wake up periodically. The same goes for the status of the sensor, if it changes then an event is sent.

Subscribe to the WakeUp.Changed event. This is fired when the sensor wakes up, and then query the battery level.

For the status of the sensor you need to subscribe to Alarm.Changed.

SteveGxr commented 3 years ago

Hi Rob

Yes, I managed to work my way through it yesterday afternoon. My problem was the wakeup interval on the door sensors was set at 12 hours so I was never seeing the wakeup event.

I ended up editing the wakeup period via openHAB and changing the value to 15mins (just for testing). This then enabled me to get the battery and device status reports. Code below on the off chance it helps someone else out in the future.

var wakeUp = node.GetCommandClass(); wakeUp.Changed += (_, e) => { LogMessage($"WakeUp report of Node {e.Report.Node:D3} changed to [{e.Report}]"); InitDoorSensor(node); };

    private static async Task InitDoorSensor(Node node)
    {
        var battery = node.GetCommandClass<Battery>();
        var batteryReport = await battery.Get();
        LogMessage($"Battery report of Node {node:D3} is [{batteryReport}]");

        var wakeUp = node.GetCommandClass<WakeUp>();
        var wakeUpReport = await wakeUp.GetInterval();
        LogMessage($"WakeUp report of Node {node:D3} is [{wakeUpReport}]");

        var basic = node.GetCommandClass<Basic>();
        var basicReport = await basic.Get();
        LogMessage($"BasicReport report of Node {node:D3} is [{basicReport}]");

        var alarm = node.GetCommandClass<Alarm>();
        var alarmReport = await alarm.Get();
        LogMessage($"AlarmReport report of Node {node:D3} is [{alarmReport}]");

    }

The problem I have is that - really I need to check the device status at the push of a button, as it is not looking like this is possible I will implement a state tracking object on the hosted service instead and let that keep a track of where all the sensors are at, checking and updating as necessary via the periodic polling. Should work OK.

Thanks for your help!