How to store command execution results in device messages?

Article explains how to automatically store device command execution results in device messages

This article explains how to automatically store device command execution results in device messages. This approach can be useful for:

  • Maintaining a complete history of device remote management activity

  • Analyzing command execution patterns and success rates

  • Integrating command execution logs into your reporting system

  • Accessing command execution history via standard message processing tools (plugins, calculators)

  • Forwarding command execution logs via streams subscribed to the device

We will use a webhook to catch command execution logs and store them as device messages. The webhook will:

  1. Listen to command execution logs

  2. Format log data into a message

  3. Post this message back to the device

Configuration

Create a new webhook with the following configuration:

{
    "name": "Store command execution results",
    "triggers": [
        {
            "topic": "flespi/log/gw/devices/+/commands-queue/+/+"
        }
    ],
    "configuration": {
        "type": "flespi-platform",
        "method": "POST",
        "uri": "/gw/devices/%topics[4]%/messages",
        "body": "[{\"timestamp\":%timestamp%,\"message.type\":\"command.execution\",\"command.name\":\"%topics[6]%\",\"command.status\":\"%topics[7]%\",\"command.response\":%if(isstring(json(payload, \"/response\")), '\"' + strescape(json(payload, \"/response\")) + '\"', json(payload, \"/response\"))%,\"command.executed\":%json(payload, \"/executed\")%}]"
    }
}

The configuration explained:

  • Trigger topic flespi/log/gw/devices/+/commands-queue/+/+ catches all command execution logs

  • URI /gw/devices/%topics[4]%/messages posts message to the device that executed the command

  • Message contains:

    • message.type - identifies message as command execution log

    • command.name - name of executed command

    • command.status - execution status (processed, queued, sent, canceled, expired)

    • command.response - command execution response (if available)

    • command.executed - command execution result (if available)

Usage

  1. Create webhook with the configuration above

  2. Execute any command on your device

  3. Command execution result will be automatically stored as a device message

The device message will look like:

{
    "timestamp": 1621234567,
    "message.type": "command.execution",
    "command.name": "custom",
    "command.status": "processed",
    "command.response": "OK",
    "command.executed": true,
    "rest.timestamp": 1621234568
}

For each queued and successfully executed command you will receive 3 messages: queued, sent, processed. You may limit this to just processed commands by using "flespi/log/gw/devices/+/commands-queue/+/processed" topic in webhook configuration.

You can access these messages via:

  • REST API - to fetch command execution history

  • MQTT API - to receive real-time command execution logs

  • Streams - to forward command execution logs to external platforms

  • Plugins - to process or analyze command execution patterns

  • Calculators - to generate reports on command execution statistics

Examples

Here are some examples of how you can use command execution messages:

  1. Calculate daily command execution statistics using calculator:

"counters": [
{
    "type": "expression",
    "name": "commands_total",
    "expression": "message.type == 'command.execution'",
    "method": "summary"
}
]
  1. Forward only successful command executions using stream:

{
    "validate_message": "message.type == 'command.execution' && command.executed == true"
}
  1. Filter command execution messages in REST API:

GET /gw/devices/{device-id}/messages?data={"filter":"message.type=='command.execution'"}

This way you can maintain a complete history of device remote management activity and integrate it into your reporting and analysis workflows.


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