1

This is my device inventory with the custom tasks array:

{
    ...
    "c8y_IsDevice": {},
    "tasks": [
        { 
            "task_status" : "NEW",
            "task_id" : "1",
            "task_data" : {
                ...
            }
        },
        { 
            "task_status" : "DONE",
            "task_id" : "2",
            "task_data" : {
                ...
            }
        },
            ...
    ]
    ...
}   

I want to create a MQTT/SMARTREST PUT template to update a task by id and status.

For example: 800,[task_id],[task_status]

I am not able to find a way for this, especially it's an json array and all my attempts end up in overwriting the complete json array. Maybe there's sth. like a condition, if task_id = x -> set task_status = y

Thank you.

Lexycon
  • 11
  • 4

3 Answers3

1

You can only replace the whole fragment. There is no way to partially modify fragments.

Georgi
  • 165
  • 1
  • 9
0

one way to do it is get the whole array, using it for create a new one locally with the changes to want to do and finally put it again into the database. It is not a kind of solution but have been working for me.

Jorge
  • 238
  • 1
  • 10
0

Thanks for the info, but I still got a question about updating an array. Concerning your answers I want to update the whole fragment.

This is my inventory:

   "tasks": [
        {
            "address": {
                "street": "Street",
                "street_number": "1"
            },
            "description": "Test Description",
            "id": "1",
            "status": "NEW"
        },
        {
            "address": {
                "street": "Street2",
                "street_number": "2"
            },
            "description": "Test Description 2",
            "id": "2",
            "status": "DONE"
        }
    ]

My template:

801,<$.tasks.status>,<$.tasks.description>,<$.tasks.address.street>,<$.tasks.address.street_number>

Template screenshot

Now I publish:

//801,SERIAL,status,description,street_name,street_nr
801,SERIAL,NEW,1,2,3,4

Of course, this will overwrite the array and just set a json object tasks.

"tasks": {
    "address": {
        "street": "2",
        "street_number": "3"
    },
    "description": "1",
    "status": "NEW"
}

So I tried tasks[*]/tasks[] in my template (like in response templates), but this won't work too. I don't get it, maybe you can give me a small solution about putting a complete fragment with an array inside.

Lexycon
  • 11
  • 4
  • This kind of syntax is not supported by smartrest templates. To accomplish this you will need to define a smartrest template for each possible number of tasks. – Georgi Dec 25 '17 at 20:33