Page 1 of 1

Using variables structures and arrays of variables...

PostPosted: Wed May 20, 2015 11:10 pm
by russg
Folks, A newbe question, but I'm trying to figure out how to use variable arrays (i,j) and variable structures.

I have a large number of leak sensors, each with many variables, some of which are changed by the device actions and some by code. I'd rather be able to loop through these using an array of structures than explicitly add and delete code for every new/removed sensor; that becomes a maintenance nightmare quickly.

Any sample would be greatly appreciated., along with any documentation references (couldn't find any). If it turns out it can NOT be done (hard to imagine), what are some work-arounds?

I'm just using pseudo code in the example, but here is a rough idea of my thoughts

DEFINE STRUCTURE strLeakDetector
.location as string
.state as string
.lastHeartBeat as dateTime
END STRUCTURE

DEFINE ARRAY aryLeakDetector strLeakDetector(24)

(set values for aryLeakDetector)....

And be able to use code that would not change with the addition or deletion of a device, such as:

FOR i = 1 to aryLeakDetector.count
IF aryLeakDetector(i).state = ON then
(set a leakDetectedFlag = TRUE)
EXIT FOR
END IF
(set leakDetectedFlag = FALSE)
END FOR

thanks - russg


Re: Using variables structures and arrays of variables...

PostPosted: Wed May 20, 2015 11:52 pm
by howartp
This can be done, but I'm nowhere near code at the moment.

In the loosest of pseudocode it is something like:

For each Dev in indigo.devices

If dev.type = sensor and dev.name contains 'leak' then

If dev.status = on then

Do-Something
Etc etc etc

----

The alternative, depending what you're wanting, might be to use a group trigger (user plugin) that fires when any group member changes to 'leak detected' state. Add your detectors to it when you add them to Indigo and just add them to the group. No coding needed.

(Note I still haven't read the guide for either the group plugin or the virtual device features of indigo, so it might not be right for you - I keep suggesting them without having read up on them)

Re: Using variables structures and arrays of variables...

PostPosted: Thu May 21, 2015 6:23 am
by kw123
try this:

create variable anyleak, then in an action script :
Code: Select all
indigo.variables.update("anyleak","no leak")
for dev in indigo.devices:
   if dev.name.find("leak") >-1:  ##
      if dev.states["???dontknow what you are looking for"] =="on":
### or if dev.onState =="On":  ## if it has that state
      indigo.variable.updateValue("anyLeak",dev.name+"   has detected a leak")

would set variable any leak to device name has a leak or no leak detected
Karl

Re: Using variables structures and arrays of variables...

PostPosted: Thu May 21, 2015 10:58 am
by jay (support)
Karl's script might be what you're looking to do.

You might also want to step back and describe the problem at a higher level - there are likely multiple ways to skin that particular cat and some might be easier and more effective than others.

Re: Using variables structures and arrays of variables...

PostPosted: Sun May 24, 2015 9:42 pm
by russg
Folks - thanks for all the support! I think you answered most of my questions.

I was trying to figure out how to create a single trigger for a changing number of similar devices (water detectors) without having to specifically create a alert/reset trigger for each device. (I'm going to have a lot of leak detectors)

Looping through the Devices class and enumerating the objects is perfect. I've been away from coding a bit and am new to Indigo and hadn't wrapped my head around the objects in Indigo being easily accessible to an external piece of code (residing within an Indigo object). Wow. That is just awesome.

I had also assumed I shouldn't do large chunks of sorting code for each event that comes through, deciding if I should trigger an action (lot's of overhead), instead relying on specific triggers on specific devices. But it looks like a light overhead. Again, wow.

Last piece is trying to figure out how to add 'properties' to a device (fields to the device DB?) in the device definition (such as phrases to speak, tones to play, lights to flash) that can be enumerated dynamically in a trigger. ie. how to pluck out user defined info for a particular leak detector when looping through that type of device. If nothing else, I can embed that in an XML fragment in the name field, or something.

I'm probably missing something there, too. My fault for not RTFM.

Thanks again!

Re: Using variables structures and arrays of variables...

PostPosted: Sun May 24, 2015 11:00 pm
by RogueProeliator
Last piece is trying to figure out how to add 'properties' to a device (fields to the device DB?) in the device definition (such as phrases to speak, tones to play, lights to flash) that can be enumerated dynamically in a trigger. ie. how to pluck out user defined info for a particular leak detector when looping through that type of device. If nothing else, I can embed that in an XML fragment in the name field, or something.

There is not a way directly against the objects, but what you are looking for is doable via the Global Property Manager plugin -- it is made specifically to store additional information (user-defined) against any device. Not exactly in the manual, a bit on the down low!

See the explanation thread here that describes the plugin as well as gives a download link within the file library:
http://forums.indigodomo.com/viewtopic.php?f=134&t=13299&p=91511&hilit=global+property#p91511

Adam

Re: Using variables structures and arrays of variables...

PostPosted: Tue May 26, 2015 10:27 am
by jay (support)
Have you checked out the Low Battery Script I posted? I think it might give you a leg up on your script since it solves a similar problem.