Hi Everyone,
I apologize for any errors here as I am new to the platform, but hopefully my research thus far or this discussion will help others.
I have been working on an app that I want to restore light settings (on/off/level) to the last known external (not set by my app) values. Looking for .isPhysical on events gets part way there, but does not cover if the last light value was set by another app. It seemed like looking for the last event with a installedSmartAppId that did not match app.id would work, but not all events are properly tagged with the triggering appid it seems. So this code does not work:
swEvents.find {(it.name == "level" || it.name == "switch") && (it.installedSmartAppId != app.id}
When I looked into the list of events after issuing switches.setLevel(100) in my app (excuse my sloppy debug format):
ID: 4a60d368-ae72-11e5-8b20-22000aef9668 | AppID: null | Description: TESTDIM1 level is 100 | Name: TESTDIM1 (level) | App: KODI Playback Controls | Value: 100 | Source: DEVICE | Desc:
ID: 4a665cee-ae72-11e5-8b20-22000aef9668 | AppID: null | Description: TESTDIM1 set level is 100 | Name: TESTDIM1 (switch.setLevel) | App: KODI Playback Controls | Value: 100 | Source: DEVICE | Desc:
ID: 4a3283d7-ae72-11e5-8b20-22000aef9668 | AppID: d8b42bac-a8e5-4373-b0bf-4a566a2f72a3 | Description: null | Name: KODI Playback Controls (null) | App: KODI Playback Controls | Value: setLevel | Source: APP_COMMAND | Desc: KODI Playback Controls sent setLevel command to TESTDIM1
There are three events for the single setLevel command, the two “useful” ones (level and switch.setLevel) both have a null AppID even though they were triggered by a specific app. The third event seems to actually log the fact that the application initiated the event and correctly has an AppID. The problem here is that I want to exclude the events with null AppIDs when searching through events, but there is nothing particularly distinct to distinguish them from other app events.
Then I noticed a pattern in the event IDs. The first 8 digits are unique, but the remaining 4 sets always seem to match for any given action taken (in this case the single setLevel command issued by the app). Using this I created a nested search to see if each level event had a corresponding “app event” (with matching AppID). This looks like so:
def lastState = swEvents.find {
(it.name == "level" || it.name == "switch") && (swEvents.find{it2 -> it2.id.toString().substring(8) == it.id.toString().substring(8) && it2.installedSmartAppId == app.id} == null)
}
So far this seems to work correctly in finding me the last level or switch event not triggered by my app. I have some concerns about this being a totally undocumented workaround based on an assumption about event ID formats, but I don’t see a better way at the moment.
Does any one know of a better way to accomplish this or know why installedSmartAppId is not corrctly listed on all events? I did search through the forums for answers and only found a dead end discussion on the same topic from a few months back (How to tell when an event was triggered by the current app instance).
Appreciate any feedback or discussion!