How to get user or device id from in a workflow

Sometimes, you'll need to know which device or user sent the event that triggered the workflow.  Simple query any event using Analytics to retrieve the user id.  Here's an example:

 

import Analytics
import Filter
def get_API_user_id():
    last_event = Analytics.events('raw', Filter.string_tag("raw.mqtt_connected"), None, 1, ['timestamp', 'DESC'])
    return last_event[0]["user"]