MQTT binary sensor from JSON

Trying to create a binary sensor from JSON MQTT but cannot get it to work. I want to use the “active” and turn the switch on/off by “0” and “1”.
The JSON looks like this:

{"timestamp": 1588945675722, "topic": "test:something/to/measure", "message": {"source": {}, "key": {}, "data": {"active": "0"}}}

My sensor:

- platform: mqtt
name:test
state_topic: "test:something/to/measure" 
value_template: "{{ value_json[message][active] }}""
payload_on: "1"
payload_off: "0"
device_class: motion

I do not understand how to get to the “active” from the nested JSON file.

Please read this topic and format your code correctly.

First you want to create a binary sensor , not a switch, so you need to use a MQTT binary sensor

The value template should look like this:

value_template: "{{ value_json.message.data.active }}"

Thanks. I have tried to update my post with correct formatting and changed to binary sensor. I also updated as per your proposal, but still no luck.

sensor:
  - platform: mqtt
    name: test
    state_topic: "test:something/to/measure" 
    value_template: "{{ value_json.message.data.active }}"
    payload_on: "1"
    payload_off: "0"
    device_class: motion

Here is my JSON:

{
  "timestamp": 1588945675722,
  "topic": "test:something/to/measure",
  "message": {
    "source": {},
    "key": {},
    "data": {
      "active": "0"
    }
  }
}

Go to developer tools, enter # under topic to subscribe to and click ‘start listening’

image

It is not because your json contains

"topic": "test:something/to/measure"

that that is the actual topic to listen to.

OK. Here is what I get. I want to create a sensor that turns “on” when message/data/active goes from “0” to “1”.

{
    "timestamp": 1589011166387,
    "topic": "test:something/to/measure",
    "message": {
        "source": {},
        "key": {},
        "data": {
            "active": "1"
        }
    }
}

That is not what I asked. I want to see what you did receive in HA. Something like :

Message 118 received on homeassistant/sensor/xiron_3201/humidity/config at 10:08 AM:

{
    "unit_of_measurement": "%",
    "device_class": "humidity",
    "value_template": "{{ value_json.HUM }}",
    "state_topic": "rflink/Xiron-3201",
    "name": "eetkamer_humidity",
    "unique_id": "humidity_huiskamer",
    "device": {
        "identifiers": [
            "xiron_3201"
        ],
        "name": "xiron_3201",
        "model": "Digoo temp & humidity sensor",
        "manufacturer": "Digoo"
    }
}

QoS: 0 - Retain: true 

The important line to troubleshoot is this :

Message 654 received on electricity2/tele/RESULT at 10:11 AM:

OK, I think I understand. Here is the full log from HA:

Message 895 received on device1/event/abc:test/something/to/measure at 10:27 :
{
    "timestamp": 1589012870339,
    "topic": "test:something/to/measure",
    "message": {
        "source": {},
        "key": {},
        "data": {
            "active": "1"
        }
    }
}
QoS: 0 - Retain: false

Then your sensor needs to be :

sensor:
  - platform: mqtt
    name: test
    state_topic: "device1/event/abc:test/something/to/measure" 
    value_template: "{{ value_json.message.data.active }}"
    payload_on: "1"
    payload_off: "0"
    device_class: motion

2 Likes

Absolutely brilliant!! Works perfectly. So the MQTT topic is what shows in the header from the dev panel. I was confused by the “topic” in the JSON code. Thank you very much!

HI! I am new to home assistant and was looking at this example to practice. I wanted to know where exactly do I have to publish this JSON to change the state of the sensor.