dev.batteryLevel not available when value is 0 ?

Posted on
Fri Mar 03, 2017 2:34 am
PeteVis offline
Posts: 164
Joined: Jun 19, 2015

dev.batteryLevel not available when value is 0 ?

I'm using a small python script that I found on this forum that I run in a daily schedule to check the reported battery values off all my battery operated devices.
When the reported value goes below a set value (20% in my case) it sends me a pushover notification.

I've tested it, and it works.

Today however, I noticed something odd.
I have door sensor that has dropped it's battery value from 25% to 0%.
I know this is normal for zwave devices and that the reported value is not very accurate, but what is strange, is that my python script does not seem to pick up the batteryLevel at all when it is at 0

Code: Select all
batteryMessage = (u'%s battery level: %s' % (dev.name, dev.batteryLevel))
indigo.server.log(batteryMessage)


The above does not work when this device has a reported batteryLevel of zero.
It did work perfectly when there was some battery left, but apparantly not when the level is 0.

It looks like the batteryLevel property does not exist when it is 0
I have a second identical device with still some juice left, and the above command does report a batteryLevel.


Is there another command in python I should use to catch the devices that have a reported batteryLevel of 0 ?

Posted on
Fri Mar 03, 2017 3:21 am
autolog offline
Posts: 3008
Joined: Sep 10, 2013
Location: West Sussex, UK [GMT+1]

Re: dev.batteryLevel not available when value is 0 ?

I have tested your two lines of code against a device with a battery level of 0% and it worksfor me:
Script Stella-Z04 battery level: 0


I suspect the issue might be with your code surrounding these two statements (e.g. where you are checking battery level?) - maybe you could post a larger chunk to see if we can help you better. :)

Posted on
Fri Mar 03, 2017 3:33 am
PeteVis offline
Posts: 164
Joined: Jun 19, 2015

Re: dev.batteryLevel not available when value is 0 ?

Hi Jon,

Actually the code is very simple :

Code: Select all
targetLevel = 20

for dev in indigo.devices.itervalues():
   indigo.server.log(u'Device name : %s' % (dev.name))
   if dev.batteryLevel:
      indigo.server.log(u' - Battery level : %s' % (dev.batteryLevel))
      if dev.batteryLevel < targetLevel:
         batteryMessage = (u'%s battery level low : %s' % (dev.name, dev.batteryLevel))
         indigo.server.log(batteryMessage)


It loops over all my devices and it does display the batteryLevel for identical devices. Just not this one where it's batteryLevel is 0.
However, since it works with your device, this is maybe device dependant...

I did try to trigger a status update for this device which resulted in a "no ack" state (which I think is normal since the battery was 0).
I have now replaced the battery and again it shows up in the log with my script.

I'm a little confused why it works for your device and not mine...

Posted on
Fri Mar 03, 2017 4:17 am
autolog offline
Posts: 3008
Joined: Sep 10, 2013
Location: West Sussex, UK [GMT+1]

Re: dev.batteryLevel not available when value is 0 ?

Try replacing the line:
Code: Select all
   if dev.batteryLevel:
with
Code: Select all
   if dev.batteryLevel != None:
See if that helps. :)

Posted on
Fri Mar 03, 2017 6:56 am
krissh offline
Posts: 104
Joined: Nov 18, 2012
Location: Norway

Re: dev.batteryLevel not available when value is 0 ?

The reason why the battery level drops from 25% to 0% is most likely due to the way Indigo handles low battery notification from devices, not due to the device itself. It was discussed here

Posted on
Fri Mar 03, 2017 7:15 am
PeteVis offline
Posts: 164
Joined: Jun 19, 2015

Re: dev.batteryLevel not available when value is 0 ?

That worked perfectly, thanks !

krissh, thanks for that link, it seems this is related to what I was seeing.

Posted on
Fri Mar 03, 2017 7:23 am
DaveL17 offline
User avatar
Posts: 5102
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: dev.batteryLevel not available when value is 0 ?

Re: Jon's suggestion: Indigo API 1.9 added batteryLevel attribute to device instances to return the current battery level (integer, 0 to 100) for devices that report their battery level. Devices that do not report battery level will return the None object.

I'm not smart enough to explain why, but Python evaluates zero as False. Consider this:
Code: Select all
x = 0

if x:
    print('A')
   
if not x:
    print('B')

The result is 'B'.

So
Code: Select all
   if dev.batteryLevel:

is missing the devices with the battery level of zero because they are False. In this instance, devices that don't report battery level and devices that report a battery level of zero are effectively the same thing. :D

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Fri Mar 03, 2017 7:35 am
PeteVis offline
Posts: 164
Joined: Jun 19, 2015

Re: dev.batteryLevel not available when value is 0 ?

Thanks for explaining that.
It's a little confusing that values or properties can also act as an operator at the same time... I'm not used to that. I'll have to keep that in mind.

Posted on
Fri Mar 03, 2017 7:49 am
DaveL17 offline
User avatar
Posts: 5102
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: dev.batteryLevel not available when value is 0 ?

Sure thing.

I'm sure you get it, but False and None are not the same thing. They're just both being evaluated by 'if dev.batteryLevel' as False. Consider:

Code: Select all
x = False
y = None

print(x == y)

is False.

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Fri Mar 03, 2017 9:59 am
jay (support) offline
Site Admin
User avatar
Posts: 15880
Joined: Mar 19, 2008
Location: Austin, Texas

Re: dev.batteryLevel not available when value is 0 ?

PeteVis wrote:
It's a little confusing that values or properties can also act as an operator at the same time...


Not an operator exactly, but rather the fact that pretty much everything in Python is an object. The if statement is evaluating the truthfulness of the object when there is no operator and only a single object. Check out the __nonzero__ method description for the object class - it pretty clearly explains how objects are evaluated for truth.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Sat Mar 04, 2017 2:04 am
PeteVis offline
Posts: 164
Joined: Jun 19, 2015

Re: dev.batteryLevel not available when value is 0 ?

Many thanks to all, I've certainly learned a few things :D

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 1 guest