HOWTO: Number of [lights|motion sensors|...] on...

Posted on
Wed Jun 13, 2018 6:30 am
vtmikel offline
Posts: 316
Joined: Aug 31, 2012
Location: Boston, MA

HOWTO: Number of [lights|motion sensors|...] on...

You may find that if you want to have a SingleStat panel in Grafana to show the number of lights, motion sensors, or other category are currently on in your house. However, these aggregate queries are not an easy Influx query to write because of the nature of time series databases. I've found that the easiest solution is to create a Indigo variable for the number of lights and motion sensors that are on, and have a script within Indigo keep that variable value up to date.

I cannot see making this a plugin feature. But, I wanted to share my script in case anyone else was interested in doing this.

For lights, the script uses the Relay Indigo device type to find the lights in your house. Then, we filters out locks (which are relays) and virtual devices. You can choose to enable or disable the virtual device filter in the plugin. Fans are also these types, so I provided a way to filter out devices by name, which was the only method I could find easily.

For motion sensors, it's a bit more tricky. A lot of devices use the indigo "sensor" type. I use a "include" rather than exclude approach, and looked for names continuing "motion". Tweak this to work for you. This approach may not work perfectly for everyone, as we all name our devices differently.

There is a reference to a debug variable at the top of the script. This enables a verbose output to diagnose how the script is calculating the aggregates. You can either put a variable ID into that, so that you can easily turn debugging on and off, or statically put True / False for the script_debug.

Once you have it working, you can either:
  • Set a schedule for this to run every couple of minutes.
  • Use the Group Change Listener plugin (my preferred approach). Select all of the devices that would cause the aggregate count to change, and run the script when those changes occur. Be sure to set a small delay.

In both cases, I recommend hiding execution of the script once you have it working stable.

Code: Select all
script_debug = indigo.variables[<FILL IN HERE>].getValue(bool) # "grafana_aggregates_debug"
ignoreVirtualDevices = True

lights_on = indigo.variables[<FILL IN HERE>] # "lights_on"
motion_sensors_active = indigo.variables[<FILL IN HERE>] # "motion_sensors_active"

try:
   lights_on_oldValue = int(lights_on.value)
   motion_sensors_active_oldValue = int(motion_sensors_active.value)
except:
   lights_on_oldValue = -1
   motion_sensors_active_oldValue = -1

light_devices_exclude = ["fan", "bridge"]
sensor_devices_include = ["motion"]

def CountLights():
   lightOnCount = 0
   for device in indigo.devices.iter("indigo.relay, indigo.dimmer"):
      try:
         isLock = device.ownerProps.get("IsLockSubType", False)
         isVirtualDevice = device.pluginId == u"com.perceptiveautomation.indigoplugin.devicecollection"

         if device.onState and not isLock and (isVirtualDevice and not ignoreVirtualDevices or not isVirtualDevice) and not any(x.lower() in device.name.lower() for x in light_devices_exclude):
                  if script_debug:
                     indigo.server.log("light is on: " + device.name)
      
                  lightOnCount = lightOnCount + 1
      except:
         # nothing needed
         continue

   return lightOnCount

def CountSensors():
   sensorOnCount = 0
   for device in indigo.devices.iter("indigo.sensor"):
      try:
         if device.onState and any(x.lower() in device.name.lower() for x in sensor_devices_include):
                  if script_debug:
                     indigo.server.log("motion sensor is on: " + device.name)
      
                  sensorOnCount = sensorOnCount + 1
      except:
         # nothing needed
         continue

   return sensorOnCount

newLightCount = CountLights()
newSensorCount = CountSensors()

if newLightCount != lights_on_oldValue:
   if script_debug:
      indigo.server.log("Grafana aggregate update, lights on count: " + str(newLightCount))
   indigo.variable.updateValue(lights_on, str(newLightCount))

if newSensorCount != motion_sensors_active_oldValue:
   if script_debug:
      indigo.server.log("Grafana aggregate update, motion sensors on count: " + str(newSensorCount))
   indigo.variable.updateValue(motion_sensors_active, str(newSensorCount))
Attachments
Screen Shot 2018-06-13 at 8.25.23 AM.png
Screen Shot 2018-06-13 at 8.25.23 AM.png (959.98 KiB) Viewed 120 times
Screen Shot 2018-06-13 at 8.23.23 AM.png
Screen Shot 2018-06-13 at 8.23.23 AM.png (530.78 KiB) Viewed 120 times
Screen Shot 2018-06-13 at 8.17.34 AM.png
Screen Shot 2018-06-13 at 8.17.34 AM.png (134.15 KiB) Viewed 120 times

Posted on
Fri Jun 15, 2018 8:55 am
blysik offline
Posts: 179
Joined: Jan 06, 2015

Re: HOWTO: Number of [lights|motion sensors|...] on...

Very cool! I just set this up. Thanks for this.

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 0 guests

cron