Novice Question - Updating Changed Device States

Posted on
Sun Aug 27, 2023 9:07 am
ryanbuckner offline
Posts: 1080
Joined: Oct 08, 2011
Location: Northern Virginia

Novice Question - Updating Changed Device States

My Life360 plugin appears to be working fine, but I recently discovered that I update every device state regardless of whether it's value has changed. I do this because I incorrectly assumed that Indigo magically handled that during state saving. The problem obviously is that triggers always think a device state has changed, and fire unnecessarily.

Currently I do the following:
1) Call the API for each device_id in updatedevicestates which is called in a loop for each device from runConcurrentThread at the frequency chosen in pluginProps
Code: Select all
def updatedevicestates(self, device):

2) Get every state for that device from Life360 and save it in an array called device_states
Code: Select all
device_states.append({'key': 'member_first_name','value': m['firstName'] })

3) Update all the device states
Code: Select all
device.updateStatesOnServer(device_states)


What's the right way to update only the device states that have changed?

Posted on
Sun Aug 27, 2023 12:51 pm
DaveL17 offline
User avatar
Posts: 6759
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Novice Question - Updating Changed Device States

I would send the dict to a method that compares each state to the state value Indigo has and return a dict of only those states that have actually changed. There's a Python library called dictDiffer that Indigo uses for its own purposes. You should be able to import that directly in your plugin.

A word of caution with this: only users on Indigo 2022 or later will have this library automatically installed by Indigo, so if you want to support users of prior versions, you'll need to include the library in your plugin (or have users install it directly).

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Mon Aug 28, 2023 1:38 pm
ryanbuckner offline
Posts: 1080
Joined: Oct 08, 2011
Location: Northern Virginia

Re: Novice Question - Updating Changed Device States

I assume this is something that every plugin needs to do? What do they do?

Posted on
Mon Aug 28, 2023 1:41 pm
FlyingDiver offline
User avatar
Posts: 7222
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Novice Question - Updating Changed Device States

ryanbuckner wrote:
I assume this is something that every plugin needs to do? What do they do?

If I have a data source like yours, I update all the states every time. Haven't gotten any complaints. Are you sure it's actually triggering if the state value hasn't changed? Or that you're not sending multiple updates with different data?

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Mon Aug 28, 2023 7:50 pm
ryanbuckner offline
Posts: 1080
Joined: Oct 08, 2011
Location: Northern Virginia

Re: Novice Question - Updating Changed Device States

I'll have to do more testing. Got some feedback that the geofence info was always changing via trigger even when it had not.

https://github.com/ryanbuckner/life360-plugin

Posted on
Mon Aug 28, 2023 8:10 pm
FlyingDiver offline
User avatar
Posts: 7222
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Novice Question - Updating Changed Device States

You call to device.updateStatesOnServer() on line 366 has the wrong indent. It should be indented two more tab stops, I think.

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Mon Aug 28, 2023 8:49 pm
jay (support) offline
Site Admin
User avatar
Posts: 18224
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Novice Question - Updating Changed Device States

The server should not fire a changed event, if the value of the state is set to the same value. You’ll want to make sure that you’re not setting the value to something else and then back in your code. We’ve had several occasions when a developer didn’t realize that they were setting the value do something else (like None or empty string) and then back to the original value.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Tue Aug 29, 2023 7:49 am
ryanbuckner offline
Posts: 1080
Joined: Oct 08, 2011
Location: Northern Virginia

Re: Novice Question - Updating Changed Device States

jay (support) wrote:
The server should not fire a changed event, if the value of the state is set to the same value. You’ll want to make sure that you’re not setting the value to something else and then back in your code. We’ve had several occasions when a developer didn’t realize that they were setting the value do something else (like None or empty string) and then back to the original value.


For the geofence states, I'm likely doing some form of this because they are calculated values. I'll take a look and make sure I'm minimizing the areas where I set to empty string or 0 first.

Posted on
Tue Aug 29, 2023 8:35 am
FlyingDiver offline
User avatar
Posts: 7222
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Novice Question - Updating Changed Device States

FlyingDiver wrote:
You call to device.updateStatesOnServer() on line 366 has the wrong indent. It should be indented two more tab stops, I think.


FYI, this is probably why you're getting bogus updates. You're making that call even when the device doesn't meet your criteria for updating.

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Tue Aug 29, 2023 8:52 am
ryanbuckner offline
Posts: 1080
Joined: Oct 08, 2011
Location: Northern Virginia

Re: Novice Question - Updating Changed Device States

FlyingDiver wrote:
FlyingDiver wrote:
You call to device.updateStatesOnServer() on line 366 has the wrong indent. It should be indented two more tab stops, I think.


FYI, this is probably why you're getting bogus updates. You're making that call even when the device doesn't meet your criteria for updating.


Ohh, I see that. Thank you!

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 7 guests