[ANSWERED]Best way to "store" devices in a plugin

Posted on
Sun Jun 21, 2015 7:50 am
Colorado4Wheeler offline
User avatar
Posts: 2794
Joined: Jul 20, 2009
Location: Colorado

[ANSWERED]Best way to "store" devices in a plugin

I have a plugin that seems to have been working but suddenly stopped. It's a pretty basic concept, the user configures one or more weather snoop devices and the plugin fires when one of those devices changes (via indigo.devices.subscribeToChanges() and then deviceUpdated().

My question is if it's better to store the device ID or the device itself in the plugin. If I store the ID then each time I run my function I need to do an indigo.devices[12345], if I store the device then I can just simply get the states I'm after (wsdevice.states["temperature_C"] for example).

My plugin storing the devices as variables seemed to be working great but suddenly broke for some reason and now the variable (wsdevice) is "stale" and only shows data as of when the plugin was reloaded rather than maintaining a persistent connection to the device for the current states.

If I am able to keep a persistent connection to the device as a variable in my plugin, what would cause it to go stale like this all of the sudden? When I say "all of the sudden", I did some code clean up to take out testing functions and stuff like that, so I'm wondering if I accidentally took something out of my script that keeps that device in a persistent state or if I was just getting lucky before. Its frustrating to have it break so suddenly but if I was doing it wrong in the first place then I suppose I can't complain, it just seems expensive to query the device list each time so it made more sense to me to keep a persistent device in the plugin.

My Modest Contributions to Indigo:

HomeKit Bridge | Device Extensions | Security Manager | LCD Creator | Room-O-Matic | Smart Dimmer | Scene Toggle | Powermiser | Homebridge Buddy

Check Them Out Here

Posted on
Sun Jun 21, 2015 12:08 pm
jay (support) offline
Site Admin
User avatar
Posts: 18255
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Best way to "store" devices in a plugin

When you get a device instance from the server:

Code: Select all
dev = indigo.devices[12345]


You're getting a copy of the device as it is on the server at that actual moment. It is not the actual server instance (since plugins run in their own process). This was done for a variety of reasons that I won't go into here. So, you can keep a reference at runtime in your plugin, but before you use any state/property in the device instance that you want to be up to date, you must first call:

Code: Select all
dev.refreshFromServer()


(this actually applies to any IOM object). So, from an efficiency perspective, there's not much difference really in refreshing an existing copy of a device instance and getting a new copy. In our plugins, we've done both and I can't think of any real advantage to either approach at the moment, except to say that introducing a bug because a cached instance isn't up-to-date might be harder to spot.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Sun Jun 21, 2015 2:18 pm
Colorado4Wheeler offline
User avatar
Posts: 2794
Joined: Jul 20, 2009
Location: Colorado

Re: [ANSWERED]Best way to "store" devices in a plugin

Ok, thanks for the answer Jay, I've already started making sure the device gets refreshed now before I do anything else but it's good to know what the actual rule is.

My Modest Contributions to Indigo:

HomeKit Bridge | Device Extensions | Security Manager | LCD Creator | Room-O-Matic | Smart Dimmer | Scene Toggle | Powermiser | Homebridge Buddy

Check Them Out Here

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 1 guest