[Wiki] MQTT Hub - Using templates

This topic is for demonstrating different ways of using value templates with the [APP][Pro] MQTT Hub - Community version.

Templates can be used for

  • valueTemplate - parsing the incoming meassage from a topic
  • outputTemplate - preparing the message to be sent to a topic.

valueTemplate

Value

When the topic returns a direct value, you can leave the value template empty.
Simple math can be used on these values. Could be used to change the multiplication factor for different units.

value / 10
value * 1000

JSONpath

When the message is a JSON, you will need to find the desired path to the value in the JSON.

JSON from topic example

{
  "state": {
    "report": {
      "number_value": 1000,
      "boolean_value": true,
      "string_value": "Some string"
    }
  }
}

Respective JSON paths would be then

$.state.report.number_value
$.state.report.boolean_value
$.state.report.string_value

equally same

$.state.report['number_value']
$.state.report['boolean_value']
$.state.report['string_value']

The later is useful when the keys used are numeric values, or contains spaces or other special characters.

JS function

Get a part of a string value

The received JSON string has two values for latitude and longitude separated by a comma. To evaluate the value from:

{
  "state": {
    "reported": {
      "ts": 1759810164000,
      "latlng": "60.234692,24.690582"
    }
  }
}

the configuration on the device could then have

  "measure_number": {
    "capability": "measure_number",
    "stateTopic": "topic/data",
    "setTopic": "",
    "valueTemplate": "(value) => Number(value.state.reported.latlng.split(',')[0])",
    "outputTemplate": "",
    "displayName": "Latitude"
  },
  "measure_number.1": {
    "capability": "measure_number",
    "stateTopic": "topic/data",
    "setTopic": "",
    "valueTemplate": "(value) => Number(value.state.reported.latlng.split(',')[1])",
    "outputTemplate": "",
    "displayName": "Longitude"
  },

Skip value update when attribute is not available in the received JSON

Some times updates to the selected topic may have differences in the JSON payload and some attribute might not be present, To skip updating the capability, you can use JS function to throw an error when the attribute was not present.

In my case the the attribute ‘389’ reflecting the Odometer of a vehicle is only sent when the is a change to the value, otherwise is not present.

{
  "state": {
    "reported": {
      "ts": 1759810164000,
      "389": 352056,
      "390": 442,
    }
  }
}

Check for presence of the attribute (if undefined). If present, return the value otherwise throw an error so that MQTT Hub device will ignore the update.

  "measure_number.4": {
    "capability": "measure_number",
    "stateTopic": "topic/data",
    "setTopic": "",
    "valueTemplate": "(value) => { if (value.state.reported['389'] === undefined) throw Error(); return value.state.reported['389'] }",
    "outputTemplate": "",
    "displayName": "Odometer km",
    "units": "km"
  },

It is also possible to do math on values before returning them:

  "measure_content_volume": {
    "capability": "measure_content_volume",
    "stateTopic": "ajovalo/fmc003/864275078490771/data",
    "setTopic": "",
    "valueTemplate": "(value) => { if (value.state.reported['390'] === undefined) throw Error(); return value.state.reported['390'] / 10 }",
    "outputTemplate": "",
    "displayName": "Fuel remaining"
  }

Convert timestamp to human readable

Received JSON

{
  "state": {
    "reported": {
      "ts": 1759810164000,
      "389": 352056,
      "390": 442,
    }
  }
}

This one converts to local time Helsinki and sv-SE local happens to have the date time string in ISO format YYYY-MM-dd HH:mm:ss I wanted.

  "measure_string.1": {
    "capability": "measure_string",
    "stateTopic": "topic/data",
    "setTopic": "",
    "valueTemplate": "(value) => new Date(value.state.reported['ts']).toLocaleString('sv-SE', { timeZone: 'Europe/Helsinki' })",
    "outputTemplate": "",
    "displayName": "Last report time"
  },

outputTemplate

Value

JS function

4 Likes

Edit the above Wiki article to document ways of using templates with the [APP][Pro] MQTT Hub - Community version.

Editing and creating wiki posts - Using Discourse - Discourse Meta