more G-Labs products

Author Topic: Stucked with Z-Wave dimmer level set  (Read 4876 times)

February 28, 2015, 01:08:40 PM
Read 4876 times

Bounz

  • ***
  • Information
  • Full Member
  • Posts: 94
Hi Gene, hi everyone.
I'm trying to modify standard "Smart Lights" application so it changes the dimmer's level based on current time (at the day time I want bright light (100%) and at the night - dimmed to 40%).
I'm using Fibaro Dimmer (FGD-211) to control lamps.
The problem is that sometimes application code
Code: [Select]
mod.IterationDelay(0.4).Level = 40; works well and sometimes - not.

I tried to investigate the problem and found that sometimes ZWaveLib can't handle the message. Also I tried to turn light on (at 40%) through API (always works). Here are logs from console for both cases.
Control through application:
Code: [Select]
[18:55:11.269567] SPO < 01 0A 00 13 0F 03 20 01 28 05 0F E9
[18:55:11.274976] SPI > 01 09 00 04 00 12 03 60 0D 01 8F 18

ZWaveLib: bad checksum message 01 09 00 04 00 12 03 60 0D 01 8F 18

[18:55:11.329949] SPI > 01 09 00 04 00 12 03 60 0D 01 8F
[18:55:11.335782] SPO < 06
ZWaveLib UNHANDLED message: 01 09 00 04 00 12 03 60 0D 01 8F

Control through API:
Code: [Select]
[18:55:53.657281] SPO < 01 0A 00 13 0F 03 20 01 28 05 12 F4
[18:55:53.692231] SPI > 06 01 04 01 13 01 E8
[18:55:53.696771] SPO < 06
[18:55:53.746305] SPI > 01 05 00 13 12 00 FB
[18:55:53.752059] SPO < 06

Any suggestions what could be the root of problem?

My first thought was that the problem is in Fibaro Motion Sensor and the way it's reporting motion alert to controller. Sometimes there are zwave "unhandled message" reports too. Thats why I tried to delay Level set using ".IterationDelay(0.4)".

February 28, 2015, 02:21:26 PM
Reply #1

Gene

  • *****
  • Information
  • Administrator
  • Posts: 1472
  • Tangible is the future!
    • Yet Another Programmer
Code: [Select]
[18:55:11.269567] SPO < 01 0A 00 13 0F 03 20 01 28 05 0F E9
[18:55:11.274976] SPI > 01 09 00 04 00 12 03 60 0D 01 8F 18

My guess is that the command has been sent while another one was being received.
The problem is that ZWavePort.cs seem not to be parsing 0x18 (the last byte) as a separate message.
Infact, 0x18 is the "CANCEL" message that mostly indicates that the last command has been ignored and so that is should be resent.
This is also why ZWaveLib is reporting "bad checksum"... because of extra byte 0x18 at the end of the received message.

https://github.com/genielabs/HomeGenie/blob/master/MigFiles/SupportLibraries/ZWaveLib/ZWavePort.cs#L303

g.
« Last Edit: February 28, 2015, 02:23:13 PM by Gene »

February 28, 2015, 02:29:08 PM
Reply #2

Gene

  • *****
  • Information
  • Administrator
  • Posts: 1472
  • Tangible is the future!
    • Yet Another Programmer
To understand the reason why the 0x18 is not taken apart, you should put a break point/debuggin here:

https://github.com/genielabs/HomeGenie/blob/master/MigFiles/SupportLibraries/ZWaveLib/ZWavePort.cs#L275

Cheers,
g.

February 28, 2015, 06:00:38 PM
Reply #3

Bounz

  • ***
  • Information
  • Full Member
  • Posts: 94
Well, I looked more precisely at logs when motion is detected:
Code: [Select]
[19:40:55.474855] SPI > 01 0D 00 04 00 12 07 56 01 30 03 FF D1 CB 62
[19:40:55.479676] SPO < 06
ZWaveLib UNHANDLED message: 01 0D 00 04 00 12 07 56 01 30 03 FF D1 CB 62
[19:40:55.536919] SPI > 01 09 00 04 00 12 03 20 01 FF 3D
[19:40:55.542637] SPO < 06
[19:40:55.593388] SPI > 01 09 00 04 00 12 03 60 0D 01 8F
[19:40:55.607147] SPO < 06
2015-02-28T19:40:55.6196920+03:00 HomeAutomation.ZWave 18 ZWave Node ZWaveNode.Basic 255.0
2015-02-28T19:40:55.6469310+03:00 HomeAutomation.ZWave 18 ZWave Node Status.Level 1
# Array index is out of range.
  at ZWaveLib.Handlers.MultiInstance.GetEvent (ZWaveLib.ZWaveNode node, System.Byte[] message) [0x00000] in <filename unknown>:0
  at ZWaveLib.ZWaveNode.MessageRequestHandler (System.Byte[] receivedMessage) [0x00000] in <filename unknown>:0
  at ZWaveLib.Controller.ZwaveMessageReceived (System.Object sender, ZWaveLib.ZWaveMessageReceivedEventArgs args) [0x00000] in <filename unknown>:0
