# Trigger custom operation via SmartRest (not thinedge in the agent code)

**URL:** <https://community.cumulocity.com/t/trigger-custom-operation-via-smartrest-not-thinedge-in-the-agent-code/7785>\
**Category:** Forum\
**Tags:** cumulocity\
**Created:** [January 23, 2024, 6:59pm UTC](https://community.cumulocity.com/t/trigger-custom-operation-via-smartrest-not-thinedge-in-the-agent-code/7785 "2024-01-23T18:59:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![nageshrao.eate](https://avatars.discourse-cdn.com/v4/letter/n/4da419/32.png) [@nageshrao.eate](https://community.cumulocity.com/u/nageshrao.eate)\
**Post date:** [January 23, 2024, 6:59pm UTC](https://community.cumulocity.com/t/trigger-custom-operation-via-smartrest-not-thinedge-in-the-agent-code/7785/1 "2024-01-23T18:59:19Z")

</div>

_ **Product/components** used and **version/fix** level:_  
Cumulocity, version/fix 10.11

_Detailed explanation of the problem:_  
Custom operations are created on the platform using REST API.  
Agent/device agent is using MQTT and is subscribed on topic s/ds for notification.  
Out of box operations are getting triggered, but we have custom operation that is created by custom UI using webSDK (REST). Agent is not getting this trigger on topic s/ds because its a custom operation.

Is there a way the agent will get notificiation. Please note operation is created by custom UI using webSDK, but the agent needs to be notified on MQTT.

---

<div class="post-metadata">

**Author:** ![Cyril\_Poder](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/cyril_poder/32/17142_2.png) [@Cyril\_Poder](https://community.cumulocity.com/u/Cyril_Poder)\
**Post date:** [January 23, 2024, 7:39pm UTC](https://community.cumulocity.com/t/trigger-custom-operation-via-smartrest-not-thinedge-in-the-agent-code/7785/2 "2024-01-23T19:39:42Z")

</div>

Hi,

If you want to be notified through MQTT, you need to create a SmartREST template that will match your operation.  
The way SmartREST templates match an operation is rather simple, you just need something in your operation that helps differentiate it. Say you have an operation that looks like that:

```json
{
    "myCustomFragment": {
        "someData": "someValue"
    }
}

```

Then most probably you want to create a SmartREST template that will match “myCustomFragment” fragment.  
If you do that through the UI, just go to “Device Types” → “SmartREST templates” then create a new template.  
Be careful with the template ID as this will be part of the MQTT topic you’ll have to subscribe to (s/dc/\<template ID\>).  
Create a new response: response ID can be whatever you want, again be careful as it will be part of the message sent to you through MQTT so you know what operation it is.  
You can leave “base pattern” empty.  
Then, in patterns, simply add “myCustomFragment”, save and that’s it.

---

<div class="post-metadata">

**Author:** ![nageshrao.eate](https://avatars.discourse-cdn.com/v4/letter/n/4da419/32.png) [@nageshrao.eate](https://community.cumulocity.com/u/nageshrao.eate)\
**Post date:** [January 23, 2024, 8:52pm UTC](https://community.cumulocity.com/t/trigger-custom-operation-via-smartrest-not-thinedge-in-the-agent-code/7785/3 "2024-01-23T20:52:58Z")

</div>

Hi Cyril Poder,

Here is the custom operation  
{  
“deviceId”: “123456”,  
“description”: “Firmware cancel request”,  
“hr\_FirmwareCancellation”: {}  
}

 ![image](https://europe1.discourse-cdn.com/flex005/uploads/cumulocity/original/3X/7/3/734831b235e70ccde00e5da818a512c418a8e765.png)

and in MQTT box, I have subscribed to s/dc/hr\_FirmwareCancellation, but not getting anyting in MQTTbox.

Did I miss anything? Appreciate your help.

Regards,  
Nagesh.

---

<div class="post-metadata">

**Author:** ![Cyril\_Poder](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/cyril_poder/32/17142_2.png) [@Cyril\_Poder](https://community.cumulocity.com/u/Cyril_Poder)\
**Post date:** [January 23, 2024, 11:46pm UTC](https://community.cumulocity.com/t/trigger-custom-operation-via-smartrest-not-thinedge-in-the-agent-code/7785/4 "2024-01-23T23:46:59Z")

</div>

Could be several things:

- Check that the MQTT client id match the c8y\_Serial external id of the device. If the device has no such external id (possible if it was created through http API) then create one (in that case you should also change how devices are created and ensure they always have a c8y\_Serial external id)
- Check that the MQTT user is at least the owner of the device or has rights over it (like the admin users)

---

<div class="post-metadata">

**Author:** ![Stefan\_Witschel](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/stefan_witschel/32/31955_2.png) [@Stefan\_Witschel](https://community.cumulocity.com/u/Stefan_Witschel)\
**Post date:** [January 24, 2024, 9:26am UTC](https://community.cumulocity.com/t/trigger-custom-operation-via-smartrest-not-thinedge-in-the-agent-code/7785/5 "2024-01-24T09:26:30Z")

</div>

The condition in your response template is missing.

Just add `hr_FirmwareCancellation` there too otherwise no Message will be created.  
Patterns describe which information will be added to that message in the order you have defined it. In your case just nothing as `hr_FirmwareCancellation` is an empty object. So the message will basically look like this: `1300,`

As Cyril stated it is important that the the owner of the device matches the user used in MQTTBox. Also the ClientID must match the c8y\_Serial.  
For a simple test just do the following:

1. Create a new device using MQTTBox `100,TestDevice`
2. Subscribe to `s/dc/hr_FirmwareCancellation` in MQTTBox.
3. Create the Operation (Postman or UI etc.)
4. You should have received you message `1300` with the properties you have defined.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/system/32/31990_2.png) [@system](https://community.cumulocity.com/u/system)\
**Post date:** [July 22, 2024, 9:26am UTC](https://community.cumulocity.com/t/trigger-custom-operation-via-smartrest-not-thinedge-in-the-agent-code/7785/6 "2024-07-22T09:26:38Z")

</div>

Closed on July 22, 2024, 9:26am UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
