How to get value from mqtt json to mqtt sensor?

I try to get battery and rssi value from there:

{“topic”:“home/hallway/climate-sensor-tem-hum-outside-unite”,“status”:{“rssi”:6,“battery”:9},“payload”:{“temperature”:{“value”:25.2,“unit”:“degC”},“humidity”:{“value”:57,“unit”:"%"}},"_msgid":“e4459774.686e88”}

I try like this but not working:

      - platform: mqtt    
        name: "mqtt_hallway_climate_sensor_bat_outside_unite"
        state_topic: "home/hallway/climate-sensor-tem-hum-outside-unite"
        qos: 0
        unit_of_measurement: ""
        value_template: "{{ value_json.battery }}"

      - platform: mqtt    
        name: "mqtt_hallway_climate_sensor_rssi_outside_unite"
        state_topic: "home/hallway/climate-sensor-tem-hum-outside-unite"
        qos: 0
        unit_of_measurement: ""
        value_template: "{{ value_json.rssi }}"

and

      - platform: mqtt    
        name: "mqtt_hallway_climate_sensor_bat_outside_unite"
        state_topic: "home/hallway/climate-sensor-tem-hum-outside-unite"
        qos: 0
        unit_of_measurement: ""
        value_template: "{{ value_json.status.battery }}"

      - platform: mqtt    
        name: "mqtt_hallway_climate_sensor_rssi_outside_unite"
        state_topic: "home/hallway/climate-sensor-tem-hum-outside-unite"
        qos: 0
        unit_of_measurement: ""
        value_template: "{{ value_json.status.rssi }}"
  1. Despite reading four variables you are always using the same name
name: "mqtt_hallway_climate_sensor_bat_outside_unite"

Make the name unique for each entity like

name: "mqtt_hallway_climate_sensor_bat_outside_unite_battery"
  1. Debug the MQTT broker from the command line with
mosquitto_sub -v -h [IP_of_MQTT_broker] -u [user_of_MQTT_broker] -P [password_of_MQTT_broker] -p 1883 -t 'home/hallway/climate-sensor-tem-hum-outside-unite'

and ensure that the message is published.

This is the correct value_template to extract the value of battery:

value_template: "{{ value_json.status.battery }}"

In addition, follow abalakov’s advice and make the name of each entity unique.

Tip:
Use the Template Editor to experiment with templates.

For example, paste this into the Template Editor:

{% set value_json = {"topic":"home/hallway/climate-sensor-tem-hum-outside-unite","status":{"rssi":6,"battery":9},"payload":{"temperature":{"value":25.2,"unit":"degC"},"humidity":{"value":57,"unit":"%"}},"_msgid":"e4459774.686e88"} %}

{{value_json.status.rssi}}
{{value_json.status.battery}}
{{value_json.payload.temperature.value}}
{{value_json.payload.humidity.value}}

The result will be this:
Screenshot%20from%202019-06-21%2010-36-52

5 Likes

I already but I get unknow for rssi and battery. Temperature and humidity is working correct.

      - platform: mqtt    
        name: "mqtt_hallway_climate_sensor_bat_outside_unite"
        state_topic: "home/hallway/climate-sensor-tem-hum-outside-unite"
        qos: 0
        unit_of_measurement: ""
        value_template: "{{ value_json.status.battery }}"

      - platform: mqtt    
        name: "mqtt_hallway_climate_sensor_rssi_outside_unite"
        state_topic: "home/hallway/climate-sensor-tem-hum-outside-unite"
        qos: 0
        unit_of_measurement: ""
        value_template: "{{ value_json.status.rssi }}"
  - platform: mqtt    
    name: "mqtt_hallway_climate_sensor_tem_outside_unite"
    state_topic: "home/hallway/climate-sensor-tem-hum-outside-unite"
    qos: 0
    unit_of_measurement: ""
    value_template: "{{ value_json.temperature.value }}"

  - platform: mqtt    
    name: "mqtt_hallway_climate_sensor_hum_outside_unite"
    state_topic: "home/hallway/climate-sensor-tem-hum-outside-unite"
    qos: 0
    unit_of_measurement: ""
    value_template: "{{ value_json.humidity.value }}

unknown_1

Do all received payloads contain this information: "status":{"rssi":6,"battery":9},

I get this from node-red but rssi and battery is not under payload?

unknown_3

here is my node red flow:

