Issue is binary_sensor and a Zigbee Agara door sensor returning a true or false value

HA Version: 2024.8.2
OS: Ubuntu 22.04
Agara Zigbee door sensor.
Mosquitto broker

I have an issue with using HA alsways showing an “off” state in my sensor template.

    - name: "CGC Door Contact"
      state_topic: 'zigbee2mqtt/0x00158d000ad62d3d'
      value_template:
            "{% if is_state('sensor.cgc_door_contact', 'on') %}
              CLOSED
            {% else %}
              OPEN
            {% endif %}"

The door sensor payload has the contact state as true when the Agara sensor and magnet are in close contact which is door closed.

The template function is returning a false state regardless of what I have for the state comparision value. I have tried “on”, “off” and “true”, but it the function call still returns a false state.

I did try to use payload_off and payload_on but that threw an error saying these keys are not valid in a manual configuration.

Zigbee2mqtt does display the correct payload values that is published.

[2024-08-21 08:29:02] info: 	z2m:mqtt: MQTT publish: topic 'zigbee2mqtt/0x00158d000ad62d3d', payload '{"battery":100,"contact":false,"device_temperature":30,"linkquality":138,"power_outage_count":7,"trigger_count":5,"voltage":3095}'
[2024-08-21 08:29:03] info: 	z2m:mqtt: MQTT publish: topic 'zigbee2mqtt/0x00158d000ad62d3d', payload '{"battery":100,"contact":true,"device_temperature":30,"linkquality":138,"power_outage_count":7,"trigger_count":5,"voltage":3095}'

Anyone have any thoughts as to what I might be doing wrong?

thanks
Mark

Seems like a mix of a mqtt sensor and a template sensor

For a start I’m betting this is a binary sensor, not a sensor:

Secondly if you want to invert the state do this:

mqtt:
  - binary_sensor:
      - name: "CGC Door Contact"
        device_class: door
        state_topic: 'zigbee2mqtt/0x00158d000ad62d3d'
        value_template: "{{ not value_json }}"

Or if it is not json data:

mqtt:
  - binary_sensor:
      - name: "CGC Door Contact"
        device_class: door
        state_topic: 'zigbee2mqtt/0x00158d000ad62d3d'
        value_template: "{{ not value }}"

The Agara door sensor is actually a multi sensor so it can be either a sensor or binary_sensor. This is what I eventually came up with which is working nicely.
The key was the payload_on and payload_off entries which matched the value_json.contact data. Thanks for the hints.

binary_sensor:
 - name: "CGC Door Contact"
      state_topic: 'zigbee2mqtt/0x00158d000ad62d3d'
      payload_on: false
      payload_off: true
      device_class: door
      value_template: '{{value_json.contact}}'