Matplotlib Plugin for Indigo 7 - Tips and Tricks

Posted on
Sun Nov 13, 2016 9:55 am
DaveL17 offline
User avatar
Posts: 4884
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Matplotlib Plugin for Indigo 7 - Tips and Tricks

Tip: Create a folder so that you have a place to keep all of your Matplotlib Plugin devices in one place:
Screen Shot 2016-11-13 at 9.39.14 AM.png
Screen Shot 2016-11-13 at 9.39.14 AM.png (130.66 KiB) Viewed 1456 times

Tip: Name all of your chart images with a common string like 'chart_' so that the files will appear together in the Control Page editor and in Finder:
Screen Shot 2016-11-13 at 9.42.45 AM.png
Screen Shot 2016-11-13 at 9.42.45 AM.png (25.93 KiB) Viewed 1456 times

Tip: Start by leaving the default settings in place until you get the hang of how the plugin works.

Tip: Create some data elements and then wait a while so that you have data to chart.

Tip: Make a small number of changes at a time until you know how the plugin will react.

Tip: Leave the Advanced Settings disabled until you know how the plugin works.

Tip: Be judicious with your choices:

My Illumination Chart:
chart_illumination.png
chart_illumination.png (17.41 KiB) Viewed 1456 times

My Hideously Ugly Illumination Chart (and that's only with two changes!):
chart_illumination.png
chart_illumination.png (19.92 KiB) Viewed 1456 times


TIP: To see the available device states for any device, you can run the following code segment in an Indigo scripting shell:
Code: Select all
indigo.server.log(unicode(indigo.devices[DEVICE_ID_GOES_HERE].states))

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

[My Plugins] - [My Forums]

Posted on
Wed Nov 23, 2016 1:36 pm
richo offline
Posts: 155
Joined: Nov 25, 2014
Location: Pomorskie, Poland

Re: Matplotlib Plugin for Indigo 7 - Tips and Tricks

Hi Dave,

could you also list "Source State" names for typical devices for those who are not very familiar wit device definitions in Python?

Ryszard

Posted on
Wed Nov 23, 2016 1:56 pm
DaveL17 offline
User avatar
Posts: 4884
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Matplotlib Plugin for Indigo 7 - Tips and Tricks

richo wrote:
Hi Dave,

could you also list "Source State" names for typical devices for those who are not very familiar wit device definitions in Python?

That would be an awful lot of choices! I've included a tip above to show how to print out a list of states for a device.

I have a feature request to allow the selection of an existing device and then automatically list available states.

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

[My Plugins] - [My Forums]

Posted on
Sun Nov 27, 2016 8:13 pm
DaveL17 offline
User avatar
Posts: 4884
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Matplotlib Plugin for Indigo 7 - Tips and Tricks

Moved to a new subforum.

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

[My Plugins] - [My Forums]

Posted on
Sun May 06, 2018 9:13 am
DaveL17 offline
User avatar
Posts: 4884
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Matplotlib Plugin for Indigo 7 - Tips and Tricks

I've added a new tutorial to show one way to create custom CSV data using Indigo Triggers to create custom data based on certain conditions. This may be useful for logging data in a more granular way for charting.

The matplotlib plugin wiki wrote:
The CSV Engine device works well for taking snapshots of data at routine intervals (I have mine set to log every 15 minutes). However, sometimes you may wish to have more granular logging to track the exact times that certain events take place or to log every change to a device. This is a fairly easy thing to do when you combine the power of Indigo Triggers and a little bit of simple Python programming. You do not need to know how to program Python to use this method. Note that there are other ways to track all changes to a device--including using Indigo's built-in SQL Logger plugin--the benefit of the following method is that you have a greater level of control over the conditions governing when the logging takes place.

Creating Custom CSV Data

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

[My Plugins] - [My Forums]

Posted on
Sun Jul 08, 2018 1:52 pm
johnpolasek offline
Posts: 866
Joined: Aug 05, 2011
Location: Aggieland, Texas

Re: Matplotlib Plugin for Indigo 7 - Tips and Tricks

When I tried using your script as defined to monitor my door switches and motion sensors, I got an error as follows
    Matplotlib Critical Error Security State: Check path to CSV file (could not convert string to float: true)
    Matplotlib Critical Error ValueError: ordinal must be >= 1

I modified the code to create numeric values (which also allows me to set up a single "state has any change" trigger to do both the up and down as follows...

Code: Select all
import os
import datetime
#This is where you put the data
data_file = "/Users/Jeff/Documents/indigoUtilities/DavePlot/ServerMotion.csv"

#This is the Heading you want in the plot
heading = "Server Motion"

#This finds the device (by copy/past indigo ID in case you change the name)
DevID = 1697872060
dev = indigo.devices[DevID]

#This translates whatever the device is doing into a standard 0/numeric value
DevState = '0'
#Get the device state name from and state name from the detail info on the device dialog
if (dev.states['onOffState']):
    DevState ='1.5'
else:
    DevState = '0'
if not os.path.isfile(data_file):
    with open(data_file, 'a') as outfile:
        outfile.write(u"timestamp, heading\n")

with open(data_file, 'a') as outfile:
    outfile.write(u"{0}, {1}\n".format(datetime.datetime.now(), DevState))


Which allows me to set a different value for each door/sensor to plot multiple ones on the same graph... any possibility of allowing more than 4 on a chart?

Posted on
Sun Jul 08, 2018 4:11 pm
DaveL17 offline
User avatar
Posts: 4884
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Matplotlib Plugin for Indigo 7 - Tips and Tricks

Thanks for reporting your experience. Later versions of the plugin should be able to account for text-based binary values like 'true' or 'on'. What version of the plugin are you using?

I'll add an increase in lines to the wish list. You're not the first to ask for that. :D

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

[My Plugins] - [My Forums]

Posted on
Mon Jul 09, 2018 6:06 am
johnpolasek offline
Posts: 866
Joined: Aug 05, 2011
Location: Aggieland, Texas

Re: Matplotlib Plugin for Indigo 7 - Tips and Tricks

One other thing; I also discovered that the code (yours and mine) has one more bug: the line
Code: Select all
 outfile.write(u"timestamp, heading\n")


should be
Code: Select all
 outfile.write(u"timestamp, {0}\n".heading)


to make it put the actual heading text rather than the word "heading" in the title line.

And I'll work on trying to figure out how to make the script clean the data older than whatever plot interval you are interested in.

Posted on
Mon Jul 09, 2018 6:22 am
DaveL17 offline
User avatar
Posts: 4884
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Matplotlib Plugin for Indigo 7 - Tips and Tricks

Thanks for the heads up on this. I've corrected the wiki.

And I'll work on trying to figure out how to make the script clean the data older than whatever plot interval you are interested in.

Thanks. This is something that's been on the list that I haven't gotten around to.

Dave

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

[My Plugins] - [My Forums]

Posted on
Mon Jul 09, 2018 4:20 pm
johnpolasek offline
Posts: 866
Joined: Aug 05, 2011
Location: Aggieland, Texas

Re: Matplotlib Plugin for Indigo 7 - Tips and Tricks

BTW I was running 2.03 which was not reporting updates when I checked the menu item periodically... I am now on 6.04.


And just got
    Matplotlib Critical Error [Outdoor weather] 'line1Legend'
    Matplotlib Critical Error [Security State] 'line1Legend'

Do you no longer permit spaces in the legend texts, or do I need to update the sizing parameters?

Posted on
Mon Jul 09, 2018 7:44 pm
DaveL17 offline
User avatar
Posts: 4884
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Matplotlib Plugin for Indigo 7 - Tips and Tricks

johnpolasek wrote:
BTW I was running 2.03 which was not reporting updates when I checked the menu item periodically... I am now on 6.04.


And just got
    Matplotlib Critical Error [Outdoor weather] 'line1Legend'
    Matplotlib Critical Error [Security State] 'line1Legend'

Do you no longer permit spaces in the legend texts, or do I need to update the sizing parameters?

I can't reproduce this error. I can add spaces to the legend label of a line chart on my end.

There is code to block the use of a legend and an X axis label (you can only do one or the other). I had to come to a compromise due to space constraints. Matplotlib can easily handle both elements, but not when the dimensions are constrained. I felt that users would rather have the chart image size be constant and sacrifice one or the other element. But I don't know whether that's related to the error you're seeing.

Can you please turn on debugging, refresh the charts, and post the steps that were taking place when the error is thrown?

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

[My Plugins] - [My Forums]

Posted on
Sun Jul 15, 2018 2:22 pm
johnpolasek offline
Posts: 866
Joined: Aug 05, 2011
Location: Aggieland, Texas

Re: Matplotlib Plugin for Indigo 7 - Tips and Tricks

I wasn't ever able to get the error to reappear... But I did take the time to do a "brute force" fix to the event logging code to cut the data to a specific time delta. I know that it can be cleaned up further, but as is, it does work
Code: Select all
#Get Python commands from libraries
import os
import datetime
from datetime import datetime, timedelta
import time
import shutil
import csv

################################################################################
#This finds the device (by copy/past indigo ID in case you change the name)
DevID = 1235288368 #Side Door Pi

#This is where you put the data
data_file = "/Users/Jeff/Documents/indigoUtilities/DavePlot/SideDoor.csv"

#This is a temporary file that will be deleted as part of the script execution
temp_File = "/Users/Jeff/Documents/indigoUtilities/DavePlot/SideDoorT.csv"

#This is the length of time you want to plot
TimeRange = timedelta(hours=48)

#This is the scale factor you want to place on the data
Scale = '1.2'
###############################################################################

#Get the device you are interested in
dev = indigo.devices[DevID]
heading = dev.name

#This translates whatever the device is doing into a standard numeric value
DevState = '0'
#Get the device state name from and state name from the detail info on the device dialog and set appropriately
if (dev.states['status'] == "ON"):
    DevState = Scale
else:
    DevState = '0'

#if the output file doesn't exist, create it and write the header.
if not os.path.isfile(data_file):
    with open(data_file, 'a') as outfile:
        outfile.write(u"timestamp, {0}\n".format(heading))

#   Open or reopen the file at the beginning this time since the previous open
#for append got closed when the "with" ended.
with open(data_file, 'r') as outfile:
    #Open a temporary file to hold the truncated data if necessary
    with open(temp_File,'w') as tempFile:
        bHeader=1
        bEdit=0
        csvIterate =csv.reader(outfile,delimiter=",")
        for line in csvIterate:
            #always copy the header
            if bHeader:
                tempFile.write(u"timestamp, {0}\n".format(heading))
                bHeader=0
            else:
                #test to see if the data is stale and don't copy anything outside the date range
                if (datetime.now() - TimeRange < datetime.strptime(line[0],"%Y-%m-%d %H:%M:%S.%f")):
                    tempFile.write(u"{0}, {1}\n".format(line[0],line[1]))
                else:
                    #stale data was found;  we will need to update the output file
                    bEdit=1
        #close the file after we finish reading it
        outfile.close
        if bEdit:
            #if we deleted any lines, update the data file by renaming the temp file over it
            shutil.move(temp_File, data_file)
        else:
            #close and delete the unneeded temp file
            tempFile.close
            os.remove(temp_File)

#and open the output file again for appending
with open(data_file, 'a') as outfile:
    outfile.write(u"{0}, {1}\n".format(datetime.now(), DevState))



Right now, I'm fighting with it not giving me the labels I set in the legend, but I'll hopefully be able to work that out on my own.

Posted on
Mon Jul 16, 2018 5:38 am
DaveL17 offline
User avatar
Posts: 4884
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Matplotlib Plugin for Indigo 7 - Tips and Tricks

johnpolasek wrote:
I wasn't ever able to get the error to reappear... But I did take the time to do a "brute force" fix to the event logging code to cut the data to a specific time delta. I know that it can be cleaned up further, but as is, it does work

[snip]

Right now, I'm fighting with it not giving me the labels I set in the legend, but I'll hopefully be able to work that out on my own.

Thanks for reporting back. Glad you found a solution that will work for you. There's a major refactoring on this plugin somewhere down the line that will include built-in limits to address the stale data issue. I have a couple other irons in the fire to hammer out first, though. Let me know if you can't figure out the label issue, and we can try to work that out.

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

[My Plugins] - [My Forums]

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 0 guests