Loops in Expressions


#1

1) Give a description of the problem
I have a collection of devices I want to loop through in an expression to write an attribute to log or piston state

Ladies and Fine Fellows,

Is there a function for looping through a collection inside of an Expression?
Imagine I have a device variable into which I have added multiple (variable number) Light Switches.
I would like to set the Piston State reporting the status of each of the Light Switches within the collection.

Is there syntax for a loop statement, where by I can I grab each device in the collection and access it’s switch property/attribute by index of the loop?


#2

Here’s an example… I use this for my wifipresence tiles.

Edit: I did not read that correctly, you wanted to do it inside an expression. I am not aware of any way to do that.


#3

Thanks for the prompt reply.

This does give me an idea through concatenation and interpolation I could do to achieve the goal. However, I am looking for a way to do a loop inside of an Expression, if I can find one.


#4

You can’t do it in an expression but there is a workaround.

define
device allLights = x y z n
device lightsOn
device lightsOff

IF
any of allLights are on
x2 Save matching devices to {lightsOn}
x2 Save non-matching devices to {lightsOff}
THEN
set piston state = “The “ {lightsOn} “ light’s are on and the “ {lightsOff} “ lights are off.”
end if

The great thing about the ‘Save matching / non-matching’ feature (found by clicking the cog when building an IF statement) is that it places commas and the word ‘and’ in the right place so the resulting sentence makes sense grammatically.

“The Kitchen, Living Room and Bedroom lights are on and the Garden, Porch and Sex Dungeon lights are off.”


#5

You could take this a step further with:

(count(lightsOn)==0?”All lights are off”:(count(lightsOff)==0?”All lights are on”:(count(lightsOn)==1?”The “ {lightsOn} “ light is on and the ” {lightsOff} “ lights are off”:(count(lightsOn)>2?”The “ {lightsOn} “ lights are on and the ” ((count(lightsOff)==1?” the {lightsOff} “ light is off” : “the {lightsOff} “ lights are off”) : “”)))))

Completely untested lol :point_up:


#6

I really hope the Sex Dungeon lights are RGBW’s… standard dimmable hue’s would be boring, lol


#7

Wondered if anyone would notice that hidden gem haha…

Not sure such a room could ever be boring!


#8

Right… :stuck_out_tongue_winking_eye:


#9

I don’t even have a hub, let alone a sex dungeon… but I certainly would love a dungeon if the wife allowed it!!

I think it would have fibre optic RBGW star scene lighting… to the drawing board!


#10

Thanks folks!