MQTT ststus update


I am using mosquitto mqtt. If I turn the switch to ‘ON’ in open hab application. I have written a program in NODE MCU 12E board to trigger a relay. Its working fine. But my problem is, In rare case if my Node mcu gets restart my relay goes to off state, but switch state in OPEN HAB app is in ‘ON’ state. How to sync the state of the topic to my NODE MCU on restart…

Also If am outside my home I am controlling my devices. How should I ensure it is responded correctly. Some time If am outside, I will switch on the light in OPENHAB app… but in rare case my device have not responded. but stiill state of light is ‘ON’ in my app.


This is one of the problems we face in this type of communication, in my case I created a repetition routine in which it starts only when my Node MCU turns on. I also created a timer on it, which sends the status via MQTT every minute.
It was a solution I had, I hope it can help you in your project:

verificaEstado();//Verify the status of outputs
leituraSensor();//Sensor reading
unsigned long currentMillis = millis();//Start the counter
if (currentMillis - previousMillis > redLedInterval)  //if is more than 1minute
    previousMillis = currentMillis;    // Save the current time
    calcTemp(); //Calculate the temperature
    MQTT.publish(TOPICO_PUBLISH, buffer); //Publish the topic

    leituraSinal(); //Read the signal and publish the topic       
//sends the status of the lamp and temperature only once
  MQTT.publish("QuartoRenan/Luz/state", ledStatus);
  MQTT.publish(TOPICO_PUBLISH, buffer);

In that case, you can create a rule in openhab that if you do not receive updates by the mqtt topic, notify you that the device is not responding and turn off the openhab light


thank you so much…
what u r trying to say is… using digitalread function i need to check the status in node mcu and to publish it as a topic periodically. then using rule i want to switch of the device if it is not responding for certain period of time
am i correct?


i have created a “STATE” topic , Now I am receiving response from Node MCU. But I am not sure how to write a rule for not receiving more than particular time period in open. Could you give any rule template related to this
Thanks in advance…


Yes, exactly.

For this you can create a date type item and make a rule that places the last date and time the item was updated.

rule “Last Update item”
         Item YourItem received update OPEN
         YourDateItemType.postUpdate(new DateTimeType())

Now, you will always have the last date, time, minutes and even seconds when it was the last message received by NodeMCU


thank you… I will try this tonight.


i tried my best, but i could’t able to create dateitem type and so on…
can u provide me some examples like. one item template, sitemaptemplate

“YourDateItemType.postUpdate(new DateTimeType())” i didnt understand this part in ur example…



In your .items file, create an item like this:
DateTime LastUpdate "Last update: [%1$tH:%1$tM - %1$td/%1$tm]" <calendar>

Modifying your .rules file will look like this:

rule “Last Update item”
     Item YourItem received update OPEN
     LastUpdate.postUpdate(new DateTimeType())

Finally, your .sitemap file should have:

Text item=LastUpdate


thank you a lot…
its working great…