Selecting from multiple devices of the same type

I'm working on an app to control multiple thermostats.

As a test I created two virtual thermostat devices and used them in the app.

This is how I got them into the app:

input(name: d_tstat_1 , type: capability.thermostat, title: use thermostat..., description: , required: true)|

input(name: d_tstat_2 , type: capability.thermostat, title: use thermostat..., description: , required: true)|

Now I would like to do something like this where tid specifies which device to use

switch(tid) {
case "1":
def d_tstat_x = d_tstat_1
break
case "2":
def d_tstat_x = d_tstat_2
break
}

def temp = d_tstat_x.currentValue("currentTemp")

Is this sort of thing possible?

Thanks, Frederick

Yes, but how do you want to choose between them? You could use the idAsLong property (or id, though that is a String and seems excessive unless that's the type I really need in that context) for something that is truly tied to the device, but displayName or anything you can access would also work.

If you have two separate inputs like that, you could do something like:

switch (device.idAsLong) {
  case d_tstat_1.idAsLong:
     // stuff
    break
 // ...
}

This is assuming you have what I called device (but could be called anything) in that context. In an event subscription handler, for example, you'd have an event parameter with a device property you could use instead of this (and actually a deviceId property you could use to save a bit of typing or the need to dig into the device yourself).

If it's something else, you may need to explain more about what you're trying to do.

1 Like

Thanks.

Being able to set one var to hold the reference to a specific device works fine.

In my case the code in the app is responding to commands received over a telnet connection to control/monitor 1 of 8 thermostats.

A command is a simple string with three parts delimited using the = char which makes getting the individual tokens easy:

thermostat_id=command_name=command_value.

For example to get the current indoor temp from thermostat 1 the string looks like:

1=get_status=temp_inside

To set the operating mode for thermostat 4 to auto the string looks like:

4=set_mode=auto

Since the code that process the command names and values is the same for all 8 thermostats I needed to first use the thermostat ID to control which thermostat the code referenced. And the switch command works just fine doing that.

The app is mostly working.

I'm refining the handling of changes made at the thermostat which need to get communicated back to the ELAN automation system and changes made in the automation system out to the thermostats. The timing is proving to be a bit tricky since, under certain conditions, changes are going both ways at the same time.

Thanks, Frederick

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.