How to Send a MQTT command on Contact Item State Change?

  • Platform information:
    • Hardware: Raspberry Pi 3
    • OS: openHABianPi
    • Java Runtime Environment: Latest available
    • openHAB version: 2.2.0-1
  • Issue of the topic:

I’m trying to get a MQTT command to be sent when a door contact is open or closed.
I have a ESP12E connected to a YX5300 mp3 player and I have it working for the Security System On Off switch. Contact DoorSensor4 not commented out is my first test attempt can anyone tell me if I’m even close?

  • Items configuration related to the issue
//Security items
Switch SecuritySystem "Security System"  (Security)                    {mqtt=">[broker:ESP_377E2F/cmd:command:ON:play,15],>[broker:ESP_377E2F/cmd:command:OFF:play,16]" }
//Contact DoorSensor1 "Front Door [%s]"    <door> (Doors,Security)     { mqtt="<[broker:SmartDoor/security/DoorSensor1:state:default]" }
//Contact DoorSensor2 "Basement Door [%s]" <door> (Doors,Security)     { mqtt="<[broker:SmartDoor/security/DoorSensor2:state:default]" }
//Contact DoorSensor3 "Garage Door [%s]"   <door> (Doors,Security)     { mqtt="<[broker:SmartDoor/security/DoorSensor3:state:default]" }
//Contact DoorSensor4 "Back Door [%s]"     <door> (Doors,Security)     { mqtt="<[broker:SmartDoor/security/DoorSensor4:state:default]" }
Contact DoorSensor4 "Back Door [%s]"     <door> (Doors,Security)       { mqtt=">[broker:ESP_377E2F/cmd:command:OPEN:play,09],>[broker:ESP_377E2F/cmd:command:CLOSED:play,10],<[broker:SmartDoor/security/DoorSensor4:state:default]" }

The Contact items don’t receive command therefore you need to use the state
And you need to add the statement autoupdate=“false” or you risk to create an mqtt loop.

Contact DoorSensor4 "Back Door [%s]"     <door> (Doors,Security)       { mqtt=">[broker:ESP_377E2F/cmd:state:OPEN:play,09],>[broker:ESP_377E2F/cmd:state:CLOSED:play,10],<[broker:SmartDoor/security/DoorSensor4:state:default]", autoupdate="false" }

That should work

1 Like

Thanks @vzorglub it works perfect. Thanks for the super fast responce too.