Battery Charting Script

Posted on
Thu Apr 07, 2016 8:51 pm
DaveL17 offline
User avatar
Posts: 6289
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Battery Charting Script

I have been playing around with matplotlib as a substitute for Gnuplot and wrote a quick script to create a chart that shows the health of all of my battery-powered devices. The script automatically picks up all the devices--when a new device is added to the system, it will automatically be added to the chart (implementations with a significantly different number of devices will require playing with the chart scaling parameters.)

When a battery drops below 10 percent, its bar will automatically turn yellow and below 5 percent it will turn red (many parameters can be easily adjusted in the script) The resulting image is placed in the appropriate Indigo directory to make it available in the control page editor. The script completes quickly, but I'd suggest running it as a linked script (rather than embedded), and I believe all components are available natively in OS X. Note: to make it look its best on a control page, you need to browse to the image to note it's dimensions.

I'm pretty happy with the results so far. Enjoy!
Dave

EDIT: Updated to include:
- Device names sorted alphabetically
- Error trapping for imports, device information gathering, data parsing
- Karl's data point labels (look for setting: show_data_labels [True/False])

EDIT 2: Updated to include:
- moved the data labels inside the ends of the bars,
- moved all bars up to keep them from crowding the x axis,
- moved the y axis labels slightly to actually be centered (for 2 line labels),
- picks up devices with battery level of zero,
- adds a 'fontname' value to specify a choice.

EDIT 3:
- coerces a battery level of zero when device reports an empty string for its battery level.

EDIT 4:
- fixes error where coercions were improperly created as strings.

Confirmed to work under:
Python 2.6.8
Matplotlib 1.1.1
Indigo Server 6.1.7

To download the Python 2 version, visit the link below and select the Python 2 fork.

EDIT 5:
- Updates script to Python 3 to work with Indigo 2022.1.

Confirmed to work under:
Python 3.10.2
Matplotlib 3.5.1
Indigo Server 2022.1

To download the Python 3 version, visit the link below and select the Master fork.

battery_test.png
battery_test.png (28.1 KiB) Viewed 7273 times

You can download the script on GitHub.

[EDIT] Move script to GitHub.

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

[My Plugins] - [My Forums]

Posted on
Fri Apr 08, 2016 12:21 am
howartp offline
Posts: 4503
Joined: Jan 09, 2014
Location: West Yorkshire, UK

Re: Low Battery Charting Script

Nice.

What devices are those - my Fibaro motion sensors are rubbish at reporting battery. Your post has given me the idea to extrapolate battery life out over say 30 days so I can see the average decline over the period.

(I did get my first ever notification from my battery-alert email last night - but that was my Nissan Leaf, not motion sensors!)


Sent from my iPhone using Tapatalk

Posted on
Fri Apr 08, 2016 2:43 am
Asconasny offline
Posts: 161
Joined: Jan 16, 2015

Re: Low Battery Charting Script

Great
Worked right out of the box!

brilliant and thank you

regards
Asconasny

Posted on
Fri Apr 08, 2016 3:20 am
Shutter offline
Posts: 345
Joined: Mar 07, 2014
Location: London, UK

Re: Low Battery Charting Script

Dave,

Very nice and easy. Thanks for this.

As much as we all love Karl's INDIGOplotD, this was very simple to use and set up. Already modified a few settings - not that I know Python, but I can figure some things out.

Funny, just yesterday finished listening to the hometech.fm podcast, where they mention Indigo and others, and they had that mini rant about how lots of the DIY solutions love charts. There is a time and a place for a chart. This is one of them. :D

Simon

Posted on
Fri Apr 08, 2016 4:05 am
DaveL17 offline
User avatar
Posts: 6289
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Low Battery Charting Script

howartp wrote:
Nice.

What devices are those - my Fibaro motion sensors are rubbish at reporting battery. Your post has given me the idea to extrapolate battery life out over say 30 days so I can see the average decline over the period.

(I did get my first ever notification from my battery-alert email last night - but that was my Nissan Leaf, not motion sensors!)


Sent from my iPhone using Tapatalk

The motion sensors are Aeon units (the one in the attic is connected via USB to the mains, so it will always report 100%.)

Your post caused me to look at the chart with fresh eyes and now my OCD is forcing me to rename my devices so that they're more consistent.

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

[My Plugins] - [My Forums]

Posted on
Fri Apr 08, 2016 4:07 am
DaveL17 offline
User avatar
Posts: 6289
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Low Battery Charting Script

Asconasny wrote:
Great
Worked right out of the box!

brilliant and thank you

regards
Asconasny

My pleasure. Glad to hear that it worked for you. Keep an eye out for enhancements as I'm loathe to leave well enough alone.

Dave

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

[My Plugins] - [My Forums]

Posted on
Fri Apr 08, 2016 4:08 am
DaveL17 offline
User avatar
Posts: 6289
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Low Battery Charting Script

Shutter wrote:
Dave,

Very nice and easy. Thanks for this.

As much as we all love Karl's INDIGOplotD, this was very simple to use and set up. Already modified a few settings - not that I know Python, but I can figure some things out.

Funny, just yesterday finished listening to the hometech.fm podcast, where they mention Indigo and others, and they had that mini rant about how lots of the DIY solutions love charts. There is a time and a place for a chart. This is one of them. :D

Simon

You've got it brother, glad you like it.

I'm an analyst by trade and an economist by schooling and I can tell you this; there is ALWAYS room for a chart. :D

Dave

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

[My Plugins] - [My Forums]

Posted on
Fri Apr 08, 2016 7:16 am
ckeyes888 offline
Posts: 2342
Joined: Nov 26, 2009
Location: Kalispell, MT

Re: Low Battery Charting Script

