Sofia2 IoT Workshop: CEP Rule Creation. SCRIPT Rule Creation. Final Exercise. (Part 4/4)

The goal of this workshop is the realization of a real example on which we can evaluate the capabilities of Sofia2 platform.


In order to do this, we are going to simulate a building that has several floors, and three devices for the reading of energy consumption, temperature and humidity in each one of them.  Finally, we will create a dashboard to display this information and we will publish data in an API to be consumed in a simple way by any application.


In order to get a better understanding of the workshop, we will structure the steps to follow in 4 posts.


In previous posts we were working about:


(Part 1/4) – Data Model. Ontology. ThinKP.


(Part 2/4) – Simulate input data. Simulator Configuration and Execution.


(Part 3/4) – Control panel. Gadget and Dashboard Creation. Publish Ontology as Api.


Today we will see (Part 4/4) where we’ll create a CEP rule, a SCRIPT rule, and we’ll make a final exercise. This will conclude the IoT workshop.



CEP Rule Creation


Access to ‘My CEP Events’ menu and click on ‘New Event’.




As Identification we’ll assign ‘IoTWorkshopEVENT’ + Our initials, we’ll select our ontology and press the ‘Load fields’ button.




Select ‘TEMPERATURE’, ‘TYPE’ and ‘LOCATION’ fields and press ‘Create’ button.


Let’s look at ‘CEP Event name’ column, that will be the name we should use in the next point.




Now go to ‘My CEP Rules’ menu and click on ‘New Rule’ button.




Select the Event we created.




In ‘from’ field we set the compliance parameters of the rule.





In ‘select’ we set the fields we want to retrieve when CEP rule is released.




In ‘Insert Into’ we set the rule we want to generate, in our case WORKSHOPIOTRULE + Our initials. Once you have entered the three boxes, press the ‘Create’ button.




We have already created a rule that will generate an event each time an ontology instance arrives with value ‘TEMPERATURE’ greater than 30 and is ‘TEMPERATURE’ type.




SCRIPT Rule Creation


Access to ‘My Script Rules’ menu and click on ‘New Script’.




We assign IoTWorksopSCRIPT + Our initials to the Script Identification. We assign a timeout of 5 seconds, choose the type of Script ‘CEP’ and select the rule we created before. Now, when the event associated with our rule is released, this script will be executed. Finally, we chose ‘Groovy’ language to the Script.




We force the execution of the ‘Then’ block by adding a return true in the ‘if’ evaluation block.




At Motor Scripting Usage Guide we’ll find more information about the use of Script and the APIs that it offers.




Final Exercise


In the ‘then’ block we’ll add the logic we want to execute when the CEP events occur.


The following code sends an email notifying us that we have exceeded 30 degrees.





If we want to retrieve the Event data, the projection we made through the ‘select’ clause of the CEP Rule, we have the ‘InEvents’ Object.




And through the attribute ‘getValuesJson’ we can retrieve each of the attributes of the Event, which were ‘VALUE’ and ‘LOCATION’.


To finish, I propose to create a new ontology we’ll call IoTWORKSHOPALARM + Our initials, which should contain the fields LOCATION String and VALUE Number, both required.


We can use the same ThinKP we created at point 4 and also assign this ontology to it, and finally use the API Script to perform an insertion in the ontology Alarm when an event occurs.


Here’s an example of how to insert an ontology from the Script Rules:




At Sofia2 Documentation you have all the documentation of the platform.


The APIs SCRIPT Guide describes the APIs availability.


So far the IoT Workshop Sofia2.


We hope you enjoyed it and we’ll be waiting for you at the next workshop!

Sofia2 IoT Workshop: CEP Rule Creation. SCRIPT Rule Creation. Final Exercise. (Part 4/4)


Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de

Estás comentando usando tu cuenta de Cerrar sesión /  Cambiar )

Google photo

Estás comentando usando tu cuenta de Google. Cerrar sesión /  Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión /  Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión /  Cambiar )

Conectando a %s