Shelly door2 sensor revert to closed state after every restart

I have the shelly door2 sensor. Each time HA restart, the sensor defaults to closed. How to setup the last known state after a reboot? This mqtt page suggest about a retain state, but I don’t know how to go about it. This is currently my sensor.

binary_sensor:

  - platform: mqtt
    state_topic: "shellies/D8767l31A8C6A/sensor/state"
    name: "Genkan Door"
    payload_on: 'open'
    payload_off: 'close'
    device_class: lock
    qos: 0

Ok. I think we need to state the retain to true(check box) by logging into the the shelly sensor using the local ip address assigned to it.