[
    {
        "id": "2e9ac976.0e4dd6",
        "type": "comment",
        "z": "7cd8e0dd.c9139",
        "name": "HODNIK - KLIMA TEMPERATURA/&VLAGA ZUNANJA ENOTA",
        "info": "",
        "x": 278,
        "y": 1439.5000400543213,
        "wires": []
    },
    {
        "id": "972120d9.c1467",
        "type": "debug",
        "z": "7cd8e0dd.c9139",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "x": 1037.5000305175781,
        "y": 1666.0000495910645,
        "wires": []
    },
    {
        "id": "7fc72096.eec4e",
        "type": "change",
        "z": "7cd8e0dd.c9139",
        "name": "climate.outside.temp&hum",
        "rules": [
            {
                "t": "set",
                "p": "topic",
                "pt": "msg",
                "to": "home/hallway/climate-sensor-tem-hum-outside-unite",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 542.0000457763672,
        "y": 1458.7500228881836,
        "wires": [
            [
                "ae82025e.94902",
                "972120d9.c1467"
            ]
        ]
    },
    {
        "id": "ae82025e.94902",
        "type": "mqtt out",
        "z": "7cd8e0dd.c9139",
        "name": "Publish MQTT Message",
        "topic": "",
        "qos": "0",
        "retain": "true",
        "broker": "2df91666.ec75ea",
        "x": 1420,
        "y": 1528.5,
        "wires": []
    },
    {
        "id": "a20b0177.f32bf",
        "type": "rfx-sensor",
        "z": "7cd8e0dd.c9139",
        "name": "",
        "port": "11472fa1.7c1d6",
        "topicSource": "all",
        "topic": "",
        "x": 140.00000381469727,
        "y": 1491.0000314712524,
        "wires": [
            [
                "972120d9.c1467",
                "9155bf9c.ff1ec"
            ]
        ]
    },
    {
        "id": "9155bf9c.ff1ec",
        "type": "switch",
        "z": "7cd8e0dd.c9139",
        "name": "",
        "property": "topic",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "TH10/0x4903",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 1,
        "x": 301.2500343322754,
        "y": 1474.1667709350586,
        "wires": [
            [
                "7fc72096.eec4e"
            ]
        ]
    },
    {
        "id": "2df91666.ec75ea",
        "type": "mqtt-broker",
        "z": "",
        "name": "MQTT Broker",
        "broker": "192.168.200.199",
        "port": "1883",
        "tls": "bcc42a56.a547d8",
        "clientid": "hassos-rpi3-nr-mqtt",
        "usetls": false,
        "compatmode": true,
        "keepalive": "60",
        "cleansession": true,
        "birthTopic": "",
        "birthQos": "0",
        "birthPayload": "",
        "closeTopic": "",
        "closePayload": "",
        "willTopic": "",
        "willQos": "0",
        "willPayload": ""
    },
    {
        "id": "11472fa1.7c1d6",
        "type": "rfxtrx-port",
        "z": "",
        "port": "/dev/ttyUSB0",
        "enableDebug": true,
        "rfyVenetianMode": "EU"
    },
    {
        "id": "bcc42a56.a547d8",
        "type": "tls-config",
        "z": "",
        "name": "",
        "cert": "fullchain.pem",
        "key": "privkey.pem",
        "ca": "",
        "certname": "",
        "keyname": "",
        "caname": "",
        "servername": "",
        "verifyservercert": false
    }
]

I’ve caused some confusion because of how I used the word ‘payload’. When I said ‘payload’ I meant the entire MQTT payload, meaning the entire MQTT message, not the JSON key with the same name ("payload") within the message.

Anyway, I think you answered my question. Node-Red’s debug output shows that the MQTT payload (message) always includes the status key with rssi and battery:

If it only included it occasionally, that could explain why the sensor’s value can be unknown. However, because status is always within the message, it doesn’t explain why the template "{{ value_json.status.rssi }}" is failing to extract it (and failing to extract battery). :man_shrugging:

Thanks @123

I added another change node:

[
    {
        "id": "2e9ac976.0e4dd6",
        "type": "comment",
        "z": "7cd8e0dd.c9139",
        "name": "HODNIK - KLIMA TEMPERATURA/&VLAGA ZUNANJA ENOTA",
        "info": "",
        "x": 269.42857360839844,
        "y": 1350.9285297393799,
        "wires": []
    },
    {
        "id": "972120d9.c1467",
        "type": "debug",
        "z": "7cd8e0dd.c9139",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "x": 1037.5000305175781,
        "y": 1666.0000495910645,
        "wires": []
    },
    {
        "id": "7fc72096.eec4e",
        "type": "change",
        "z": "7cd8e0dd.c9139",
        "name": "climate.outside.temp&hum",
        "rules": [
            {
                "t": "set",
                "p": "topic",
                "pt": "msg",
                "to": "home/hallway/climate-sensor-tem-hum-outside-unite",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 542.0000457763672,
        "y": 1458.7500228881836,
        "wires": [
            [
                "ae82025e.94902"
            ]
        ]
    },
    {
        "id": "ae82025e.94902",
        "type": "mqtt out",
        "z": "7cd8e0dd.c9139",
        "name": "Publish MQTT Message",
        "topic": "",
        "qos": "0",
        "retain": "true",
        "broker": "2df91666.ec75ea",
        "x": 1420,
        "y": 1528.5,
        "wires": []
    },
    {
        "id": "a20b0177.f32bf",
        "type": "rfx-sensor",
        "z": "7cd8e0dd.c9139",
        "name": "",
        "port": "11472fa1.7c1d6",
        "topicSource": "all",
        "topic": "",
        "x": 140.00000381469727,
        "y": 1491.0000314712524,
        "wires": [
            [
                "9155bf9c.ff1ec",
                "972120d9.c1467"
            ]
        ]
    },
    {
        "id": "9155bf9c.ff1ec",
        "type": "switch",
        "z": "7cd8e0dd.c9139",
        "name": "",
        "property": "topic",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "TH10/0x4903",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 1,
        "x": 301.2500343322754,
        "y": 1474.1667709350586,
        "wires": [
            [
                "f863d92e.1baea8",
                "7fc72096.eec4e"
            ]
        ]
    },
    {
        "id": "f863d92e.1baea8",
        "type": "change",
        "z": "7cd8e0dd.c9139",
        "name": "climate.outside.rssi&battery",
        "rules": [
            {
                "t": "set",
                "p": "topic",
                "pt": "msg",
                "to": "home/hallway/climate-sensor-rssi-bat-outside-unite",
                "tot": "str"
            },
            {
                "t": "move",
                "p": "status",
                "pt": "msg",
                "to": "payload",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 547,
        "y": 1398,
        "wires": [
            [
                "ae82025e.94902"
            ]
        ]
    },
    {
        "id": "685c28c1.6abfd8",
        "type": "mqtt in",
        "z": "7cd8e0dd.c9139",
        "name": "",
        "topic": "home/hallway/climate-sensor-tem-hum-outside-unite",
        "qos": "0",
        "datatype": "auto",
        "broker": "2df91666.ec75ea",
        "x": 274,
        "y": 1662,
        "wires": [
            [
                "972120d9.c1467"
            ]
        ]
    },
    {
        "id": "2df91666.ec75ea",
        "type": "mqtt-broker",
        "z": "",
        "name": "MQTT Broker",
        "broker": "192.168.200.199",
        "port": "1883",
        "tls": "bcc42a56.a547d8",
        "clientid": "hassos-rpi3-nr-mqtt",
        "usetls": false,
        "compatmode": true,
        "keepalive": "60",
        "cleansession": true,
        "birthTopic": "",
        "birthQos": "0",
        "birthPayload": "",
        "closeTopic": "",
        "closePayload": "",
        "willTopic": "",
        "willQos": "0",
        "willPayload": ""
    },
    {
        "id": "11472fa1.7c1d6",
        "type": "rfxtrx-port",
        "z": "",
        "port": "/dev/ttyUSB0",
        "enableDebug": true,
        "rfyVenetianMode": "EU"
    },
    {
        "id": "bcc42a56.a547d8",
        "type": "tls-config",
        "z": "",
        "name": "",
        "cert": "fullchain.pem",
        "key": "privkey.pem",
        "ca": "",
        "certname": "",
        "keyname": "",
        "caname": "",
        "servername": "",
        "verifyservercert": false
    }
]

and made some changes yaml:

# EXTERNAL SENSOR ON OUTSIDE UNITE
  - platform: mqtt    
    name: "mqtt_hallway_climate_sensor_bat_outside_unite"
    state_topic: "home/hallway/climate-sensor-rssi-bat-outside-unite"
    qos: 0
    unit_of_measurement: ""
    value_template: "{{ value_json.battery }}"

  - platform: mqtt    
    name: "mqtt_hallway_climate_sensor_rssi_outside_unite"
    state_topic: "home/hallway/climate-sensor-rssi-bat-outside-unite"
    qos: 0
    unit_of_measurement: ""
    value_template: "{{ value_json.rssi }}"

Now is working :smile:

short thank you for the tip. Was struggling with this and came accross your post.