Add a device to a collection variable


#1

Hiya,

I have a variable called @residents that contains a set of presence sensors. I want to connect an IFTTT recipe triggered by Google Assistant so I can say “Hey Google, activate guest key number 1” to add that key to the residents list.

Otherwise, spare keys that I have for guests will make webcore think that there is always someone at home :-).

This is what I have so far, but the logs show “Empty device list” for the value of $devices and the name of my location for the value of $currentEventDevice. I know I could do something like "Set variable @residents to be Key 1, but that would remove all the existing devices in the @residents collection. How do I add one?


#2

I’m assuming I need an expression to set variable @residents, such as:

@residents + $currentEventDevice

Oh but obviously $currentEventDevice is empty because it’s an IFTTT trigger, not a device trigger. So I just need to figure out how to address a device by name in an expression, something like:

@residents + devices("House keys #1")

#3

Whoa hold the PHONE. I just discovered:

@residents + [House keys #1]

Is there any way to use an expression for the device’s name? So could I do something like:

@residents + device("House keys  #" + $args.keyNumber)

#4

OK this is probably very rudimentary, but seems to work. I created two IFTTT recipes so the input includes the key number and an action which is whether to add or remove it. Any top tips?


#5

could you not simply have each spare key as a switch in ST, this switch is either on or off.

whenever a switch changes state the piston runs.

If switch changes to on add key 1 to the global variable
if switch changes to off remove key 1 from the global variable

I might do something similar with my mother-in-laws key that she has. Currently she has an arrival sensor that lets her enter our house whenever she wants, which has always been a little concern of mine (lets say me and her daughter are… well you know :love_hotel:

This way I could just say to alexa “let the mother in” and her key would be activated, or “lock the other out” and her key would be deactivated :wink:

Do you think doing it my way would make it easier for you?


#6

I think my method is simpler, actually. It involves only one trigger - the IFTTT webhook, vs a switch for each key. It would definitely be improved if only I could reference a device dynamically though…


#7

you could try setting a variable for each key, then when you say add key 1, you are simply adding the device stored in that variable


#8

Or create a device array and use keyArray[index]. Just note that array index starts at 0 so you will want to pass in numbers starting at 0 or subtract 1.


#9

yeah this is exactly where I was coming from, I had assumed he was already using an array and that what was needed was a way to identify in code what key number matched the actual device, which you can do in a variables as well, key1 = device 1, key 2 = device 2, etc then when you say add key 1 you simply add device 1 to array[index+1]