Issues with mqtt switch

Hi Awesome Home-Assistant people, i need a little helping hand.

I created this switch to my mqtt switch:
switch 1:

  • platform: mqtt
    name: “TV”
    state_topic: “/tv1/relay/state”
    command_topic: “/tv1/gpio/12”
    payload_on: “1”
    payload_off: “0”
    qos: 1
    retain: false

The issue are when My mqtt are on, and if i change anything in hass, i reboot the system, and after that i need to go in and send a on command to change the switch again.

Device: Sonoff smart switch.

Hope you guys understand.

What should i do to remember the last state on or off after hass rebooted?

Greetings DKAutomater.

Setting the retain flag when a topic is published causes the MQTT broker to keep the state of the topic, and send it to any new clients when they subscribe.

When HA starts up, it will subscribe to the state_topic to get the state, so the client that publishes this should set the retain flag for that topic (in this case /tv1/relay/state)

So should i remove the remain flag in the yaml file, or should i change the mqtt settings some where else?

The retain flag in the yaml file isn’t relevant, it is only refers to topics that HA publishes, such as the command_topic

The client (the physical switch, or whatever is driving it) that publishes the state_topic must set the retain flag when it sends that message.

Ohh okay, so in my case, the physical switch is a Sonoff with ESPEasy software on it.
Do you know where the remain flag can be enabled on that?

/DKAutomater.

Anyone, how can i tell home-assistant what the last state of my sonoff relay is, after i rebooted the home-assistant server.