Contact State MQTT

I’ve just started with my first openhab 2 implementation and have started with a PIR sensor using MQTT and running on a raspberry PI.

I’ve followed the instructions here and tweaked the code to use a Particle Photon as I had one to hand.

I’ve got the Photon publishing out to MQTT and can see the messages coming through, see my code below:

(Please note there will be a lot of references to a door, this is just from me adapting the code from a door sensor to a PIR sensor)

// This #include statement was automatically added by the Particle IDE.
#include <MQTT.h>
#include <Debounce.h>

/* MQTT Settings */
// Topic which listens for commands
char* outTopic = "MK-SmartHouse/security/MK-DoorSensor1"; 

//MQTT Server IP Address
byte server[] = { 192,168,1,55 };

//Unique device ID 
const char* mqttDeviceID = "MK-SmartHouseDevice1"; 

//MQTT Client
MQTT client(server, 1883, callback);

// receive message nothing to do here - we're only sending
void callback(char* topic, byte* payload, unsigned int length) {}

Debounce motioninput;

void setup() {
    motioninput.attach(A0, INPUT);
    motioninput.interval(100); // interval in ms

bool resetSensor;

void loop() {
    while (!client.isConnected()) {
    if ( == HIGH) {
        client.publish(outTopic, "OPEN");
        if (Particle.connected()) {
            Particle.publish(outTopic, "HALLWAY 1", PRIVATE, WITH_ACK);
        while ( == HIGH) {
        } // hang tight here until motion stops
        // Allow next close event
        resetSensor = true;
    else {
        if (resetSensor) {
            client.publish(outTopic, "CLOSE");
            // Stop next close event until it has been opened
            resetSensor = false;

The sensor is connecting and I can see the contact and switch I’ve added in the basicUI, I also get the notifications through when the state goes high or low, again currently this says OPEN or CLOSE at the moment.

The problem I am having though is that the contact in the UI always says OPEN and never says closed, even though I can see the notifications coming through.

These are my rules

//Side Door Sensor Is Open
rule "Side Door Sensor"
   Item MKDoorSensor1 received update OPEN
       if(SecuritySystem.state == ON)
           sendBroadcastNotification("SECURITY SYSTEM: SIDE DOOR OPEN")

//Side Door Sensor Is Closed
rule "Side Door Sensor Closed"
   Item MKDoorSensor1 received update CLOSE
       if(SecuritySystem.state == ON)
           sendBroadcastNotification("SECURITY SYSTEM: SIDE DOOR CLOSED")

These are my items

Switch SecuritySystem "Security System"
Contact MKDoorSensor1 "Side Door [%s]" <door> { mqtt="<[broker:MK-SmartHouse/security/MK-DoorSensor1:state:default]" }

And this is my sitemap

sitemap home label="MK-SmartHouse"
        Frame label="Security"
                Switch item=SecuritySystem
                Text item=MKDoorSensor1

Any help would be much appreciated

I think the Contact will receive “OPEN” string from MQTT rather than OPEN state, although I would expect that to sort itself out?
Here’s a similar post that he did get working, that uses specific state updates

1 Like

The states for a Contact are OPEN and CLOSED (notice the D).

There is now CLOSE state so your rule trigger will never happen (I would actually expect to see errors in the log and definitely errors in Designer on that line).

I believe if you change “CLOSE” to “CLOSED” in your Particle code as well as in the OH rules it should start working.


Thanks this did the trick!