Home Assistant Sensors & Switches in wrong state after power cycle

Hi,

I feel like I have been going through every article I can find online, and not winning. So was hoping someone has experienced this. After a power cycle of my Raspberry Pi (hassio) my magnetic sensors defaults to close(Sonoff RF Bridge, with Tasmota), and not the last know state. The bigger issue is that I have a switch on my garage doors and after every power cycle the garage doors will open. This is configured in Tasmota as a relay with 2sec On/Off.

I have tried so many articles and am somewhat lost, below is an example of the configuration of 1 sensor and the garage switch.

switch:
  - platform: mqtt
    unique_id: garage_relay_03
    name: "Garage Door 03"
    state_topic: "stat/voogie_relay_gd03/POWER"
    command_topic: "cmnd/voogie_relay_gd03/POWER"
    qos: 1
    optimistic: true
    payload_on: "ON"
    payload_off: "OFF"
    retain: false
    
binary_sensor:
  - platform: mqtt
    name: "Garage Door 03"
    state_topic: "tele/voogie_rf_bridge/RESULT"
    value_template: '{{value_json.RfReceived.Data}}'
    payload_on: "D08D0A"
    payload_off: "D08D0E"
    device_class: door
    qos: 1

Here is the Status of my switch (garage) in Tasmota if this helps?

07:31:47 MQT: stat/voogie_relay_gd03/STATUS = {"Status":{"Module":18,"DeviceName":"Tasmota","FriendlyName":["Tasmota"],"Topic":"voogie_relay_gd03","ButtonTopic":"0","Power":0,"PowerOnState":1,"LedState":1,"LedMask":"FFFF","SaveData":1,"SaveState":1,"SwitchTopic":"0","SwitchMode":[0,0,0,0,0,0,0,0],"ButtonRetain":0,"SwitchRetain":0,"SensorRetain":0,"PowerRetain":0}}

Try with

retain: true

This will ‘retain’ the last mqtt value during a power cycle :wink:

Just made the change, but noticed something interesting. I think the issue is with Tasmota, as every time I restart Tasmota (The device) the relay toggles and my garage door opens or close, starting to wonder if it could be due to the fact that I selected a relay and not some sort of button.

If I restart Tasmota, this is the console output, but it still triggers the relay, not sure where the issue could be.

00:00:00 CFG: Loaded from flash at F5, Count 79
00:00:00 QPC: Count 1
00:00:00 Project tasmota Tasmota Version 9.2.0(tasmota)-2_7_4_9(2020-12-21T15:03:40)
00:00:00 WIF: Connecting to AP1 XCOM-LTE_AP Channel 2 BSSId CC:32:E5:24:78:DA in mode 11n as voogie_relay_gd03-6495...
00:00:02 WIF: Connected
00:00:03 HTP: Web server active on voogie_relay_gd03-6495 with IP address 192.168.0.202
08:33:24 RSL: tele/voogie_relay_gd03/INFO1 = {"Module":"Generic","Version":"9.2.0(tasmota)","FallbackTopic":"cmnd/DVES_ABD95F_fb/","GroupTopic":"cmnd/tasmotas/"}
08:33:24 RSL: tele/voogie_relay_gd03/INFO2 = {"WebServerMode":"Admin","Hostname":"voogie_relay_gd03-6495","IPAddress":"192.168.0.202"}
08:33:24 RSL: tele/voogie_relay_gd03/INFO3 = {"RestartReason":"Software/System restart"}
08:33:24 RSL: stat/voogie_relay_gd03/RESULT = {"POWER":"OFF"}
08:33:24 RSL: stat/voogie_relay_gd03/POWER = OFF
08:33:26 QPC: Reset
08:33:28 RSL: tele/voogie_relay_gd03/STATE = {"Time":"2020-12-31T08:33:28","Uptime":"0T00:00:10","UptimeSec":10,"Heap":27,"SleepMode":"Dynamic","Sleep":50,"LoadAvg":19,"MqttCount":0,"POWER":"OFF","Wifi":{"AP":1,"SSId":"XCOM-LTE_AP","BSSId":"CC:32:E5:24:78:DA","Channel":2,"RSSI":26,"Signal":-87,"LinkCount":1,"Downtime":"0T00:00:04"}}

I Mananged to fix the NodeMCU(Tasmota) side of the problem, changed from pin D0 to D2 (GPIO4) and now it does not toggle on restart of the device, now it still toggles on a power cycle of HomeAssistant. Seems like it sends and empty payload which then trigger the relay.

09:21:54 MQT: tele/voogie_relay_gd03/LWT = Online (retained)
09:21:54 MQT: cmnd/voogie_relay_gd03/POWER = 

Check PowerOnState and SetOption63 values in the Tasmota console

https://tasmota.github.io/docs/PowerOnState/