Python plugin: Battery Level for Z-Wave Devices

Python and python framework

Moderator: leecollings

Logread
Posts: 228
Joined: Sunday 28 August 2016 7:48
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: France
Contact:

Re: Python plugin: Battery Level for Z-Wave Devices

Post by Logread »

Egregius wrote:Just looked at the new devices, some are marked red, probably because they haven't updated in the last 720 minutes (sensor timeout in settings).
Didn't look at the code yet how often this is executed but maybe not a bad idea to update the sensors every x hours to avoid this?
59 minutes refresh interval in the code... precisely to avoid what you are seeing...
Your set up might be such that since I refactored the code yesterday to ignore some devices with "non-standard" (at least by my standards :D ) hash logic between device ID and zwave node id, some of these devices are now ignored by the polling and therefore no longer updated... you may want to delete these and restart to see if these are created again ?

I am under the impression that the plugin works well for most users but some complex setups like yours yield some impredictable results... since you are an advanced user I believe, can you take a look at the plugin code and debug info and see where things can be tweaked ? I am 99% sure this is the hash logic I described above that might not be backwards compatible with some systems that have been created a while ago, or something along these lines... Looking forward to your feedback.
User avatar
Egregius
Posts: 2589
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: Python plugin: Battery Level for Z-Wave Devices

Post by Egregius »

I'll try to look tomorrow and enable debug, but I don't know anything about Python...
Logread
Posts: 228
Joined: Sunday 28 August 2016 7:48
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: France
Contact:

Re: Python plugin: Battery Level for Z-Wave Devices

Post by Logread »

Egregius wrote:I'll try to look tomorrow and enable debug, but I don't know anything about Python...
I think I am starting to understand better the logic (or lack of...) between device IDs and zwave node "hashing"... there are many threads on this in the forum, some of which you posted in such as https://www.domoticz.com/forum/viewtopic.php?t=11894
I am going to try to figure how best to address this, since I clearly overlooked some of this, not having a multitude of devices myself... stay tuned, but if anyone has some ideas, you are welcome
Logread
Posts: 228
Joined: Sunday 28 August 2016 7:48
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: France
Contact:

Re: Python plugin: Battery Level for Z-Wave Devices

Post by Logread »

Egregius wrote:I'll try to look tomorrow and enable debug, but I don't know anything about Python...
I rewrote the "device ID to zwave node id" translation function, after looking at the Domoticz source code file "/hardware/ZWaveBase.cpp", hence I changed:

Code: Select all

                    nodeID = int(device["ID"][:-2], 16)
to:

Code: Select all

                    FullID = int(device["ID"], 16)
                    ID1 = (FullID & 0xFF000000) >> 24
                    ID2 = (FullID & 0x00FF0000) >> 16
                    ID3 = (FullID & 0x0000FF00) >> 8
                    ID4 = (FullID & 0x000000FF)
                    nodeID = (ID2 << 8) | ID3
                    if nodeID == 0:
                        nodeID = FullID
