====Triggering Code==== The **Trigger** library is used to execute code when specific events are detected in your live data. This can be very useful for producing event, batch or production-run reports. Unlike shift and daily reports or analytics, triggered events often have erratic timing. It can also be useful for notifying users of particular process events, such as a production line stopping outside scheduled times. It's also useful for summarising fast-moving events. ===Creating a Trigger=== To create an ARDI trigger, you create a function that you want to run based on the data-driven event. You can use //python decorators// to queue the function to run when specific conditions are met. You can also trigger code on **periods** of time rather than single moments. When called, the function is passed a object that includes information about the triggering event - the current and previous values of the properties that triggered the event, the amount of time it's been active etc. The object also gives you easy access to the [[mos:welcome|Modular Output System]] to write KPIs, metrics and events. For example, the code below logs an event whenever Wind Turbine #1 stops producing power.t import arditrigger as at import datetime @at.trigger @at.data('Wind Turbine #1.Power') @at.when(lambda o: o.Value() == 0) def TurbineStopped(o): if o.Condition() == True: o.LogEvent("Turbine #1 Has Stopped") at.Interactive("demo.optrix.com.au","ex",secure=True) ===Description=== @at.trigger This marks the function as one that should be //triggered// by live data. @at.data('Wind Turbine #1.Power') This specifies a point of live data that is used in the your event trigger logic. This can include a single point name or a list of point names, in the form //.//. @at.when(lambda o: o.Value() == 0) This is a python **lambda function** that returns True if the triggering condition has been met (although this behaviour can be changed with other decorators). In this case, it returns True when the value of the one data point we have is 'zero'. If you are using multiple data points, o.Value is an array. @at.interactive This specifies which ARDI server to connect to, and that the triggers should be run //interactively// - which means that the script remains persistently running as a service and accepts command-line parameters to control its behaviour. ===Controlling Trigger Timing=== Normally, triggers are only called on the //rising edge// (ie. when the condition first becomes //true//). You can adjust this using decorators such as **@changes** (called at any change) or **@falling** (called when the condition goes back to //false//). ===Trigger Attachment=== Triggers can also be [[agency:alert_attachment|attached]] the same way as Agency Alerts - meaning you can create a single trigger that works across all assets of a particular type. See the [[trigger:start|trigger documentation]] to learn more. ===Examples=== The [[analytics_example_bundle]] contains two examples of simple triggers. **single_turbine_example.py** performs basic logging of when a turbine stops producing power. **all_turbines_example.py** is similar, but does a couple of things differently... * It connects to //all// of the wind turbines rather than just one, \\ * It outputs a digital (ie 0 or 1) value indicating if each turbine is running or stopped. ===Continue Reading=== [[full_cycle|Discover Full-Cycle Integration]]