[Solved] Mqtt with aqara contact sensor (zigbee2mqtt)

Hi,

I think I’ve got kind of transformation error…

I’m running OH 3.1

I added an aqora contact sensor to my textual mqtt config.
The sensor is online and I am getting values from the channels except from one

The “contact” channel…

I think this is a json conversion problem.

While searching the forum I found this thread and tried to use the .js file to convert the values…

but still no luck :frowning:

my things file looks like this

...

Thing topic Contact_Balcony "MQTT -Balkontür" @ "Balkon" {
        Channels:
            Type contact : contact "Kontakt" [
                stateTopic="zigbee2mqtt/Magnetkontakt_Balkon",
                transformationPattern="JS:aqaracontact.js"
            ]
            Type number : battery "Batterie" [
                stateTopic="zigbee2mqtt/Magnetkontakt_Balkon",
                transformationPattern="JSONPATH:$['battery']"
            ]
            Type number : linkquality "Link" [
                stateTopic="zigbee2mqtt/Magnetkontakt_Balkon",
                transformationPattern="JSONPATH:$['linkquality']"
            ]
            Type number : temperature "Temperatur" [
                stateTopic="zigbee2mqtt/Magnetkontakt_Balkon",
                transformationPattern="JSONPATH:$['temperature']"
            ]
            Type number : voltage "Spannung" [
                stateTopic="zigbee2mqtt/Magnetkontakt_Balkon",
                transformationPattern="JSONPATH:$['voltage']"
            ]
    }

...

aqaracontact.js

(function(x){
    var result = "";
    var json = JSON.parse(x);
    if (json.contact)
    {
        result="CLOSED";
    }
    else
    {
        result="OPEN";
    }
    return result;
})(input)

I also tried something like this:

Thing topic Contact_Balcony "MQTT -Balkontür" @ "Balkon" {
        Channels:
            Type contact : contact "Kontakt" [
                stateTopic="zigbee2mqtt/Magnetkontakt_Balkon",
                transformationPattern="JSONPATH:$['contact']",
                open="false",
                close="true"
            ]
            
         ...

    }

both not working…

maybe someone got another idea what possibly went wrong?

This is my definition via UI though:

  • id: status
    channelTypeUID: mqtt:contact
    label: Status
    description: null
    configuration:
    stateTopic: zigbee2mqtt/XIAOMI_DOOR_SENSOR_1/contact
    off: “true”
    on: “false”

Same sensor.

EDIT: Just FYI, I am using z2mqtt’s “advanced” feature breaking all states into their own topics instead of one json string. So that might be a difference here.

What is the full JSON that is published by your sensor to zigbee2mqtt/Magnetkontakt_Balkon ?

Hi Guys,

thanks for your replies…

@chrismast I think I have to search for this ‘advanced’ feature to activate… I will report back if this helps :wink:

@hafniumzinc my full json string looks like this:

{"battery":100,"contact":false,"linkquality":93,"temperature":23,"voltage":3085}

for open and

{"battery":100,"contact":true,"linkquality":90,"temperature":23,"voltage":3085}

for closed

thanks so far… I’ll try to play around with the zigbee2mqtt settings and see if this changes anything for me

Looking at your JSON string you could just use the contact part without any additional JSON file like you do for your other topics.
You could then use state description meta data to change false/true to whatever you want.
Take into account though that this is only visual, i e. In rules you still would need to use false/true (if i am not wrong).
Another option would be to just use a transform map file. This would change the value before it gets to the item and is similar to that JSON code. Maybe also a lot simpler.

hi chris,

you mean like this? that’s not working sorry…

I think you would need to chain JSON and transformation map transforms for this.
Here is a post talking about it with an example (JSONPATH:$.STA∩MAP:statizone.map).
Never tried it myself though.

Edit: in your case the chain would be JSONPATH:$.contact∩MAP:contact.map (or however you define the map file).

ok thanks chris! I will have a look

Ok Guys,

finally after restarting OH the approach with the aqaracontact.js file is working.
Don’t know why this bugged me I also restarted several times while trying yesterday…

Silly enough :wink:

So here’s the solution…
thing:

Thing topic Contact_Balcony "MQTT -Balkontür" @ "Balkon" {
        Channels:
            Type contact : contact "Kontakt" [
                stateTopic="zigbee2mqtt/Magnetkontakt_Balkon",
                transformationPattern="JS:aqaracontact.js"
            ]
...

javascript:
aqaracontact.js

(function(x){
    var result = "";
    var json = JSON.parse(x);
    if (json.contact)
    {
        result="CLOSED";
    }
    else
    {
        result="OPEN";
    }
    return result;
})(input)

thanks anyway - sorry for stealing your time :wink:

Dan

It rings a bell with me that JSON booleans in MQTT are a bit quirky, possibly not handled at all; that’s not the same as "contact":"true"

If someone could figure that out, the docs or binding may need enhancement.
The little JS script above handles the JSON natively (of course) and interprets the boolean directly.
The test would be to apply JSONPATH to the same payload and see what the result is in a String.

1 Like