JackX
(Jacob)
April 4, 2023, 9:31am
1
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?
Troon
(Troon)
April 4, 2023, 9:37am
2
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.
JackX
(Jacob)
April 4, 2023, 9:40am
3
This is also not working. I tried:
state: "{% if is_state('binary_sensor.door_sensor_shed_contact', 'on')-%}OPEN{%-else-%}CLOSED{%-endif%}"
Troon
(Troon)
April 4, 2023, 9:41am
4
Paste a screenshot like this but for your binary_sensor.door_sensor_shed_contact
:
aceindy
(Aceindy)
April 4, 2023, 9:46am
5
And use the developer / template editor to test
Troon
(Troon)
April 4, 2023, 9:50am
7
That’s chickens
rather than shed
. You have to get the entity ID correct in the template, obviously…
JackX
(Jacob)
April 4, 2023, 9:56am
8
I renamed “shed” to “chickens” , 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?
Troon
(Troon)
April 4, 2023, 10:00am
9
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
JackX
(Jacob)
April 4, 2023, 10:08am
10
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?
Troon
(Troon)
April 4, 2023, 10:09am
11
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.
JackX
(Jacob)
April 4, 2023, 10:12am
12
Can I change this device_class
for that sensor/entity in the UI - without adding code to configuration.yaml?
Troon
(Troon)
April 4, 2023, 10:17am
13
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:
1 Like
JackX
(Jacob)
April 4, 2023, 10:22am
14
Thanks! Now I understand a little bit more about HA
Have a nice day.
1 Like
aceindy
(Aceindy)
April 4, 2023, 11:29am
15
@JackX Please mark as solved (set ’ solution’ on the post that solved it for you )