Include only certain devices

Posted on
Sun Sep 12, 2021 8:57 pm
Dual offline
Posts: 257
Joined: Feb 05, 2019

Include only certain devices

I have a python script to create an email to send me dev.lastSuccessfulComm and dev.batteryLevel for SOME of my devices. I don't want it to include all devices. My hack is to turn off dev.remoteDisplay for the devices I do not want to include. Is there another easy way to do this? I had thought about putting something in the comment and parsing it. My current way is easy but less than optimum.

Code: Select all
theEmailBody = "DEVICE UPDATE."

for dev in indigo.devices.itervalues():
   if dev.remoteDisplay:
      theEmailBody = theEmailBody + "\n" + str(dev.lastSuccessfulComm) + ",   " + str(dev.batteryLevel) + ",   " + dev.name

myVar = indigo.variables[586076314] # "EmailBody"
indigo.variable.updateValue(myVar, theEmailBody)


Posted on
Mon Sep 13, 2021 4:06 am
DaveL17 offline
User avatar
Posts: 6753
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Include only certain devices

There's several ways that you could attack this, but they're not necessarily any better than what you're already doing. The one bonus might be that you'd no longer be hiding devices from remote display.

  • As you said, you can add a tag to a device's notes field and search for that. I've done this for other purposes and it works well.
  • You could hard code the list of device ID's into your script. This will of course work, but I'd suggest it's bad practice (I still do it on occasion).
  • You could add the list of device ID's to a variable.
  • You could use the Global Property Manager plugin, and search for the custom property.
There are likely others that I haven't thought of.

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

[My Plugins] - [My Forums]

Posted on
Mon Sep 13, 2021 9:38 am
jay (support) offline
Site Admin
User avatar
Posts: 18219
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Include only certain devices

This is pretty much exactly the use case for the Global Property plugin... ;)

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Mon Sep 13, 2021 11:53 am
Dual offline
Posts: 257
Joined: Feb 05, 2019

Re: Include only certain devices

Thanks guys. I will look at the plugin.


Sent from my iPhone using Tapatalk

Posted on
Mon Jan 03, 2022 10:08 pm
Dual offline
Posts: 257
Joined: Feb 05, 2019

Re: Include only certain devices

I finally got around to trying this using the Global Property Manager Plugin. Some success. Some failure.

I have created a Global Property for 4 of my many devices to test this option.

Screen Shot 2022-01-03 at 8.37.12 PM.png
Screen Shot 2022-01-03 at 8.37.12 PM.png (45.73 KiB) Viewed 852 times


I have read Jay's post on the GPM Plugin. I have created a test using his code:

Code: Select all
# First, get the device
myDevice = indigo.devices[1740614719]

# Next, get the props - we use the ID for the server for this particular set of properties
theProps = myDevice.globalProps["com.indigodomo.indigoserver"]

# Finally, access the value of the property. Use the get method here so that you can later test for None in case the property doesn't exist on the device
myPropertyValue = theProps.get("emailBattery", None)

# print value to log
theLogStr = str(myPropertyValue)
indigo.server.log(theLogStr, isError=True)


This works fine. "True" is output to the log. Device 1740614719 is the device in the screenshot.

Then I try it again for another device where I have NOT created any global properties. The script compiles ok, but when I run it it fails and the log entry is:

Code: Select all
   Script Error                    embedded script: 'key com.indigodomo.indigoserver not found in dict'
   Script Error                    Exception Traceback (most recent call shown last):

     embedded script, line 5, at top level
KeyError: 'key com.indigodomo.indigoserver not found in dict'


It seems it fails when no global property exists for a device. My plan was to iterate through all devices.

Suggestions?

I may end up doing as Dave suggested ("You could hard code the list of device ID's into your script") since managing that list is no more onerous, and indeed likely less onerous, than creating a global property for each of the many dozens of devices I want to include in the email. I noted in the GPM post that there appears to be no way to easily create the same property for many or all devices. It has to be done over and over again for each device.

Cheers

John

Posted on
Tue Jan 04, 2022 7:37 am
autolog offline
Posts: 3988
Joined: Sep 10, 2013
Location: West Sussex, UK [GMT aka UTC]

Re: Include only certain devices

Try something like this:

Code: Select all
# First, get the device
myDevice = indigo.devices[1740614719]

# Check if Props exist before proceeding:
if "com.indigodomo.indigoserver" in myDevice.globalProps:

    # Next, get the props - we use the ID for the server for this particular set of properties
    theProps = myDevice.globalProps["com.indigodomo.indigoserver"]

    # Now check the particular prop exists before proceeding
    if "emailBattery" in theProps:

        # Finally, access the value of the property. Use the get method here so that you can later test for None in case the property doesn't exist on the device
        myPropertyValue = theProps.get("emailBattery", None)
 
        # print value to log
        theLogStr = str(myPropertyValue)
        indigo.server.log(theLogStr, isError=True)

Posted on
Tue Jan 04, 2022 1:04 pm
jay (support) offline
Site Admin
User avatar
Posts: 18219
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Include only certain devices

Code: Select all
# First, get the device
myDevice = indigo.devices[1740614719]

# Next, get the props - we use the ID for the server for this particular set of properties
try:
    theProps = myDevice.globalProps["com.indigodomo.indigoserver"]

    # Finally, access the value of the property. No need to use the get function since we want it to raise an exception if it's not there
    myPropertyValue = theProps["emailBattery"]

    # print value to log
    theLogStr = str(myPropertyValue)
    indigo.server.log(theLogStr, isError=True)
except:
    # No emailBattery prop for this device, skip to the next
    pass


This script exemplifies the Python philosophy of Easier to Ask for Forgiveness than Permission. Try stuff and catch the error later. In this case, since there are multiple possible exceptions, it's very clean - if you get any exception when trying to get the property value, then just move on.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 4 guests