Trying to better understand timing of state value updates

Posted on
Thu Jul 27, 2023 8:42 am
pgershon offline
Posts: 509
Joined: Jul 10, 2004

Trying to better understand timing of state value updates

I have been modifying a plugin from the library and do not fully understand the behavior and timing of value changes I make to a custom-designed state on a device. Let me try to explain what is happening and perhaps someone can guide me to what I should do differently:

I am working with TP-Link Kasa dimmer devices. What I want to do is distinguish changes to a dimmer's on/off state that are initiated by Indigo from changes initiated by the switch itself. So I added an additional state to the device called buttonState (I actually created buttonState and buttonStateA as a way of further troubleshooting and will use in my examples below).

Code: Select all
    buttonPress  = self.tpLink_self.getDeviceStateDictForNumberType("buttonPress", "buttonPress", "buttonPress")
    statesDict.append(buttonPress)
    buttonPressA  = self.tpLink_self.getDeviceStateDictForNumberType("buttonPressA", "buttonPressA", "buttonPressA")
    statesDict.append(buttonPressA)

The plugin runs a polling loop for each device which starts with buttonPress=0 and ButtonPressA=0. The polling loop looks for instances of changes to on/off state(if devState != lastState), and when it finds one, updates the devices states on the server. As the plugin works now, buttonPress is set to 1 if the dimmer goes on and buttonPress is set to 2 if the dimmer goes off. I have tried to add the additional condition that if buttonPressA is less than 0, then do not change buttonPress to 1 or 2 but skip that step.

I also have an action defined where Indigo is told to change the dimmer status (on/off/toggle), and it changes buttonPressA to -1 when that happens. On/off changes initiated by pressing the button on the dimmer itself (and not by Indigo) do not change the value of state buttonPressA (so it should remain 0). So when the action is called, this code is initiated:

Code: Select all
            state_update_list = [
                {'key':'buttonPressA, 'value':-1},
                ]
            dev.updateStatesOnServer(state_update_list)
            dev.stateListOrDisplayStateIdChanged()
            indigo.server.log("state:"+str(dev.states['buttonPressA']))  ####  I can tell here that buttonPressA value is indeed -1

The goal is that device changes initiated by Indigo should have buttonPress=-1 and changes initiated by the device itself have buttonPress=1 or buttonPress =2. I do this because I have no way of actually triggering an action by a button press - the devices API does not sense button presses at the device - Indigo can only react to state changes.

Here is the problem: When buttonPressA becomes -1 (via the action), the polling loop still sees its value as 0, so it will treat changes to device on/off state the same whether initiated by dimmer or Indigo. The value gets updated to -1 only after the loop processs the state change:

Code: Select all
            if devState != lastState:
                self.logger.info("second state:"+str(buttonPressA'])) ######  just so I can follow the value changes - "0" gets displayed instead of -1
                        if buttonPressA >-1:
                            if logState=="On":
                                indigo.server.log("button press on (dimmer)")
                                buttonPress=1
                            else:
                                indigo.server.log("button press off (dimmer)")
                                buttonPress=2
                 lastState = devState

After processing the device changes, I look at the Indigo main screen and under my device buttonPressA=-1 and buttonPress=1(or2). So the buttonPressA's value was updated from 0 to -1, but my look did not detect the change, even though it detected the on/off state change. From my log, I get an alert that buttonPressA is -1 ("state:-1") but then I get a second alert that it is 0 ("second state:0"). But when I look at the GUI of the device, it lists the state as -1.

Can anyone explain why the mis-timing? My goal is to have the "action" update something (I chose a device state but could be anything) that the polling loop will detect. Thanks in advance and I hope this question can be followed - I tried to be clear.
Last edited by pgershon on Thu Jul 27, 2023 10:10 am, edited 1 time in total.

Posted on
Thu Jul 27, 2023 9:41 am
FlyingDiver offline
User avatar
Posts: 7222
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Trying to better understand timing of state value update

I'm not sure I can give you a complete answer, mainly because it's really hard to read your post.

1. Use the CODE tags for all code sections in your post. Otherwise, indentation gets lost and the code is really hard to read.

2. You only need to call dev.stateListOrDisplayStateIdChanged() when the state definitions change. Don't call it when state values are updated.

3. Your "dev" device is a static copy of the server's device when the plugin's method is called. If you do something to change the device's properties or state, and you need to use those updated values in the current method, then you need to refresh the device object. dev = indigo.devices[dev.id] should do it. There might be a more efficient way.

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

Posted on
Thu Jul 27, 2023 10:09 am
pgershon offline
Posts: 509
Joined: Jul 10, 2004

Re: Trying to better understand timing of state value update

Thank you. That did it dev = indigo.devices[dev.id] . I will fix the code tags too,

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 6 guests