MQTT : How to Retain Sonoff Switch Setting across Restarts of HA

So, I have also problem regarding “unknown” status after HA restart…

slika

When I click on sensor I can manualy set correct status:
slika

After I click on sensor or if automation set ON or OFF, after that status is correct…

Tasmota:

So, probably the problem is:

switch:

  - platform: mqtt
    name: "Zunaj - steber (luč)"
    state_topic: "stat/Sonoff-TH16-Steber/POWER"
    command_topic: "cmnd/Sonoff-TH16-Steber/POWER"
    availability_topic: "tele/Sonoff-TH16-Steber/LWT"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    payload_available: "Online"
    payload_not_available: "Offline"
    retain: false

sensor:

  - platform: mqtt
    name: "Zunaj-T"
    state_topic: "tele/Sonoff-TH16-Steber/SENSOR"
    value_template: '{{ value_json["AM2301"]["Temperature"] }}'
    unit_of_measurement: "°C"
  - platform: mqtt
    name: "Zunaj-V"
    state_topic: "tele/Sonoff-TH16-Steber/SENSOR"
    value_template: '{{ value_json["AM2301"]["Humidity"] }}'
    unit_of_measurement: "%"

What would be correct settings in my configuration…
Probably also enable PowerRetain, but not only this as this do not resolve my “unknown” status problem…

Can anybody help me to get correct sensor mqtt settings… Thank you…

Using the Console did it for me too - thanks!

My set up is for a delayed-stop bathroom air extractor fan. My wife wanted the ability to switch it off remotely (from the bath), but also that it comes on automatically, either all the time or if the humidity is too high (we have a separate Sonoff environment sensor in there, but it’s showing too high currently!). Because the extractor has live, ground and switched-live, the Sonoff is powered by the physical light switch and powers the switched-live of the extractor. The problem was that the Sonoff was retained the power off flag when the light switch was turned off. This posted solution works in this case, though I didn’t realise how the retain flag ended up being set to off, before I changed it.
Screencast from 2024-01-06 14-09-11