< Back to Tips for developers

Yet another way to monitor temperature and send alerts with Live Objects

You monitor events from your IoT devices? You can use directly Live Objects to alert (human/systems) when a device sends a telemetry message with a value that triggers one or several exact value(s).
Use Event processing API and Event2Action API if you are interested, just follow that "how to".

Share

Let’s do it!

We will demonstrate some alerting features embedded in Live Objects. These features are only available through APIs and are documented in the developer guide.

We will keep the devices created on Part 1: MQTT devices with tag (“freezer” or “fridge”) depending on the type of the device, see the post “How to monitor temperature and send alerts with Live Objects”.

Our use case: monitor a set of fridges and freezers. Be notified by email and HTTP push on a webhook when the temperature is out of the normal range at a maximum frequency of 1 msg per hour.

We will do this in 3 APIs calls:

  1. 1. First create 2 “matching rules”. Each rule will match if:
    • The expected tag (freezer or fridge) is in the field “tags” of the data message.
    • The value.temp field of the data message is out of the normal range for the device type
  2. 2. Create a “firing rule” that will associate a firing policy to the previous “matching rules”. The “firing rule” will generate “fired events”.
    • We choose a maximum frequency of 1 alert message per hour per device.
  3. 3. Create an “event to action rule” to transform the “fired event” into an email and HTTP push notification. This is like what we have done in the example in the part 1.

First create a matching rule

Here is the example to provision freezer “matching rule”, do a similar provisioning for fridge matching rule.

The body of the matching rule is a Boolean function, which matches (returns true) when there is an abnormal temperature for freezers.

POST https:/liveobjects.orange-business.com/api/v0/eventprocessing/matching-rule

Headers
x-api-key : secretApiKey
Body
{
    "name": "freezer matching rule",
    "enabled": true,
    "dataPredicate": {
        "and": [{
                "in": [
                    "freezer", {
                        "var": "tags"
                    }
                ]
            }, {
                "if": [{
                        "<": [{
                                "var": "value.temp"
                            },
                            -20
                        ]
                    },
                    true, {
                        "<=": [{
                                "var": "value.temp"
                            },
                            -10
                        ]
                    },
                    false,
                    true
                ]
            }
        ]
    }
}

Note:

As it may be difficult to develop a matching rule, you can test your configuration with the test API on our swagger.

Then create a firing rule for frequency policy

Here we decide to set the “sleepDuration“:”PT1H“.  It means that the minimum delay between 2 notifications of abnormal temperature of a same device (“aggregationKeys“: [“metadata.source“]) will be 1 hour.

POST https:/liveobjects.orange-business.com/api/v0/eventprocessing/firing-rule

Headers
x-api-key : secretApiKey
Body
{
"name":"temperature monitoring firing rule",
"enabled": true,
"firingType": "SLEEP",
"matchingRuleIds": ["insert here the freezer matching rule id","insert here the fridge matching rule id"],
"aggregationKeys": ["metadata.source"],
"sleepDuration":"PT1H"
}

Note:

If multiple devices are measuring the temperature of same physical system, you can use another “aggregationKeys” value instead of “metadata.source“.  It must be a unique identifier of your physical system. For instance, if you set a device property with a key “physicaltarget” and a value “mytarget”, you can use the following configuration

aggregationKeys“: [“extra.physicaltarget”]

Extra fields are described here:  https://liveobjects.orange-business.com/doc/html/lo_manual_v2.html#_routed_data_messages

Finaly create a alert with Event2action

We will do a provisioning very similar to the example in Part 1. But be careful to

POST https:/liveobjects.orange-business.com/api/v1/event2action/actionPolicies/eventprocessingnotifications
x-api-key : secretApiKey

{
    "id": "eventprocessingnotifications",
    "name": "notificationrule for event processing",
    "enabled": true,
    "triggers": {
        "matchingFired": {
            "version": 1,
            "filter": {
                "ruleIds": [
                    "insert here firing rule id”
                ]
            }
        }
    },
    "actions": {
        "emails": [
            {
                "type": "email",
                "to": [
                    "xxx@yourdomain.com"
                ],
                "subjectTemplate": "Freezer alarm for {{matchingContext.data.metadata.source}}",
                "contentTemplate": "Freezer alarm for {{matchingContext.data.metadata.source}}  abnormal temperature detected  ({{matchingContext.data.value.temp}} celcius)  at {{matchingContext.data.timestamp}}"
            }
        ],
        "httpPush": [
            {
                "id": "f4d37b61-d8c9-4a18-93f2-2771978ea8ac",
                "type": "httpPush",
                "webhookUrl": "https://yournotif.com",
                "headers": {},
                "retryOnFailure": true
            }
        ] 
    }
}

Let’s test it!

We use a MQTT application to simulate temperature telemetry of a freezer (MQTT.fx for instance).

To test we send a measure with a high temperature:

Publish on topic dev/data
{"value": { "temp" : 0}}

An email is received with the following body :

Freezer alarm for urn:lo:nsid:mqtt:freezer_1 abnormal temperature detected (0 celcius) at 2024-05-29T13:37:46.610Z

An HTTP push is received with the body :

{"type":"matchingFired","version":1,"tenantId":"5a6084e091b5f1166ba1016b","timestamp":"2024-05-29T14:53:19.820Z","firingRule":{"id":"0cf80f23-4fbe-4798-b29b-2cf46c843e77","name":"temperature monitoring firing rule"},"matchingContext":{"tenantId":"5a6084e091b5f1166ba1016b","timestamp":"2024-05-29T14:53:19.673Z","matchingRule":{"id":"e55a4c01-edaa-4b6f-904f-1233c03b65e4","name":"freezer matching rule"},"data":{"type":"dataMessage","version":1,"streamId":"urn:lo:nsid:mqtt:freezer_1","timestamp":"2024-05-29T14:53:19.510Z","value":{"temp":0},"tags":["freezer"],"extra":{"physicaltarget":"mytarget"},"metadata":{"source":"urn:lo:nsid:mqtt:freezer_1","group":{"id":"root","path":"/"},"connector":"mqtt","network":{"mqtt":{"clientId":"freezer_1"}}},"created":"2024-05-29T14:53:19.510Z"}}}

How to go further

In the next article we will play with the “context” API:

  • We will monitor devices with one event processing rule.
  • Each device will have its own alerting temperature settings.

Was this article helpful?

Yes No

Thank you for your input, feel free to leave a comment below to explain your choice.

Try Live Objects for free

Get a free account limited up to 10 devices and do not hesitate to contact us to tell us about your project.

Create a LoRa® discover accountCreate an other offer discover account