MQTT Binary Sensor stays "off"

Hello,

so I’ve browsed the forum and tried about 10 different suggestion on how to fiy my problem but the problem stays.

My Sensor always shows “Off”
The Payload looks like this: {"value":true,"timestamp":"2022-02-02T09:58:07Z"}

MY latest Config experiment ist:

  - platform: mqtt
    state_topic: "xxx/attributes/xxxxxxxxx/value"
    name: "Motion"
    value_template: "{{ 'ON' if value_json.value == 'true' else 'OFF' }}"

I’ve also tried following the docs, tried comparing value_template to paylload_on and off, made sure not to set up avaliability since this sensor does not give me avaliability status as mqtt.
If I configure it as ‘sensor’ it returns the proper string “True” and “False”

This shows the same sensor, first is configured as Sensor second as Binary Sensor.
image

I need it to be configured as binary sensor so I can set the device class, otherwise the changes in state don’t show i.e. in the Picture Card.

Checked the Log, there is no indication what I’m doing wrong.

Any suggestion would be appriciated.

Thank you!

Change this:

To this:

value_template: "{{ value_json['value'] }}"

The word value must be included in square bracket notation as it has a similar meaning to value_json. It means “all the incoming data”, just not parsed as json and without the square brackets will not be interpreted as the key “value” in the json data. See: https://www.home-assistant.io/docs/configuration/templating/#processing-incoming-data

Also binary sensor templates should return true or false, not ON or OFF.

Thank you, but the sensor still only shows Off.

Do you have the state topic correct?

Is Home Assistant connected to the MQTT broker correctly?

Is the MQTT broker receiving the data and is the data what you have shown?

You can use MQTT explorer to check that last one.

Yes, I’ve checked all that.

All other MQTT Sensors are working, checked witht he MQTT Broker it listens to the topics and recieves them just as shown.

I’m assuming it is connected to the MQTT broker correctly as it’s showing the values it should if I configure it as sensor not as binary sensor.

Oh! Okay so i checked this code in my Template Editor and my return type is shown as string.
Let’s see where I can go from here, thank you

Edit: never mind my mistake it is shown as booloan, so why isn’t it working
As far as I understand binary_sensors need boolean values don’t they?

Yes they do. So, true/false, or on/off but not 'ON'/'OFF' or 'true'/'false' those are strings.

EDIT:

Try this:

value_template: "{{ value_json['value'] == 'true' }}"

Maybe your data is returning a string ('true') rather than a boolean (true).

1 Like

Oh my god. I feel so incredibly stupid. So sorry and thank you!!!

Edit:

All I had to do is set payload_on = true not = ‘true’.
You can’t imagine how stupid I feel right now.

Ah, you didn’t show a payload config. And I forgot he default payloads were ‘OFF’ and ‘ON’.

Oh yeah it was something I had tried earlier but after looking through the forums I removed it because it was removed in so many posts. After you showed me the template editor I tried it again but stupid me wrote ‘true’ instead of true. I was having HA compare a string to a boolean. Could have waited all night…

1 Like