How to use custom fields with event rules
Using this method you will allow your application users to select their notification preferences for their assets' event rules. This is especially useful in a restricted environment with users with limited roles. You as an administrator will save time with fewer user modification requests.
The users can do it by themselves in a very intuitive way by just modifying the asset properties and select the values in the GUI. In our example, we will create a combo box (Custom Field) that is going to be available in the Asset Properties (User Access Mask) where the user can select and save which notification type he wants for a speeding alert for that asset. The speed limit value is also a property that the user can set for each asset and will define the execution of the event rule.
Steps
- Add the Custom Fields
- Modify the User Type
- Create a new User Access Mask
- Create a new role and assign the Access Mask
- Create your Event Rules with the script
- Testing
Add the Custom Fields
Read how to enable user custom fields here.
We have to select the Custom Field list and put here all the different notifications that our Event Rules will handle. We have to remember what we have typed in the Item and Value columns because we will call them later in our scripts.
We also want to add another Custom Field called Speed Limit of the type Text
Modify the User Type
Now that we have these Custom Fields set we need to add them to our Assets’ properties. To simplify this task we will modify the Device User Type in the User Type Editor.
We select User Type Device, and then we will add our new Custom Fields created previously, under Details
If we edit an Asset we can see the two added Custom Fields
Create your Event Rules with the script
Now we have to create Event Rules for each different notification we want to send. As we created three different choices for our notifications (Email, SMS, Marker, Disabled) we will use three different Event Rules. One is for email, another for SMS, and another for marker.
Email notification case
When creating your Event Rule, on step 4. Expressions, select Script Expression.
Read how to enable script expressions here.
In this example, we will use a simple Javascript code that will compare the Speed Limit value from the asset properties and return true if matches the condition.
A script for the Email notification could be like this:
var notifType = session. user .getCustomFieldValue ( 'Speeding Alert' ); // from the Asset properties
var limit = session. user .getCustomFieldValue ( 'Speed Limit' ); // from the Asset properties
var speed = fields.get( 'Speed' , null )*3.6; // current Asset speed with mt/s to Km/h conversion
if(notifType == 'Email' && speed > limit) return true ;
// else
return false ;
|
Remember that the values you are using in the function getCustomFieldValue and the comparison values inside the IF clause should match the values and custom field labels set up previously.
Select Email as the notification type for this rule
When you've configured your email notification rule, Save it.
SMS notification case
Create a new rule for SMS cases.
The SMS and Marker scripts change slightly. In the following images, we highlight the changes you need to make:
Marker case
For the Marker notification case, we create a new event rule and introduce the changes shown below.
We won't select any notification on this step (because we just want a marker on the map, we do that in step 6), we click on Next.
We select our on-map options as below
Save now the rule.
Testing
Verify for your asset/s which custom fields need to be selected, according to the notification type you wish for each asset.
1. Edit the asset.
2. Input the values for Speeding Alert and Speed Limit.
These values will define which notification (event rule) will be used and at which speeds these alerts will react.
We recommend using the asset simulator to test the behaviour of these new rules and notifications.