Detecting an Unloading Operation with Trigger

In this example, a crane is moving to locations that we can see in it's position property. It is also carrying product that we can see the weight of with weight.

In this trigger, we want to detect when the crane is unloading product. We want to determine when the unload began, when it finished, and how much product it dropped off.

NOTE: Because the unloading process isn't always smooth, we have a distinct start and end rule for our trigger. In this case, we start whenever the position is not 0 and the weight begins to fall, and we finish whenever we change position again.

@at.trigger
@at.data('Crane.Position','Crane.Weight')
@at.startwhen(lambda o: float(o.Value(0)) >= 1 and o.IsFalling(1))
@at.endwhen(lambda o: o.IsChanging(0))
def ChargeRecord(o):
 
    position = o.Value(0)
    if o.Condition() == True:
 
        #Indicate that a oven push has started...        
        o.Output("Machine/Loading Point",int(position))
        o.Output("Machine/Loading",1)
        o.Output("Machine/Mode",2)
        o.startinglevel = o.LastValue(1)
    else:
        #Ignore if the condition is false on the first run.
        if o.FirstRun() == True:
            o.Output("Machine/Loading Point",0)
            o.Output("Machine/Loading",0)
            o.Output("Machine/Mode",0)
            return
 
        #Get the time of the event...
        r = o.Range(utc=True)
 
        #Mark as no longer charging and log a charge event.
        o.Output("Machine/Loading Point",0)
        o.Output("Machine/Loading",0)
        o.Output("Machine/Mode",0)
 
        newlevel = o.startinglevel - o.Value(1)
        o.LogEvent("Loaded Point " + str(position) + " with " + str(newlevel) + " kg",r[2])
        o.Output("Loading Point " + str(position) + "/Drop Weight",newlevel)
 
        #Process these details
        LoadAnalytics(r[0],r[1],o)
 
 
def LoadAnalytics(start,end,options):
        #Do some custom analytics here...
        pass