Count Devices On

Posted on
Sun Sep 03, 2017 7:23 am
Sharek326 offline
User avatar
Posts: 377
Joined: Jul 20, 2014
Location: Lansford, PA

Count Devices On

Does anyone know if there is a way to count how many devices are in an on state to be displayed on a control page.

Posted on
Sun Sep 03, 2017 9:38 am
jay (support) offline
Site Admin
User avatar
Posts: 18199
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Count Devices On

There isn't a built-in way. You'd need to write a script that periodically cycles through all devices that support on/off and count the number that are on then stick that into a variable. Alternately you could write a plugin that subscribes to device events and keeps a running total of devices that are on.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Sun Sep 03, 2017 9:43 am
kw123 offline
User avatar
Posts: 8333
Joined: May 12, 2013
Location: Dallas, TX

Count Devices On

This one should do write to log file and create 2 variables .
you need to define what "UP" is: which state and what the up value should be.

Code: Select all
statesToCheck = ["onOffState","status","up"]

valuesForUp = ["up","on","true","100"]


countUp = 0
countTotal = 0
for dev in indigo.devices:
   countTotal +=1
   for ss in statesToCheck:
      if ss in dev.states:
         indigo.server.log(dev.name.ljust(30)+"  "+ss.ljust(20)+"  "+unicode(dev.states[ss]))
         if unicode(dev.states[ss]).lower() in  valuesForUp:
            countUp+=1
            break
indigo.server.log("devices total= "+str(countTotal) )
indigo.server.log("        up=    "+str(countUp) )

try:
   var = indigo.variables["numberOfUpDevices"]
except:
   indigo.variable.create("numberOfUpDevices")
try:
   var = indigo.variables["numberOfDevices"]
except:
   indigo.variable.create("numberOfDevices")

indigo.variable.updateValue("numberOfUpDevices", str(countUp))
indigo.variable.updateValue("numberOfDevices", str(countTotal))


Put it into an action/server/script of a schedule to execute on a regular basis.

Karl

updated the code
Last edited by kw123 on Mon Sep 04, 2017 10:38 am, edited 1 time in total.

Posted on
Sun Sep 03, 2017 12:59 pm
Sharek326 offline
User avatar
Posts: 377
Joined: Jul 20, 2014
Location: Lansford, PA

Re: Count Devices On

Thanks Karl ...I'll give that a shot

Posted on
Mon Sep 04, 2017 10:40 am
kw123 offline
User avatar
Posts: 8333
Joined: May 12, 2013
Location: Dallas, TX

Re: Count Devices On

updated the code to include "onOffState"
and added a line that print every device to log file while it goes through.. good for testing, should be removed if you use it regularly

Karl

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 2 guests