Every time my ESP8266 Restart it gets "off" MQTT message even it was "on" before

Hello, I am trying to keep my lights “on” if its status “on” before the ESP8266 was down (power off). I am using retain: true but still every time my ESP restarts it get OFF status using Alexa (fauxmo Library). But if I turn it ON or OFF from Hassbian dashboard, Mosquitto stores the value properly in database.

I am using latest version of Hassbian and Mosquitto as my MQTT broker. Here is how my configuration file looks like.

- platform: mqtt
    name: Dressing Room Light
    state_topic: "home/nehulroom/dressingroomlight"
    command_topic: "home/nehulroom/dressingroomlight"
    payload_on: "on"
    payload_off: "off"
    retain: true
  - platform: mqtt
    name: Makeup Light
    state_topic: "home/nehulroom/makeuplight"
    command_topic: "home/nehulroom/makeuplight"
    payload_on: "on"
    payload_off: "off"
    retain: true

Here Is how I am publishing on my topic from my ESP device.

void fauxmoSetup(){

    Serial.println("Listing Available Devices for fauxmos");
    fauxmo.addDevice("Bathroom Light");
    fauxmo.addDevice("Bathroom Fan");
    fauxmo.addDevice("Dressing Room Light");
    fauxmo.addDevice("Makeup Light");

    fauxmo.setPort(80); // required for gen3 devices
    fauxmo.enable(true);

    Serial.println("Fauxmo All Devices setup");

    fauxmo.onSetState([](unsigned char device_id, const char * device_name, bool state, unsigned char value) {
        Serial.printf("[MAIN] Device #%d (%s) state: %s value: %d\n", device_id, device_name, state ? "ON" : "OFF", value);

           Serial.println(device_name);
           Serial.println(state);
           Serial.println(value);
           Serial.println(device_id);
          if (strcmp(device_name, "Bathroom Light")==0) {
              if(state){
                Serial.println("From bathroom light");
                digitalWrite(RELAY_1, LOW);
                relayStatusOnFile(relay1_file_status, "ON");
                if (client.connected()) {
                  client.publish(topic1, "on");
                }
              }else{
                digitalWrite(RELAY_1, HIGH);
                relayStatusOnFile(relay1_file_status, "OFF");
                if (client.connected()) {
                  client.publish(topic1, "off");
                }
              }
          } else if (strcmp(device_name, "Bathroom Fan")==0) {
              if(state){
                digitalWrite(RELAY_2, LOW);
                relayStatusOnFile(relay2_file_status, "ON");
                if (client.connected()) {
                  client.publish(topic2, "on");
                }
              }else{
                digitalWrite(RELAY_2, HIGH);
                relayStatusOnFile(relay2_file_status, "OFF");
                if (client.connected()) {
                  client.publish(topic2, "off");
                }
              }
          } 

          
    });

    Serial.println("Fauxmo Ready");
}

Is there something wrong I am sending in PayLoad? Because this is all I can think of right now. When I am sending a topic to publish I should send a proper payload I guess.

Thank you!

With your device off, subscribe to the command topic with mosquitto_sub (or any other client) and see what message actually gets sent by the broker.

@gpbenton Its just “on” or “off” nothing I am even publishing from my ESP just “on” and “off” state. Do I need to send any JSON or something else to make it store in database? As home assistant is storing the value if I directly turn from home assistant dashboard.

Please paste the topic and payload you see when you subscribe, simulating what happens when your esp starts, so we can see what messages it will be receiving.

Hello,

basically this is the message I am publishing

mosquitto_pub -h 192.168.1.100 -p 1883 -t home/nehulroom/dressingroomlight -m on

On Subscribe (mosquitto_sub -h 192.168.1.100 -p 1883 -t "#" -v -V mqttv311) I receive this message

home/nehulroom/dressingroomlight on

Here is how I do in my ESP code

client.publish(topic1, "on");

This message should turn the light on. Does it do that?

Why wouldn’t you use retain in your device code?

Yes it turn on the light. But its not getting stored in mosquito.db.

I am using it, you can see in my question

- platform: mqtt
    name: Dressing Room Light
    state_topic: "home/nehulroom/dressingroomlight"
    command_topic: "home/nehulroom/dressingroomlight"
    payload_on: "on"
    payload_off: "off"
    retain: true

But still its not getting save in my Mosquitto.db

If the message was received when you subscribed with the client, then it was stored in the mosquitto db.

If anyone can help me using Skype Screen share, any desk, team viewer or anything I will happy to share my screen and show the live working of my environment, if that can help to resolve or understand the issue.

Using this in your HA config isn’t the same as your DEVICE using it

client.publish(topic1, "on"); where topic1 is a variable defined globally

const char* topic1 = "home/nehulroom/dressingroomlight";

This I didn’t mentioned in my code above.

Thank you!

And yet nowhere in that code do you tell it to send the retain flag…

How can I send that from my ESP?

https://pubsubclient.knolleary.net/api.html

Add the retain flag

I’m trying to read your code on mobile, and I can’t see the rest of it obviously but is your relay low or high when it comes on?

Oh! I never noticed it. I am just trying it. Give me moment. :slight_smile:

Super duper amazing! :sunny:

This small thing shorted my issue! :100:

Thank you so much my friends :slight_smile:

1 Like