Sonoff toggle state on cycling power with KmanOz Sonoff firmware

Hi! I tried to modificate the Sonoff firmware from KmanOz to toggle the Sonoff on/off state when it is detached from power and powerd up again. I want to use this to implement the Sonoff into an existing conventionell electrical installation to switch lights with a inverted push button. So if the Sonoff looses power for some milliseconds it boots up again it should toggle the old state.This works fine, unless the sonoff device connects itself to the mqtt broker. At that moment, the sonoff gets the old status from the broker and switches back to the old state. It seems, that the broker remembers the old stat. The strange thing is, that HomeAssistant acts as expectet and toggles the switch (e.g. when the device state was off, I power it off and on again, the devce tollges the state to on. After connecting to the mqtt broker it turns the relais off agian but homassistant toggles the state as expected to on).
Any ideas how to solve this problem?

Here the my HomeAssistant config for the Sonoff switch:

  • platform: mqtt
    name: “Sonoff”
    state_topic: “home/sonoff/1/stat”
    command_topic: “home/sonoff/1”
    qos: 0
    payload_on: “on”
    payload_off: “off”
    retain: false
    optimistic: false

I am having this same problem. Were you able to figure out a solution?