Parsing mqtt topic for different devices

Please help correct parse Mqtt topic?

I have in one topic response from diffrent devices:

{
"applicationID": 1,
"devEUI": "24e124136b323654",
"deviceName": "EM300-TH-868M",
"humidity": 91,
"temperature": -7.3
}

{
"applicationID": 1,
"battery": 93,
"co2": 1027,
"devEUI": "24e124707b42xxx7",
"deviceName": "AM307-868M",
"humidity": 38.5,
"light_level": 0,
"pir": 0,
"pressure": 1012.4,
"temperature": 24.1,
"tvoc": 1.4
}

below HA configuration.yaml:

> mqtt:
>     sensor:
>     - name: "EM300-TH-868M Temperature"
>       state_topic: "milesight/uplink"
>       value_template: "{{ value_json.temperature }}"
>       unique_id: 24e124136bx32654
>       unit_of_measurement: "°C"

How can i get correct response for diffrent devices in string:
value_template: “{{ value_json.temperature }}”?

Hello Vitaly-adm,

Thanks for coming here and asking a question and welcome to the HA forum.

Would you be so kind as to adjusting the format of your code so that we can read it properly & check the YAML spacing, etc. Editing your original is the preferred way. It is very hard for us to tell what is what when the text formatter jumbles everything like that.

Use the </> button or this:
Here is an example of how to fix it from the site FAQ Page.
How to help us help you - or How to ask a good question.

You will need to use the solution I presented here:

      value_template: >
        {% if value_json is defined and value_json.deviceName == EM300-TH-868M %}
          {{ value_json.temperature }}
        {% else %}
          {{ states('sensor.EM300-TH-868M_Temperature') }}
        {% endif %}

or possibly using " "

      value_template: >
        {% if value_json is defined and value_json.deviceName == "EM300-TH-868M" %}
          {{ value_json.temperature }}
        {% else %}
          {{ states('sensor.EM300-TH-868M_Temperature') }}
        {% endif %}

You could try this without having to add automations

Thank Spiro!

With " " i have got correct data.

How many sensors use the same MQTT topic? Only two?

When I use 3 sensors, one of them (sensor.EM500-SMTC-868M_Greenhouse_temperature) collects data from itself and the other two.
Probably because this sensor has a problem with the correct display of negative temperature. The humidity for all three sensors is displayed correctly. Below is a JSON string for.EM500-SMTC-868M_Greenhouse_temperature:

{“applicationID”:1,“devEUI”:“24e124126b423935”,“deviceName”:“EM500-SMTC-868M”,“ec”:0,“moisture”:3.18,“temperature”:649}

    sensor:
    - name: "EM300-TH-868M Outdoor Temperature"
      state_topic: "milesight/uplink"
      value_template: >
        {% if value_json is defined and value_json.devEUI == "24e124136b323654" %}
           {{ value_json.temperature }}
        {% else %}
          {{ states('sensor.EM300-TH-868M_Outdoor_Temperature') }}
        {% endif %}
      unique_id: "EM300-TH-868M Outdoor Temperature"
      unit_of_measurement: "°C"
      

    - name: "EM500-SMTC-868M Greenhouse temperature"
      state_topic: "milesight/uplink"
      value_template: >
         {% if value_json is defined and value_json.devEUI == "24e124126b423935", %}
          {{ value_json.temperature }}
         {% else %}
          {{ states('sensor.EM500-SMTC-868M_Greenhouse_temperature') }}
         {% endif %}
      unique_id: "EM500-SMTC-868M_Greenhouse_temperature" 
      unit_of_measurement: "°C"


    - name: "AM307-868M Kitchen Temperature"
      state_topic: "milesight/uplink"
      value_template: >
        {% if value_json is defined and value_json.devEUI == "24e124707b429517" %}
           {{ value_json.temperature }}
        {% else %}
          {{ states('sensor.AM307-868M_Kitchen_temperature') }}
        {% endif %}
      unique_id: "AM307-868M Kitchen Temperature"
      unit_of_measurement: "°C"

The reason why I asked how many sensors are involved is because the suggested technique doesn’t scale well.

Imagine six sensors where each one is subscribed to the same MQTT topic. When a payload is published to the topic, all six sensors process the payload but it contains data for just one of them. That’s not very efficient.

When many sensors are involved, the more efficient technique is to use an automation to “demultiplex” the payload. In other words, the automation is subscribed to the MQTT topic (not the sensors). When it receives a payload it determines which sensor should receive it then re-publishes it to a different MQTT topic that is unique to each of the six sensors. This is more efficient because each payload only gets processed twice (once by the demultiplexing automation and then by the appropriate sensor) as opposed to six times.

Dear Taras!

Thanx for your solution. Could you help me with code?

alias: MQTT demultipler
description: ""
triggers:
  - trigger: mqtt
    topic: milesight/uplink
    payload: payload_json.temperature
conditions: []
actions:
  - action: mqtt.publish
    metadata: {}
    data:
      retain: true
      topic: lora
      payload: "{{ trigger.payload_json.temperature['Value'] }}"

Here is the demultiplexer automation. The received temperature value is re-published to a new MQTT topic that contains the device’s devEUI.

For example, if it receives this payload:

{
"applicationID": 1,
"devEUI": "24e124136b323654",
"deviceName": "EM300-TH-868M",
"humidity": 91,
"temperature": -7.3
}

It will publish -7.3 to lora/temp/24e124136b323654

alias: MQTT demultiplexer
description: ""
triggers:
  - trigger: mqtt
    topic: milesight/uplink
conditions: []
actions:
  - action: mqtt.publish
    metadata: {}
    data:
      retain: true
      topic: "lora/temp/{{ trigger.payload_json.devEUI }}"
      payload: "{{ trigger.payload_json.temperature }}"
mode: queued

Your sensor configurations should be changed to this:

  sensor:
    - name: "EM300-TH-868M Outdoor Temperature"
      state_topic: "lora/temp/24e124136b323654"
      value_template: "{{ value }}"
      unique_id: "EM300-TH-868M Outdoor Temperature"
      unit_of_measurement: "°C"
      

    - name: "EM500-SMTC-868M Greenhouse temperature"
      state_topic: "lora/temp/24e124126b423935"
      value_template: "{{ value }}"
      unique_id: "EM500-SMTC-868M_Greenhouse_temperature" 
      unit_of_measurement: "°C"


    - name: "AM307-868M Kitchen Temperature"
      state_topic: "lora/temp/24e124707b429517"
      value_template: "{{ value }}"
      unique_id: "AM307-868M Kitchen Temperature"
      unit_of_measurement: "°C"
1 Like

Thank. Its best solution!

1 Like