How to include an event detector value into a function
-
Hi guys,
I need to write a script that gets data from a data point and compares the result with a data item from an event detector.
From the drop down list I can not select in External Content the event detector and associate it with a variable. Is there a way to do this? -
Have you tried doing your script in the event handler ? The event handler can set a point when the event is triggered. Or you can write a script
-
Hi pierjsap,
CraigWeb's direction is certainly one to look in. If you are trying to script some logic on the event detector raising or returning the event, then indeed a Set Point Handler with its appropriate active or inactive action being "set point to scripted value" which will be executed when the event occurs, and you can
return UNCHANGED;
from the script body to not set a value to the target point, if desired. The event is also in the context for event handler scripts as 'event'You may need to add detail as to what 'data item from an event detector' means. Currently there are not script-flavored ways of getting, say, the last ten events for a point. In the current version you could get a List<EventInstance> objects for the point by,
//Not limited, the second argument is a userId since you need permissions // to view the data point to view its events. So, I am using 1 presuming // the 'admin' user is intact var eventsList = com.serotonin.m2m2.db.dao.EventDao.instance.getEventsForDataPoint(contextPoint.getDataPointWrapper().getId(), 1);
Does make me think scripts could use a better utility for querying events.
-
Thanks guys, i'm try to understand ;)
I will try to manage events in both ways ... -
@CraigWeb @phildunlap
This is what I would like to achieve.
There is a way to get the object for example:
ED_1 {"XID": "xidxxxxxx", "value": "xx", "stateAlarm": "safety"}
to read it in the script of the event handler of my ED4_SetPoint? -
Can you explain the arrow to the event detector from the large box on the right? You cannot set values to an event detector, but you could set the value to the meta point and have it trigger that event detector.
Somehow I didn't think of this while writing my post yesterday, but you can get if a point's event detectors are currently active like the following,
//The CONTEXT_POINTS object is a map of varNames to DataPointRT objects, so we'll get // the eventDetectors on the point and check if they're active. Note that if the point was disabled // its key would be null in the CONTEXT_POINTS object, and this first statement would NPE var eventDetectors = CONTEXT_POINTS["p1"].getEventDetectors(); for(var k in eventDetectors) { print(eventDetectors[k].isEventActive()); }
Given that the data points you describe are all on the same data source, here's what I'd do:
- Create a meta point with a binary data type to handle the "ED_4" logic, like
for(var varName in CONTEXT_POINTS) { if("my" === varName) continue; //The meta point is in its own context, so skip it var eventDetectors = CONTEXT_POINTS[varName].getEventDetectors(); for(var k in eventDetectors) { if(eventDetectors[k].isEventActive()) return true; } } return false;
- Have only one of the points update the context. Otherwise multiple updates will occur at the same millisecond, which is not efficient.
- Use a one second execution delay, this ensures that if the point that is updating the context is updated before the other points, those points have a chance to be updated before the event fires and computes the state of the meta point.
- Create a state event detector on the meta point for whatever you need to trigger from this true/false.
edit: A note about execution delays:
You cannot use an execution delay longer than or equal to the frequency of updates in the meta points context. Meta point execution delays will cancel the delay timer and start it again from a second update, while a script data source will run at the execution delay from the first update it received while not waiting to execute. -
@phildunlap
You understood perfectly what I wanted to do. In practice, I wanted to group a series of typed alarms into one. Thanks for your help. I will write you about any developments -
@pierjsap
Please do share your solution as I can see this logic being very useful. I think my method would be to duplicate your event sent points and logic in the meta data point script. Then use the if statement that your proposed to set the meta point. Keep the event detectors so that your event page will still list the events.