So the root of the problem may be in this unhandled messages and time took to proceed them.
Also, Gene, do you have any documentation on ZWave message format? I'm confused with headers (first 5 bytes), can't understand what does every byte mean.

February 28, 2015, 06:59:25 PM
Reply #4

Gene

  • *****
  • Information
  • Administrator
  • Posts: 1472
  • Tangible is the future!
    • Yet Another Programmer
Hi Bounz,

I've no documentation.
The unhandled message is because command class 0x56 is not implemented in HG.
https://github.com/genielabs/HomeGenie/blob/master/MigFiles/SupportLibraries/ZWaveLib/Enums.cs#L101

This is the meaning of first 5 bytes:
1) message header (https://github.com/genielabs/HomeGenie/blob/master/MigFiles/SupportLibraries/ZWaveLib/ZWaveMessage.cs#L31)
2) message length
3) message type (https://github.com/genielabs/HomeGenie/blob/master/MigFiles/SupportLibraries/ZWaveLib/ZWaveMessage.cs#L39)
4) **should be function**
5) **I don't know :)**

Cheers,
g.

February 28, 2015, 07:06:56 PM
Reply #5

Gene

  • *****
  • Information
  • Administrator
  • Posts: 1472
  • Tangible is the future!
    • Yet Another Programmer
Code: [Select]
[19:40:55.593388] SPI > 01 09 00 04 00 12 03 60 0D 01 8F
# Array index is out of range.
  at ZWaveLib.Handlers.MultiInstance.GetEvent (ZWaveLib.ZWaveNode node, System.Byte[] message) [0x00000] in <filename unknown>:0
  at ZWaveLib.ZWaveNode.MessageRequestHandler (System.Byte[] receivedMessage) [0x00000] in <filename unknown>:0
  at ZWaveLib.Controller.ZwaveMessageReceived (System.Object sender, ZWaveLib.ZWaveMessageReceivedEventArgs args) [0x00000] in <filename unknown>:0

this is because there must be something missing in the MultiInstance encapv2 decoding:

https://github.com/genielabs/HomeGenie/blob/master/MigFiles/SupportLibraries/ZWaveLib/Handlers/MultiInstance.cs#L49

g.

March 08, 2015, 07:34:25 AM
Reply #6

Bounz

  • ***
  • Information
  • Full Member
  • Posts: 94
