Value template not working for binary sensor

I have a Aqara door & window contact sensor (Xiaomi MCCGQ11LM), connected via zigbee2mqtt to Home Assistant.

I’d like to map the values open/closed values to custom values, but I cant get it working.

What I did was this this template here:

Option (1):

template:
  - sensor:
      - name: "Door Status"
        state: "{% if is_state('binary_sensor.door_sensor_shed_contact', 'true')-%}OPEN{%-else-%}CLOSED{%-endif%}"

But its always showing me “CLOSED” (which is not correct, it should be “OPEN”).

When I look at the raw MQTT message it says "contact: true", hence should map to “OPEN”:

{
	"battery":100,
	"contact":true,
	"device_temperature":3,
	"last_seen":"2023-04-04T10:43:54+02:00",
	"linkquality":40,
	"power_outage_count":19,
	"voltage":3225
}

For testing, I added this to my mqtt section - and this works. But this is only plan B!

Option (2):

mqtt:
  ...
  sensor:
    - name: "shed.closed"
      unique_id: "shed.closed"
      state_topic: "zigbee2mqtt/door_sensor_shed"
      value_template: "{%if (value_json.contact == true)-%}OPEN{%-else-%}CLOSED{%-endif%}"

Question: Why is Option (1) - the template approach - not working?

State should be 'on' not 'true':

        state: >
          {% if is_state('binary_sensor.door_sensor_shed_contact', 'on') %}
            OPEN
          {% else %}
            CLOSED
          {% endif %}

Look in Developer Tools / States to see the real state of an entity.

This is also not working. I tried:

state: "{% if is_state('binary_sensor.door_sensor_shed_contact', 'on')-%}OPEN{%-else-%}CLOSED{%-endif%}"

Paste a screenshot like this but for your binary_sensor.door_sensor_shed_contact:

And use the developer / template editor to test :wink:


That’s chickens rather than shed. You have to get the entity ID correct in the template, obviously…

I renamed “shed” to “chickens” :see_no_evil:, so that shouldn’t be the problem.

I just noticed that, while MQTT says "contact": true, HA says State = off. Shouldn’t this be the other way round?

image

OK, so now show us the sensor code that matches your current entity naming, that still isn’t working. By the way, you don’t need to be so concerned about spacing in templates: the result is trimmed of leading and trailing whitespace.

Convention that a door sensor goes 'on' when the door opens. See door under device_class here: Binary sensor - Home Assistant

:see_no_evil:
That was the issue! Thanks a lot!

I assumed that

  • "contact" = true would result in binary_sensor.door_sensor_chickens_contact = on
    and
  • "contact" = false would result in binary_sensor.door_sensor_chickens_contact = off

But it’s the other way round.

Not really intuitive (at least not for me). Do you know why they’ve made it this way?

A door sensor, by convention, goes on when the door is opened. The actual mechanics of your sensor are a contact (probably magnet and reed switch) that is closed when the door is closed.

Can I change this device_class for that sensor/entity in the UI - without adding code to configuration.yaml?

Depends on how the entity is set up. Settings > Devices and Services > Entities > click it. Then if “Show As” is visible, you can change it:

image

1 Like

Thanks! Now I understand a little bit more about HA :slight_smile:

Have a nice day.

1 Like

@JackX Please mark as solved (set ’ solution’ on the post that solved it for you :wink: )