Trouble with MQTT Binary Sensor

I have a pool controller which I have connected to a raspberry pi running nodejs-poolController. The njsPC is connected to my MQTT broker and publishes a topic that indicates the status of my automatic pool cover. I’m trying to set up a binary sensor that reports the cover status in HA. Here is what I have in my configuration.yaml:

binary_sensor:
  - platform: mqtt
    name: Pool Cover Sensor
    icon: mdi:window-shutter
    state_topic: intellicenter-i8ps/state/covers/1/cover1
    payload_on: '{"id":1,"isClosed":"true"}'
    payload_off: '{"id":1,"isClosed":"false"}'
    value_template: "{{ value_json.cover1 }}"
    unique_id: 10fc753b-f65b-4dfa-b790-33e173a107ea

But no matter what the actual payload on that topic is, I can’t get the binary sensor to reflect the change. It just always shows “off”. even where I can use MQTT Explorer to verify the payload for the topic is currently {“id”:1,“isClosed”:“true”}

I don’t really under stand templating, and I’m just mimicking what I’ve done for other MQTT sensors and switches, so maybe there’s something wrong with my value_template?

Looks like your value_template is bogus. Try removing it.

Well that’s embarrassing. I swear I tried it before without the value_template… Removing it fixed the issue.

For context, “value_template” extracts from the MQTT message the value the payload_* will be compared to. In your case, you compare the full payload so it’s unnecessary.

So if I wanted to only read the “isClosed” value from the payload, I would use the value_template? If yes, what would be the syntax to do that? Something like this?

    state_topic: intellicenter-i8ps/state/covers/1/cover1
    payload_on: "true"
    payload_off: "false"
    value_template: "{{ value_json.cover1.isClosed }}"

Yes, you get the correct idea :+1:

In you case, there is no “cover1” in the json, though, so you’d have to remove that. “value_json” points to the root of the json.

Thanks, that helps a lot!

Or like this:

    state_topic: intellicenter-i8ps/state/covers/1/cover1
    value_template: "{{ 'ON' if value_json.cover1.isClosed == 'true' else 'OFF' }}"

This assumes true is received as a string value. If it’s received as a boolean value then there’s no need to compare isClosed to 'true'.