Hi, Gene!
I have some success with implementing 0x56 (CRC_16_ENCAP) command class. It works and can extract encapsulated message (thanks for this discussion https://groups.google.com/forum/#!topic/openhab/xg_j2tyGwLc).
But also I have found out that current ZWaveLib architecture can't be used to easily process encapsulated messages, because CommandClass handlers relies on full message length with a hardcoded array indexes like this from the SensorBinary command class handler:
Code: [Select]
byte cmdType = message[8];
if (cmdType == (byte)Command.SensorBinaryReport)
{
    nodeEvent = new ZWaveEvent(node, EventParameter.Generic, message[9], 0);
}

You can take a look at the handler in my repository: https://github.com/Bounz/HomeGenie/blob/zwave-rework/MigFiles/SupportLibraries/ZWaveLib/Handlers/Crc16.cs
Do you agree that we have to rework ZWaveLib further?

March 08, 2015, 11:00:54 AM
Reply #7

Gene

  • *****
  • Information
  • Administrator
  • Posts: 1472
  • Tangible is the future!
    • Yet Another Programmer
Hi Bounz,

I agree, so feel free to modify zwavelib for best generic use. Just keep it as most as compatible with what is already working with it.

Cheers,
g.


March 09, 2015, 04:40:40 PM
Reply #8

Bounz

  • ***
  • Information
  • Full Member
  • Posts: 94
Ok, I'll work on ZWaveLib this week.

March 15, 2015, 05:34:17 PM
Reply #9

Bounz

  • ***
  • Information
  • Full Member
  • Posts: 94
So, here you can see some results: https://github.com/Bounz/HomeGenie/commit/4c91a38e6a23f5408e4961ba78ef10140896263b
It works. I'll test it a little bit more.

Also I've noticed, that IterationDelay is not working.
For example, I have following code in smart lights application:
Code: [Select]
Program.Notify("Smart Lights 2", module.Instance.Name + "<br>switched ON " + mod.Instance.Address + " " + mod.Instance.Name);
if(DateTime.Now.Hour > 6 && DateTime.Now.Hour < 23)
{
    mod.IterationDelay(1).Level = 100;                               
} else {
    mod.IterationDelay(1).Level = 40;                               
}
but in logs I see that there is no 1 second delay between notification and command:
Code: [Select]
2015-03-15T19:28:54.0680180+03:00 HomeAutomation.HomeGenie.Automation 1000 Automation Program Program.Notification {"Title":"Smart Lights 2","Message":"коридор<br>switched ON 15 Коридор (свет)"}
[19:28:54.073859] SPO < 01 0A 00 13 0F 03 26 01 63 05 1C B7

I'll try to investigate this problem too.

March 15, 2015, 10:43:15 PM
Reply #10

Gene

  • *****
  • Information
  • Administrator
  • Posts: 1472
  • Tangible is the future!
    • Yet Another Programmer
Hi Bounz,

your z-wave work looks great so far. I will be testing it soon so I can possibly give some suggestions if needed.
About the Iteration delay, this is meant to work with a selection of modules, it has no effect with one module only.
For example
Code: [Select]
// select all dimmers
var dimmers = Modules.OfDeviceType("Dimmer");
// set all dimmers level to 40 putting 1 second pause between each module command
dimmers
   .IterationDelay(1)
   .Level = 40;

Cheers,
g.

March 16, 2015, 12:51:19 PM
Reply #11

Gene

  • *****
  • Information
  • Administrator
  • Posts: 1472
  • Tangible is the future!
    • Yet Another Programmer
Hey bounz,

did a little testing and created two pull requests.
https://github.com/Bounz/HomeGenie/pulls
with your changes as-is we would lost the ability of creating the path of routed events in MIG/Interfaces/Automation/ZWave.cs:
https://github.com/genielabs/HomeGenie/blob/master/MigFiles/MIG/Interfaces/HomeAutomation/ZWave.cs#L1016
This is causing problems with querying values of a multichannel/multiinstance node from the module setup page:
https://github.com/genielabs/HomeGenie/blob/master/HomeGenie/Service/HomeGenieService.cs#L640
that expects multichannel/multiinstance events in the form : ZWaveNode.MultiInstance.<instance_number> .
See my comment to the pull request #2 as a possible solution:
https://github.com/Bounz/HomeGenie/pull/2

Cheers,
g.

March 16, 2015, 07:19:14 PM
Reply #12

Bounz

  • ***
  • Information
  • Full Member
  • Posts: 94
Hi, Genie!
Thank you for explaining .IterationDelay() and fixing offsets in MultiInstance and Association command classes.
About MultiInstance command class. Yes, I saw that additional node.RaiseUpdateParameterEvent() commands but have no chance to test it on a real device. Also I was looking on the openhab project and how they implemented ZWave protocol. It's interesting.
Anyway I'll try to implement nested ZWaveEvents for MultiInstance/MultiChannel calls.

Also I found out that my Fibaro FGMS-001 should not send MultiInstance commands as it doesn't support it at all (according to documentation and NIF)! But it sends it and sends completely wrong. This was the reason of a part of the problem described in the first message in this topic:
Code: [Select]
[18:55:11.329949] SPI > 01 09 00 04 00 12 03 60 0D 01 8F
[18:55:11.335782] SPO < 06
ZWaveLib UNHANDLED message: 01 09 00 04 00 12 03 60 0D 01 8F

March 16, 2015, 07:52:50 PM
Reply #13

Gene

  • *****
  • Information
  • Administrator
  • Posts: 1472
  • Tangible is the future!
    • Yet Another Programmer
Hi Bounz,

I will test both multichannel and multiinstance. Make a pull request to the z-wave-rework branch so I can merge and review/contribute to the new code.
I thought openHab was using open-zwave? Isn't it?

Cheers,
g.

March 17, 2015, 04:41:06 AM
Reply #14

Bounz

  • ***
  • Information
  • Full Member
  • Posts: 94
Implemented NestedEvent in ZWaveEvent class, made a pull request.

I don't know exactly about open-zwave roots of openhab's zwave implementation, but CRC16 command class has empty realization in open-zwave and working one in openhab. Also Java code is a little bit easier to understand that C++.