Unable to keep the state of the switch or light after Hassbian restart

Hello, I am new to Hassbian started working on this from last 1 week. And its an amazing platform for me. I personally love all the possible customisations I can do, which make me feels like more personalised :slight_smile:

But from last few days I am facing 2 issues.

  1. My light (Night lamp) automatically get turned OFF at 07:00AM around in the morning. Even I don’t have any automation configured yet. Also I don’t have any routine setup in Alexa. And RETAIN is set TRUE.
  2. I am unable to keep the records of all the ON and OFF even using recorder with sqlite with purge value of 15 days. So every time I reboot Raspberry pi 3 it lost all the switch state, the light stays ON because I guess I have RETAIN value as TRUE that’s. But the switch shows in Hassbian dashboard shows that switch is OFF even the light is ON.

I have a basic setup of NodeMCU v1.0 with 5v relay connected.

Here is how my configuration.yaml looks like:

light:
  - platform: mqtt
    name: Night Lamp
    state_topic: "home/nehulroom/nightlamp/status"
    command_topic: "home/nehulroom/nightlamp"
    payload_on: "on"
    payload_off: "off"
    qos: 1
    retain: true

And recorder is configured like this

recorder:
  purge_keep_days: 15
  exclude:
    entities:
      - sun.sun # Don't record sun data
      - sensor.dark_sky_summary
      - sensor.dark_sky_icon 
      - sensor.dark_sky_temperature
      - sensor.dark_sky_apparent_temperature
      - sensor.dark_sky_wind_speed

If someone can suggest what to do, because I have been scratching my head from a last 2 days for these 2 problems and seems like everyone is talking about tasmota and other stuff, but nothing related to my problem, so opening this new topic.

You have retain: true

Yes, is that a problem? Should I try removing it? Or should I set it false?

Also can you please specify for which issue you are mentioning retain : true?

What software do you use on your NodeMCU?
Does the NodeMCU have a stable WiFi connection?

Yes I have stable Wifi-Connection to upload sketch I am using Arduino IDE

Also what @DavidFW1960 says; don’t use the retain: true flag. It can mess up the sync between MQTT broker and client and can cause strange behaviour if the NodeMCU restarts or loses connection to either WiFi or MQTT broker.

Sure I will seep that in mind. I will try today to remove it and reset the retain flag then I will update the status here.

Thank you! :slight_smile:

After you remove the retain flag, you might need to clear the persistence database… easiest way in hass.io is to remove the Mosquito broker and add it back again.

@DavidFW1960 Thanks for the suggestion, I am using Hassbian, so any command for that? or I have to remove and re-add the whole Mosquitto is the only option?

Look for mosquito.db and delete it after stopping the service. I knew (doh!) you were on hassbian too…

@DavidFW1960 :smiley: no problem, I will try to figure out :slight_smile: But this looks like a very helpful forum I have been reading this whole night.

1 Like

I am still not able to AUTOMATICALLY ON the lights if power supply goes down (pi 3 also gets turned OFF completely). And come back again, it should keep the light ON as it was ON before.

Any suggestions for this? @DavidFW1960

Also sometimes home Hassbian dashboard don’t show any entity at all. I have quite few sensors, switch and lights attached. But sometimes it do not show when I open the control panel. I have to restart the Hassbian server to see them back again. Seems like there is some instability problem in this.

Thank you!

I have the exact same problem, When ever i see a power failure ( which i see a lot of ) all my switches turn off. Its a huge problem as my Wife get annoyed if she has to turn on the fan again after a power cut.
Any way the state can be restored?
SonOffs running factory firmware retain the state.

Sorry @nehulagr for using your thread.

Rishab