Door mqtt switch goes open and close at ha restart

My mqtt based door switch goes to open and close when I reboot home assistant.

I have an automation that is triggered when the door goes from open to close.

So when I restart ha the automation is triggered, which is not wanted.

I think it has something to do with the retain flag.
So I tried to set up retain at my tasmota device with powerretain 1 but this also did not help. So I changed it back and tried to set retain to true in the switch.yaml file.
This also did not work.

So how can I stop that the state of the door goes to open and close at a restart? What causes this? The door is closed during the restart.

Post the relevant portion(s) of your configuration.yaml file so we can get some clues to what’s causing this problem.

Of course here is my config:

This is my automation (automation.yaml):

- alias: Light- door trigger
  initial_state: 'on'
  trigger:
    - platform: state
      entity_id: cover.door
      from: 'open'
      to: 'closed'
      for: '00:00:02' 
  action:

in cover.yaml

- platform: template
  covers:
    door:
      value_template: "{{ states.binary_sensor.door_state.state == 'on' }}"
      close_cover:
        service: homeassistant.turn_on
        data:
          entity_id: script.door_script
      open_cover:
        service: homeassistant.turn_on
        data:
          entity_id: script.door_script

in binary_sensor.yaml:

- platform: mqtt
  name: "door_state"
  state_topic: "stat/sonoff_door/RESULT"
  value_template: '{{ value_json["POWER1"] }}' 
  force_update: true
  device_class: garage_door

Actually, it’s desirable to publish state topics with retain=true. So if the sonoff is publishing its status as a retained message, that’s fine.

Try this: remove force_update: true then restart Home Assistant. Let us know if the automation continues to be triggered.

Looks like I already solved the problem.

Ididn’t tell that the above could is already my second version.

Before I had a mqtt sensor (not binary) to extract the data and a binary template sensor so look for that sensor and go on and of.

With a little more experience than at the beginning (when I set up this automation) I know direclty use an mqtt binary sensor to extract that data.

Fun thing… I tried deleting force_update: true but this will again trigger the automation. Going back and it works.

In my second version (code in my last post) I also changed the the code of cover a little bit.

Instead of this

value_template: "{{ states.binary_sensor.door_state.state == 'on' }}"

I had something that looked like this

value_template: "{{ is_states(sensor.door_state) }}"

So maybe that makes the difference.

I was wrong, the automation still gets triggered but it seems like it does not every restart.

I don’t know what to tell you. Based on what you wrote, I no longer understand what you do or don’t currently have in your configuration.

All I can tell you is that this:

value_template: "{{ states.binary_sensor.door_state.state == 'on' }}"

and this:

value_template: "{{ is_states(sensor.door_state) }}"

are not referring to the same entity.

binary_sensor.door_state is a different entity from sensor.door_state. You’ve only shared the config for the binary_sensor so it’s impossible for anyone to comment on how sensor.door_state behaves.

Good luck finding a solution to the problem.

I know that. The code that you can find above is the current code I use.
Before I had a sensor and a binary sensor. Now I ony use one binary_sensor.

I hope I fill still get suggestions on how to solve this based on the code above: