Sonoff MQTT how to clear state in MQTT Database

I have a Sonoff flashed with Tasmota and I am controlling a light. I would like to ensure that the Sonoff turns on when somebody turns on the light switch. I’ve configured Sonoff to successfully do this, however MQTT is remembering that it was turned off hence switches it back off again after a couple of seconds. I have confirmed it is definiatly the Mosquitto MQTT server since when it is shutdown the light operates correctly (ie. turns on and stays on).

I thought a good way around this would be to create an automation so when the Sonoff moves to ‘unavailable’ state it clears the database. This isn’t working for me. Every time I power up the light it switches off about 2 seconds later because MQTT has remembered state and turns it off.

Here is my code for the switch:-

  - platform: mqtt
    name: "light"
    state_topic: "stat/light/POWER"
    command_topic: "cmnd/light/POWER"
    availability_topic: "tele/light/LWT"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    payload_available: "Online"
    payload_not_available: "Offline"
    retain: true

And here is the code for my attempted fix; happy to resolve this a different way if anybody has ideas??

- id: '1561897707172'
  alias: Set Payload for Sonoff Tasmota to null when switched off
  - entity_id: switch.light
    platform: state
    to: 'unavailable'
  condition: []
  - service: mqtt.publish
      topic: cmnd/light/POWER
      payload: ''

Change this;

retain: true


retain: false

You will also have to remove the retained message. Here is an excellent video on the how and why:

Thanks, that is great! Did the trick!!