How do I get the triggering user or device id in a workflow?

Sometimes, you'll need to know which device or user sent the event that triggered the workflow. Simply query any event using the Analytics library 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"]