SmartApps and past events

I’m writing my first SmartApp for my specific home automation setup. There are a couple of places where I query past events for a device in order to determine what to do. For instance, when I get a temperature event to check for a “too cold” or “too hot” warning, I first attempt to get the previous temperature event. I use this info to prevent multiple notifications (i.e., on notify “too cold” if the current temp is below the threshold and the previous temp was above it). But when I run the SmartApp in the simulator, the first time the temperature event happens, device.events contains only that event and nothing prior. Any subsequent triggers will show events that occurred since installing the SmartApp. Is this just a simulator thing? Or is there no way for a SmartApp to get at the full device event history? Thanks.

It might be a simulator bug, or source of confusion (if you’re using a simulated device, that device is created on the fly when installed in the simulator, so has no previous events). You’ll probably be best served by self-publishing and installing on your mobile app, then looking at logging for debugging purposes.

You can get up to the last seven days worth of event history using the events() method as you are doing.

Hope that helps!

1 Like

Thanks for the quick reply, @Jim. I am not using any simulated devices, so I’ll have to do a real install later when I have a chance. I will report back, but hoping it’s just a simulator bug.

1 Like

@Jim, I’ve self-published the app and the results seem to be hit-or-miss. Here’s an example from my app that takes a temperature event, and checks it and the previous two temperature events against a threshold.

def temperatureHandler(evt)
{
    def pos = tempSensors.findIndexOf { it.id == evt.deviceId }
    
    if (pos >= 0)
    {
        log.debug "temperature from $evt.displayName: $evt.value"

        def recentTempEvents = evt.device.events ()?.findAll { it.name == "temperature" }
        def actualTempLow = (tempLow) ? tempLow : 35
        def actualTempHigh = (tempHigh) ? tempHigh : 85
        def lastTemp = (recentTempEvents && (recentTempEvents.size () > 1)) ? recentTempEvents[1].doubleValue : -1
        def prevTemp = (recentTempEvents && (recentTempEvents.size () > 2)) ? recentTempEvents[2].doubleValue : -1

        log.debug "last temperatures from $evt.displayName: $lastTemp, $prevTemp"

        if ((evt.doubleValue <= actualTempLow) && ((lastTemp == -1) || (lastTemp > actualTempLow)) && ((prevTemp == -1) || (prevTemp > actualTempLow)))
        {
            sendPush("Low temperature warning from $evt.displayName: $evt.value")

            if (audioDevice)
            {
                def audioTrackName = "lowTempAudioTrack" + pos
                def audioTrack = this."$audioTrackName"

                if (audioTrack)
                {
                    log.info "temperatureHandler (low) - $audioDevice.displayName play track $audioTrack"
                }
            }
        }

        if ((evt.doubleValue >= actualTempHigh) && ((lastTemp == -1) || (lastTemp < actualTempHigh)) && ((lastTemp == -1) || (lastTemp < actualTempHigh)))
        {
            sendPush("High temperature warning from $evt.displayName: $evt.value")

            if (audioDevice)
            {
                def audioTrackName = "highTempAudioTrack" + pos
                def audioTrack = this."$audioTrackName"

                if (audioTrack)
                {
                    log.info "temperatureHandler (high) - $audioDevice.displayName play track $audioTrack"
                }
            }
        }
	}
}

After a settings update, I sometimes (but not always) see:

56c1fd13-bd83-4bfd-b220-4936698acb63  4:16:38 PM: debug last temperatures from Master Bedroom Door: -1, -1
56c1fd13-bd83-4bfd-b220-4936698acb63  4:16:38 PM: debug temperature from Master Bedroom Door: 72

I’ve written the code so that it doesn’t necessarily rely on the previous event information, but it would certainly be nice to know why I’m not getting it when events() is supposed to give m a 7-day history. Thanks.