Should this work with 10.6.8? I don't have Python 2.7 installed, which it appears to need.
Would it cause any issues with Indigo to install 2.7, Numby, Mathplotlib etc?

Thanks,

Carl

Posted on
Fri Apr 08, 2016 7:51 am
DaveL17 offline
User avatar
Posts: 6289
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Low Battery Charting Script

ckeyes888 wrote:
Should this work with 10.6.8? I don't have Python 2.7 installed, which it appears to need.
Would it cause any issues with Indigo to install 2.7, Numby, Mathplotlib etc?

Thanks,

Carl

Hi Carl - the script doesn't require Python 2.7 -- I'm running it under 2.6 within Indigo with no problems. I was under the impression Matplotlib and Numpy were available within the factory Python install that comes with OS X, but maybe they were added in later versions of the OS.

Try this:
From the Plugins menu within Indigo, open the Scripting Shell. At the prompt,
1. Type "import matplotlib" and hit enter.
2. Type "import numpy" and hit enter.
3. Type "exit()" and hit enter.

Then paste the results in a reply to this thread. You should get something that looks like the following (which will likely contain errors.)
Code: Select all
Python 2.6.8 (unknown, Mar  9 2014, 22:16:00)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)]
Connected to Indigo Server v6.1.7, api v1.20 (localuds:1176)
>>> import matplotlib
>>> import numpy
>>> exit()
exit requested: interactive shell shutdown
logout

[Process completed]

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

[My Plugins] - [My Forums]

Posted on
Fri Apr 08, 2016 11:11 am
kw123 offline
User avatar
Posts: 8090
Joined: May 12, 2013
Location: Dallas, TX

Re: Battery Charting Script

Matplotlib was added in OS X 10.8 I believe. You could install it yourself but then you run into problems with Python 2.5 which could be overcome by launching an external script. But then this gets really complicated.


Sent from my iPhone using Tapatalk

Posted on
Fri Apr 08, 2016 1:28 pm
kw123 offline
User avatar
Posts: 8090
Joined: May 12, 2013
Location: Dallas, TX

Re: Battery Charting Script

just a little teaser: adding this to the code after
plt.barh(y_axis, x_values, align='center', color=bar_colors, alpha=1.0, height=0.5, zorder=3)
Code: Select all
for ii in range(len(y_axis)):
   plt.annotate("%3d"%x_values[ii],xy=(x_values[ii],y_axis[ii]), xycoords='data',textcoords='data',color=font_color)

will add numbers at the end of the bars..

Matplotlib is VERY powerful!!

Karl

annotate has a lot of options and features:
http://matplotlib.org/users/annotations_intro.html
Attachments
Screen Shot 2016-04-08 at 2.25.50 PM.png
Screen Shot 2016-04-08 at 2.25.50 PM.png (61 KiB) Viewed 7452 times

Posted on
Fri Apr 08, 2016 1:30 pm
ckeyes888 offline
Posts: 2342
Joined: Nov 26, 2009
Location: Kalispell, MT

Re: Battery Charting Script

Here ya go.
Code: Select all
Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49)
[GCC 4.2.1 (Apple Inc. build 5646)]
Connected to Indigo Server v6.1.4, api v1.19 (localuds:1176)
>>> import mathplotlib
Traceback (most recent call last):
  File "<console>", line 1, in <module>
ImportError: No module named mathplotlib
>>> import numphy
Traceback (most recent call last):
  File "<console>", line 1, in <module>
ImportError: No module named numphy
>>> exit()
exit requested: interactive shell shutdown
logout

[Process completed]



Appreciate the help.

edit: Oops...had a typo..should have been numpy.

Carl

Posted on
Fri Apr 08, 2016 1:55 pm
kw123 offline
User avatar
Posts: 8090
Joined: May 12, 2013
Location: Dallas, TX

Re: Battery Charting Script

you need OS-X 10.9 - Mavericks,
or you need to install matplotlib and numpi yourself: https://pypi.python.org/pypi/matplotlib#downloads and that be difficult, a lot of dependencies on Py versions etc..

Posted on
Fri Apr 08, 2016 2:19 pm
DaveL17 offline
User avatar
Posts: 6289
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Battery Charting Script

kw123 wrote:
just a little teaser: adding this to the code after
plt.barh(y_axis, x_values, align='center', color=bar_colors, alpha=1.0, height=0.5, zorder=3)
Code: Select all
for ii in range(len(y_axis)):
   plt.annotate("%3d"%x_values[ii],xy=(x_values[ii],y_axis[ii]), xycoords='data',textcoords='data',color=font_color)

will add numbers at the end of the bars..

Matplotlib is VERY powerful!!

Karl

annotate has a lot of options and features:
http://matplotlib.org/users/annotations_intro.html

Thanks Karl!

That was on the to do list. Now I can check that one off!

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

[My Plugins] - [My Forums]

Posted on
Fri Apr 08, 2016 2:19 pm
DaveL17 offline
User avatar
Posts: 6289
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Battery Charting Script

ckeyes888 wrote:
Here ya go.
Code: Select all
Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49)
[GCC 4.2.1 (Apple Inc. build 5646)]
Connected to Indigo Server v6.1.4, api v1.19 (localuds:1176)
>>> import mathplotlib
Traceback (most recent call last):
  File "<console>", line 1, in <module>
ImportError: No module named mathplotlib
>>> import numphy
Traceback (most recent call last):
  File "<console>", line 1, in <module>
ImportError: No module named numphy
>>> exit()
exit requested: interactive shell shutdown
logout

[Process completed]



Appreciate the help.

edit: Oops...had a typo..should have been numpy.

Carl

You might be sunk on this one Carl. Sorry!

Dave

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

[My Plugins] - [My Forums]

Who is online

Users browsing this forum: No registered users and 1 guest