Skip to content

Commit ca14c5d

Browse files
committed
Document how to define measurement units
Signed-off-by: Didier Wenzek <[email protected]>
1 parent 94058ff commit ca14c5d

File tree

2 files changed

+108
-6
lines changed

2 files changed

+108
-6
lines changed

docs/src/references/mappers/c8y-mapper.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,82 @@ c8y/measurement/measurements/create
378378

379379
</div>
380380

381+
### Measurement units
382+
383+
The Cumulocity mapper uses the measurement meta topics to attach units to measurement values.
384+
385+
The metadata for a measurement topic:
386+
387+
```mermaid
388+
graph LR
389+
te --/--- entity_id["&lt;entity id&gt;"] --/--- m --/--- measurement_type["&lt;measurement type&gt;"]
390+
```
391+
392+
are to be published to the associated meta topic:
393+
394+
```mermaid
395+
graph LR
396+
te --/--- entity_id["&lt;entity id&gt;"] --/--- m --/--- measurement_type["&lt;measurement type&gt;"] --/--- meta
397+
```
398+
399+
The idea is to describe in a single metadata message all the units of the measurements published under the measurement topic.
400+
This message uses the same shape as the measurements and is published as retained on the meta topic of the measurement topic.
401+
402+
```shell
403+
tedge mqtt pub -r 'te/device/main///m//meta' '{
404+
"Climate":{
405+
"Temperature": {"unit": "°C"},
406+
"Humidity": {"unit": "%RH"}
407+
},
408+
"Acceleration":{
409+
"X-Axis": {"unit": "m/s²"},
410+
"Y-Axis": {"unit": "m/s²"},
411+
"Z-Axis": {"unit": "m/s²"}
412+
}
413+
}'
414+
```
415+
416+
The measurements published to the topic for measurements of the same type:
417+
418+
```shell
419+
tedge mqtt pub 'te/device/main///m/' '{
420+
"Climate":{
421+
"Temperature":23.4,
422+
"Humidity":95.0
423+
},
424+
"Acceleration":{
425+
"X-Axis":0.002,
426+
"Y-Axis":0.015,
427+
"Z-Axis":5.0
428+
}
429+
}'
430+
```
431+
432+
are then forwarded to C8Y with their units.
433+
434+
```json
435+
{
436+
"type": "ThinEdgeMeasurement",
437+
"Climate": {
438+
"Temperature": {"value":23.4,"unit":"°C"},
439+
"Humidity":{"value":95,"unit":"%RH"}
440+
},
441+
"Acceleration": {
442+
"X-Axis": {"value":0.002,"unit":"m/s²"},
443+
"Y-Axis": {"value":0.015,"unit":"m/s²"},
444+
"Z-Axis": {"value":5,"unit":"m/s²"}
445+
},
446+
"time":"2025-09-03T10:05:47.226Z"
447+
}
448+
```
449+
450+
- A message received on `te/device/main///m/<type>` uses the units defined on `te/device/main///m/<type>/meta`, if any.
451+
- If the unit for a measurement is unknown, the measurement value is simply sent with no unit.
452+
- Other metadata such as the precision or the min and max values can be attached to a measurement.
453+
However, these are ignored by the Cumulocity mapper.
454+
- `"Temperature"`, `"Climate": { "Temperature" }` and `"Engine": { "Temperature" }` can be given different units.
455+
- Units and all measurement metadata can be cleared by publishing an empty retained message on `te/device/main///m/<type>/meta`.
456+
381457
### Events
382458

383459
<div class="code-indent-left">

docs/src/references/mqtt-api.md

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -506,15 +506,41 @@ can be updated by publishing the following message:
506506

507507
```sh te2mqtt formats=v1
508508
tedge mqtt pub -r te/device/main///m/battery_reading/meta '{
509-
"units": {
510-
"temperature": "°C",
511-
"voltage": "V",
512-
"current": "A"
513-
}
509+
"temperature": { "unit": "°C", "precision": "±0.1°C" },
510+
"voltage": { "unit": "V", "min": 0, "max": 20 },
511+
"current": { "unit": "A" }
514512
}'
515513
```
516514

517-
The metadata fields supported by each data type will be defined in detail later.
515+
These free-form metadata fields are used to interpret the values published on the associated topic.
516+
517+
Continuing the example with the following measurement:
518+
519+
```sh te2mqtt formats=v1
520+
tedge mqtt pub te/device/main///m/battery_reading '{
521+
"temperature": 25,
522+
"voltage": 20,
523+
"current": 5,
524+
"ttl": 1
525+
}'
526+
```
527+
528+
The metadata for *battery_reading* are used by the Cumulocity mapper, to attach units to these values:
529+
530+
```json
531+
{
532+
"temperature": {"temperature":{"value":25.0,"unit":"°C"}},
533+
"voltage":{"voltage":{"value":20.0,"unit":"V"}},
534+
"current":{"current":{"value":5.0,"unit":"A"}},
535+
"ttl":{"ttl":{"value":1.0}},
536+
"time":"2025-09-05T13:47:23.818993647Z",
537+
"type":"battery_reading"
538+
}
539+
```
540+
541+
Note that:
542+
- Metadata can be missing. The Cumulocity mapper doesn't mandate units and publishes the *ttl* value even if no units is known.
543+
- Extra metadata can be added. The Cumulocity mapper only uses the units and ignores all other metadata fields.
518544

519545
## Twin metadata
520546

0 commit comments

Comments
 (0)