How to set up notifications about device message parameters change?

The guide on how to set up a calculator to receive live customizable MQTT messages when device parameters change according to a complex condition.

Consider the following task: you need to receive a live customizable notification when some of the device parameters change according to a complex condition. E.g. receive MQTT message when vehicle engine status changes based on engine.ignition.status parameter. Although you can write the change event directly in device message using plugin, calculator does not depend on the messages reception order, automatically handles historical messages and provides you with much more flexibility especially when you need to access the data from multiple messages. For example you can ignore short ignition state changes or attach to the event message when ignition has been started and when it was stopped.

In this article we consider how to achieve this using the flespi analytics engine in 3 basic steps:

  1. Create a calculator
  2. Assign an appropriate device to the calculator
  3. Subscribe to the calculator’s special topic

We assume that we already have a registered device getting a constant message inflow with engine.ignition.status parameter.

Step 1. Create a calculator

You may find calculators in the Telematics Hub section of the flespi panel. Pay attention to the following details:

  • selector’s type is an “engine.ignition.status” expression with "change" method. Such a selector will generate a new interval whenever expression evaluation result changing - e.g. when engine is turned on or turned off. Enable the "merge_unknown" option so that messages without engine.ignition.status parameter do not cause the active interval to end. And enable "merge_message_after" option to add to the interval one extra message that changed the state.
  • add a “expression” type counter named "ignition" with "engine.ignition.status" as expression and "first" method. This counter will contain information on the type of interval - either it is for ignition ON or OFF.
  • add two counters of “message” type - one for the "first" message and another for the "last" message. These counters will contain full message information when the ignition state was changed including vehicle position and time when ignition was switched on or off.

You can copy full JSON representation of this calculator below:

{
  "name": "engine ignition change",
  "selectors": [
    {
      "expression": "engine.ignition.status",
      "merge_message_after": true,
      "merge_unknown": true,
      "method": "change",
      "type": "expression"
    }
  ],
  "counters": [
    {
      "expression": "engine.ignition.status",
      "method": "first",
      "name": "ignition",
      "type": "expression"
    },
    {
      "method": "first",
      "name": "first",
      "type": "message"
    },
    {
      "method": "last",
      "name": "last",
      "type": "message"
    }
  ]
}

This JSON can be imported into your calculator's configuration directly by clicking on the orange curly brackets button when you edit or create a calculator.

Step 2. Assign a device to the calculator

To assign a device to the calculator go to the device’s CALCS tab, click on ‘+’ sign and select the appropriate calculator. New calculator card will appear.

Step 3. Get notification message

The calculator will automatically analyze the incoming messages and generate the message according to calculator settings to the MQTT topics:

  • flespi/interval/gw/calcs/{calculator-id}/devices/{device-id}/activated - when ignition state just changed, with JSON information about new current state
  • flespi/interval/gw/calcs/{calculator-id}/devices/{device-id}/deactivated - when ignition state just changed, with JSON information about previous state

MQTT message payload will be interval JSON with next fields (plus some other standard fields):

  • begin - time, when interval started (new state started)
  • end - time, when this interval finished (valid only for deactivated event)
  • ignition - type of interval, true if this for engine ignition ON or false for engine ignition off status
  • first - full device message JSON when interval has been started
  • last - full device message JSON when interval has been started

You can separately messages when ignition was activated or deactivated by using different subscription topics or MQTT topic filtering. Also events from intervals can be forwarded to your server using webhooks.


See also
How to add data generated by calculators to device messages