This seems to work fine on my test system.
@Egrerius and/or other volunteers, can you please update the plugin.py file with the attached and tell me if this solves the issues you are seeing on your system ?
plugin.py.txt
(11.17 KiB) Downloaded 95 times
(obviously rename the plugin.py.txt file to plugin.py and 'chmod +x plugin.py' as appropriate.
Thanks
User avatar
Egregius
Posts: 2589
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: Python plugin: Battery Level for Z-Wave Devices

Post by Egregius »

Great :D
I replaced plugin.py and restarted domoticz, the 16th device was added. In my case all devices that report battery levels are created.
I just need to look why the danfoss don't report that anymore, the status is not in OZWCP either so something went wrong with the inclusion.
Derik
Posts: 1602
Joined: Friday 18 October 2013 23:33
Target OS: Raspberry Pi / ODroid
Domoticz version: BETA
Location: Arnhem/Nijmegen Nederland
Contact:

Re: Python plugin: Battery Level for Z-Wave Devices

Post by Derik »

Is there a Wiki?
Xu4: Beta Extreme antenna RFXcomE,WU Fi Ping ip P1 Gen5 PVOutput Harmony HUE SolarmanPv OTG Winddelen Alive ESP Buienradar MySensors WOL Winddelen counting RPi: Beta SMAspot RFlinkTest Domoticz ...Different backups
Logread
Posts: 228
Joined: Sunday 28 August 2016 7:48
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: France
Contact:

Re: Python plugin: Battery Level for Z-Wave Devices

Post by Logread »

Derik wrote:Is there a Wiki?
documentation is for now only on Github at https://github.com/999LV/BatteryLevel/b ... moticz.pdf
But I'll create a wiki page soon and post link when done
Derik
Posts: 1602
Joined: Friday 18 October 2013 23:33
Target OS: Raspberry Pi / ODroid
Domoticz version: BETA
Location: Arnhem/Nijmegen Nederland
Contact:

Re: Python plugin: Battery Level for Z-Wave Devices

Post by Derik »

mmm try to test your script..

Only that Pdf is not working..'
Cannot copy paste... and so i get a lot of typo...



Edit:
Anyway....
Got this great extra option working!!!!
Great job!!

Thanks!!!

A few small things...
Perhaps change the name, little to long for the display..
ScreenShot246.jpg
ScreenShot246.jpg (65.12 KiB) Viewed 3249 times
I did the tric in the script.. only after a update...
Last edited by Derik on Monday 15 May 2017 20:00, edited 1 time in total.
Xu4: Beta Extreme antenna RFXcomE,WU Fi Ping ip P1 Gen5 PVOutput Harmony HUE SolarmanPv OTG Winddelen Alive ESP Buienradar MySensors WOL Winddelen counting RPi: Beta SMAspot RFlinkTest Domoticz ...Different backups
Logread
Posts: 228
Joined: Sunday 28 August 2016 7:48
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: France
Contact:

Re: Python plugin: Battery Level for Z-Wave Devices

Post by Logread »

Derik wrote:mmm try to test your script..

Only that Pdf is not working..'
Cannot copy paste... and so i get a lot of typo...
Documentation is now available also on the wiki at https://www.domoticz.com/wiki/Plugins/BatteryLevel.html
Viewing pdfs in Github is possible though...
Derik
Posts: 1602
Joined: Friday 18 October 2013 23:33
Target OS: Raspberry Pi / ODroid
Domoticz version: BETA
Location: Arnhem/Nijmegen Nederland
Contact:

Re: Python plugin: Battery Level for Z-Wave Devices

Post by Derik »

great..[ thanks for your time ]
Only your pictures are not visible.
And in the start of the script perhaps:

Code: Select all

/home/linaro/domoticz/plugins
In stat of plugins


And perhaps a time setpoint in the settings?
How often runs this script?
I hope i can set this time somewhere...
.py cost me a lot of memory witch i dont have :-(
Xu4: Beta Extreme antenna RFXcomE,WU Fi Ping ip P1 Gen5 PVOutput Harmony HUE SolarmanPv OTG Winddelen Alive ESP Buienradar MySensors WOL Winddelen counting RPi: Beta SMAspot RFlinkTest Domoticz ...Different backups
Derik
Posts: 1602
Joined: Friday 18 October 2013 23:33
Target OS: Raspberry Pi / ODroid
Domoticz version: BETA
Location: Arnhem/Nijmegen Nederland
Contact:

Re: Python plugin: Battery Level for Z-Wave Devices

Post by Derik »

Perhaps something strange:
ScreenShot247.jpg
ScreenShot247.jpg (20.87 KiB) Viewed 3208 times
Xu4: Beta Extreme antenna RFXcomE,WU Fi Ping ip P1 Gen5 PVOutput Harmony HUE SolarmanPv OTG Winddelen Alive ESP Buienradar MySensors WOL Winddelen counting RPi: Beta SMAspot RFlinkTest Domoticz ...Different backups
Logread
Posts: 228
Joined: Sunday 28 August 2016 7:48
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: France
Contact:

Re: Python plugin: Battery Level for Z-Wave Devices

Post by Logread »

Derik wrote:Perhaps something strange
Did you check in the settings/devices list to see if the same 104% battery level value appears ? The plugin gets the data from there (it would be better to get it directly from openzwave for sure, but could be for a future version if I find the time or help to understand how to install and use the openzwave-python API for this.
User avatar
Egregius
Posts: 2589
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: Python plugin: Battery Level for Z-Wave Devices

Post by Egregius »

If one could use /ozwcp/poll.xml would be grate.
Strange thing is that the XML isn't always the same... Sometimes all nodes, sometimes couple of nodes.
User avatar
LouiS22
Posts: 433
Joined: Friday 27 February 2015 13:21
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Budapest, Hungary
Contact:

Re: Python plugin: Battery Level for Z-Wave Devices

Post by LouiS22 »

Logread wrote:
Derik wrote:Perhaps something strange
Did you check in the settings/devices list to see if the same 104% battery level value appears ? The plugin gets the data from there (it would be better to get it directly from openzwave for sure, but could be for a future version if I find the time or help to understand how to install and use the openzwave-python API for this.
I've seen this on cheap chinese z-wave stuff, maybe a bad implementation of the battery reading routine.
Logread
Posts: 228
Joined: Sunday 28 August 2016 7:48
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: France
Contact:

Re: Python plugin: Battery Level for Z-Wave Devices

Post by Logread »

Egregius wrote:If one could use /ozwcp/poll.xml would be grate.
Strange thing is that the XML isn't always the same... Sometimes all nodes, sometimes couple of nodes.
I did not think of polling the openzwave status files... good idea, though the file that holds what we need for our purpose is I think actually "/Config/zwcfg_0xXXXXXXXX.xml" (where XXXXXXXX is the hex value of the house ID of the zwave controller)... That file seems to be refreshed at least on an hourly basis, but I do not know if the battery levels it shows are always more current that these exposed by Domoticz. Does anyknow ?
Derik
Posts: 1602
Joined: Friday 18 October 2013 23:33
Target OS: Raspberry Pi / ODroid
Domoticz version: BETA
Location: Arnhem/Nijmegen Nederland
Contact:

Re: Python plugin: Battery Level for Z-Wave Devices

Post by Derik »

Here my screendump..
Tha strange part is in Domoticz there is no battery level.
That is the reasen why i use this plugin
Screen Shot 05-17-17 at 12.00 PM.JPG
Screen Shot 05-17-17 at 12.00 PM.JPG (42.32 KiB) Viewed 3152 times
correct it is a China Zwave,,, coolcam.
Only i do have 2 or 3 battery devices that are not showing up there status.
Not after 2 days...:-)
Xu4: Beta Extreme antenna RFXcomE,WU Fi Ping ip P1 Gen5 PVOutput Harmony HUE SolarmanPv OTG Winddelen Alive ESP Buienradar MySensors WOL Winddelen counting RPi: Beta SMAspot RFlinkTest Domoticz ...Different backups
User avatar
Egregius
Posts: 2589
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: Python plugin: Battery Level for Z-Wave Devices

Post by Egregius »

/ozwcp/poll.xml is the XML that OZWCP uses to update the information on the page. Already learned that the XML contains only data since previous read.
Looking at the zwfcg xml could also be a good idea. I'll check if the values there correspond to what's in OZWCP.

edit:
indeed, they are in there:

Code: Select all

<CommandClass id="128" name="COMMAND_CLASS_BATTERY" version="1" request_flags="4" innif="true">
<Instance index="1" />
<Value type="byte" genre="user" instance="1" index="0" label="Battery Level" units="%" read_only="true" write_only="false" verify_changes="false" poll_intensity="0" min="0" max="255" value="55" />
</CommandClass>
User avatar
Egregius
Posts: 2589
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: Python plugin: Battery Level for Z-Wave Devices

Post by Egregius »

Great idea to search in the zwfcg xml ;)
Made my own script in php for this :D
Values will be stored in a MySQL database, wich is by default my preferred way of storing data. I like to keep domoticz as clean as possible. Will schedule the script to run at night.
Benefits of reading the XML is that we have the real data openzwave receives and only 1 battery level for each node.

Code: Select all

$db=new mysqli('server','user','passw','db');
if($db->connect_errno>0)die('Unable to connect to database [' . $db->connect_error . ']');
$date=strftime("%F",time);
$xml=json_decode(json_encode(simplexml_load_string(file_get_contents('/home/pi/domoticz/Config/zwcfg_0xe9238f6e.xml'),"SimpleXMLElement",LIBXML_NOCDATA)),TRUE);
foreach($xml['Node'] as $node){
    foreach($node['CommandClasses']['CommandClass'] as $cmd){
        if(isset($cmd['Value']['@attributes']['label'])){
            if($cmd['Value']['@attributes']['label']=='Battery Level'){
                $id=$node['@attributes']['id'];
                $name=$node['@attributes']['name'];
                $value=$cmd['Value']['@attributes']['value'];
                $query="INSERT IGNORE INTO `batterydevices` (`id`,`name`) VALUES ('$id','$name');";
                if(!$result=$db->query($query))die('There was an error running the query ['.$query .' - ' . $db->error . ']');
                $query="INSERT IGNORE INTO `battery` (`date`,`id`,`value`) VALUES ('$date','$id','$value');";
                if(!$result=$db->query($query))die('There was an error running the query ['.$query .' - ' . $db->error . ']');
            }
        }
    }
}
Logread
Posts: 228
Joined: Sunday 28 August 2016 7:48
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: France
Contact:

Re: Python plugin: Battery Level for Z-Wave Devices

Post by Logread »

Egregius wrote:Great idea to search in the zwfcg xml ;)
Made my own script in php for this :D
Values will be stored in a MySQL database, wich is by default my preferred way of storing data. I like to keep domoticz as clean as possible. Will schedule the script to run at night.
Benefits of reading the XML is that we have the real data openzwave receives and only 1 battery level for each node.
Great minds think alike I guess is what they say... I rewrote the python plugin along the same lines, in a more generic way, going directly after openzwave rather than the Domoticz API. Simpler, faster and more real-time probably. I'll post a beta for your testing soon. Testing on my own systems right now. I wish I had tought about that in the first time.
Logread
Posts: 228
Joined: Sunday 28 August 2016 7:48
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: France
Contact:

Re: Python plugin: Battery Level for Z-Wave Devices

Post by Logread »

I am making available today a new version of the BatteryLevel plugin (0.4.0) that MUST be installed ahead of the upcoming changes announced by @Dnpwwo (see https://www.domoticz.com/forum/viewtopi ... 65&t=17554).

This is a major rewrite of the plugin. Instead of using the Domoticz internal data to fetch zwave nodes battery levels, this version directly checks directly the status data from openwave (by reading the “domoticz/Config/zwcfg_0x????????.xml” file (where ???????? is the controller homeid).

The code is actually much simpler, more robust and the battery levels probably updated more reliably than in prior versions.

Note that the plugin will only no longer be able to fetch data from "remote" systems... only the local zwave controller will be polled (no more IP/Port parameters in the setup)

I added an option parameter for the zwave data polling interval. The default is 60 minutes.

Known limitations:
  • The node name in the openzwave is truncated at 15 characters max. If needed, you can manually edit the corresponding device name in the Domoticz interface.
  • It is possible that the node name you gave at the time of including a z-wave device in Domoticz is not properly registered in the openzwave configuration file. If this happens, you need to “refresh” the node(s) name(s) in the Setup/Hardware/OpenZWave USB/Nodes list and restart Domoticz.
I am updating the first post of the thread accordingly.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest