Analytics builder: Get event data for counter

Hi

I want to implement a counter. It should count events with a certain type and text.
I have two different text for the type c8y_DataType: ( They are defiend in the LoRa Device Protocol)
{ “STOP”: { “unit”: “Pa”, “value”: -98541.6015625 } }
{ “MOVE”: { “unit”: “Pa”, “value”: -98535.1640625 } }

I only want to count the ones with “STOP”

How to extract the “STOP”?
I tried it with c8y_DataType.text in the Extract Property Block.
No success.

I even created a seperate event, that only has the event type defined as c8y_ElevatorStop and event text as “Stop”.
Then I used c8y_ElevatorStop.name and c8y_ElevatorStop.text.
Also no succes.
And it did not work for connecting the event output directly to the counter.

How to do this properly?

Thans
Johannes

Hi Johannes,

is it mandatory for you to solve this with the Analytics Builder? I’m not an expert on the Analytics Builder, but I guess this could easily be solved using EPL Apps in the Streaming Analytics application of Cumulocity. Can you share some more information what you want to do with the counter? Should it be stored on the representation of the corresponding device or should the count be written as its own event?

One solution might be to set up a general listener for Cumulocity events in an EPL monitor. In the listener you would filter for the STOP events. In case of an STOP event, you load the corresponding device and its representation. For the loaded device you check whether it already has a count property assigned to it. If yes, then you increment the counter by 1 and send a PUT update for the device. If the count property isn’t available yet, you create the property on the device and send an update to the database.

This approach would be quite generic and you would even persist the count of STOP events directly on the corresponding device representation.

Best regards
Christian

Hi Christian

I am not bound to the Model Builder, but since it has a counter, I was trying to use it.
But yes, it could be done with an EPL Script.

I am monitoring a device in an elevator and want to count the stops during the day. So at night, it has to be reseted. This counter I want to display in the dashboard of the app.

Best regards
Johannes

Hi Johannes,

When you are using the specifically defined event type c8y_ElevatorStop, you don’t need to use the extract property block. You’ll specify the value ‘c8y_ElevatorStop’ for ‘Event Type’ parameter in the input block. This block would produce output only when matching event type is received. So you can directly connect it to the Counter block. For example:


When you are using a common type, can you provide the exact format you are using? I am assuming that the format for the event would look something like following?

{
    "source": {
        "id": "130597"
    },
    "type": "c8y_DataType",
    "text": "stop",
    "STOP": { 
        "unit": "Pa", 
        "value": -18541.6015625 
    },
    "time": "2021-12-01T17:03:14.000+02:00"
}

For this, you need to specify the “STOP” as the property name in the Extract Property block as it is the top level property. Then you can connect the output to Counter block. If the “STOP” property is missing from the event, then Extract Property block will not generate the output, giving you the desired behavior. For example:



Let me know if it doesn’t work.

Hi Johannes,

in addition to Gyanendra’s solution with Analytics Builder, a solution for EPL app might look like this (note: I haven’t tested it, no guarantee that it actually works):

using com.apama.cumulocity.Event;

monitor ElevatorStopEventCounterMonitor {
	dictionary<string,integer> stopEventCountForDevices := new dictionary<string,integer>; 

	action onload() {
		monitor.subscribe(Event.SUBSCRIBE_CHANNEL);
		// listen for elevator stop events
		on all Event(type="c8y_ElevatorStopEvent") as elevatorStopEvent {
			increaseStopEventCountForDevice(elevatorStopEvent.source);
			
			// create a new event which contains the count or 
			// store current count on source device as additional property
			// to use it on the dashboard
		}

		// cron job triggered at midnight (see https://cumulocity.com/guides/apama/advanced/#timers)
		on all at(0, 0, *, *, *) {
			// reset count for all devices
			stopEventCountForDevices := new dictionary<string,integer>;
		}
	}
	
	action increaseStopEventCountForDevice(string deviceId) {
		if (stopEventCountForDevices.hasKey(deviceId)) {
			stopEventCountForDevices.add(deviceId, stopEventCountForDevices.getOrDefault(deviceId) + 1);
		} else {
			stopEventCountForDevices.add(deviceId, 1);
		}
	}
}

Best regards
Christian

Hi Gyan

The first way you described is what I actually tried.
Now, I changed some things and at the end, got back to the initial solution, and now it works fine…
Does it make a difference, if I select the device from the group or directly?
Because first I used it from the group list, now changed it to the same device from device list and now it works…

Thanks a lot
Johannes

Hi Christian
I will give it a try and report back.

Thanks for helping
Johannes

You mean you expanded the group and then selected a specific device? It does not make any difference in that case.

Thanks,
Gyan

Then it was only an updating problem somewhere…
It works fine with the direct counter.
The extract version did not work.
Is there a way to debugg models? It would be a great improvement, if you could see the (last) data samples going in an out of a block.