IoTAgent JSON MQTT and timestamps attributes

60 Views Asked by At

I am trying to integrate MQTT devices with the FIWARE JSON IoT agent and I have some doubts about it.

The goal I want to achieve is to try to provision devices through DeviceMeasurement entities to follow the model proposed in SmartDataModels. This model proposes to store the timestamp of the measurement in the "dateObserved" attribute. In contrast, the NGSI-LD standard suggests storing the timestamp as metadata "observedAt" in the attribute itself (in this case, attribute "numValue"). Since I present historical repository implementations using dateObserved, I want to keep this attribute and also consider adding the same timestamp as metadata (observedAt) of the "numValue" attribute to make it compatible with Temporal API implementations such as Mintaka (which makes use of the "observedAt" metadata).

In short, I want to store the timestamp both in a "dateObserved" attribute and as "observedAt" metadata, so I want the entity to be displayed in this way:

 {
    "id": "urn:ngsi-ld:DeviceMeasurement:device1-temperature",
    "type": "DeviceMeasurement",
    "numValue": {
        "type": "Property",
        "value": 232.3,
        "unitCode": "CEL",
        "observedAt" : "2024-06-21T11:15:00.000Z"
    },
    "dateObserved": {
        "type": "Property",
        "value": {
            "@type": "DateTime",
           "@value": "2024-06-21T11:15:00.000Z"
        }
    },
    "refDevice": {
        "type": "Relationship",
        "object": "urn:ngsi-ld:Device:device1"
    },
    "controlledProperty": {
        "type": "Property",
        "value": "temperature"
    }
}

To try to do this, I provisioned the device like this:

{
            "device_id": "device1-temperature",
            "apikey": "apikey_test",
            "service": <service>,
            "service_path": "/",
            "entity_name": "urn:ngsi-ld:DeviceMeasurement:device1-temperature",
            "entity_type": "DeviceMeasurement",
            "timestamp": true,
            "transport": "MQTT",
            "attributes": [
                {
                    "object_id": "dateObserved",
                    "name": "dateObserved",
                    "type": "DateTime"
                },
                {
                    "object_id": "numValue",
                    "name": "numValue",
                    "type": "Property",
                    "metadata": {
                        "unitCode": {
                            "type": "Text",
                            "value": "CEL"
                        }
                    }
                }
            ],
            "lazy": [],
            "commands": [],
            "static_attributes": [
                {
                    "name": "refDevice",
                    "type": "Relationship",
                    "value": "urn:ngsi-ld:Device:device1"
                },
                {
                    "name": "controlledProperty",
                    "type": "Property",
                    "value": "temperature"
                }
            ],
            "protocol": "MQTT_JSON"
        }

and sending the following MQTT message:

 {"numValue":232.3,"dateObserved":"2024-06-21T11:30:00.000Z", "TimeInstant" : "2024-06-21T11:30:00.000Z" }

the entity shows the following:

{
    "id": "urn:ngsi-ld:DeviceMeasurement:device1-temperature",
    "type": "DeviceMeasurement",
    "numValue": {
        "type": "Property",
        "value": 232.3,
        "unitCode": "CEL",
        "observedAt": "2024-06-21T11:30:00.000Z"
    },
    "dateObserved": {
        "type": "Property",
        "value": {
            "@type": "DateTime",
            "@value": "2024-06-21T11:30:00.000Z"
        },
        "observedAt": "2024-06-21T11:30:00.000Z"
    },
    "refDevice": {
        "type": "Relationship",
        "object": "urn:ngsi-ld:Device:device1",
        "observedAt": "2024-06-21T11:30:00.000Z"
    },
    "controlledProperty": {
        "type": "Property",
        "value": "temperature",
        "observedAt": "2024-06-21T11:30:00.000Z"
    }
}

According to IoTAgent documentation, I have to include the timestamp as "TimeInstant" in the payload of the MQTT message, but this timestamp is added as metadata to all attributes (dynamic and static), when I only want to add it to numValue.

Is there any way to ensure that the "observedAt" metadata is added only to the desired attribute (dynamic) as is the case of "numValue"?

Thanks

1

There are 1 best solutions below

2
Perrolobo On

I can answer regarding IotAgent NGSIv2 Northbound binding. I'm almost sure it will behave the same than NGSI-LD binding.

I'm afraid the current behaviour is like you stated: if timestamp==true and a TimeInstant measure is present, then the value of the TimeInstant measure will be used as the value of the TimeInstant attribute and added as a metadata to every propagated attribute, in this case active and static attributes. As far as they have been progressed the metadata should make sense.

You may use the explicitAttrs and/or skipValue functionality in order to filter which attributes will be propagated but I think is not what you need in this case.

As an alternative there is one way in which we can send metadata as a part of a measure, by using the new NGSI payload support. In this way you are entitled to send the metadata as you expected in a NGSI payload (v2 or Ld), and set timestamp==false. I think this will make the metadata accessible to the final attribute.