Doorbell configuration for automation

My configuration doesn’t work. I need to play a bell signal through the local MPD every time the doorbell button sensor goes into the on state. However, in reality, the first press does nothing, the second plays the melody, and the third again does not work. I need the playback to stop and the melody to play again,like in a normal doorbell. I have been suffering with this for a long time, please help me.

alias: 'Door bell'
initial_state: 'on'
trigger:
  - entity_id: binary_sensor.doorbell
    platform: state
    to: 'on'

condition: []

action:
  - service: media_player.volume_set
    data_template:
      entity_id: media_player.mpd
      volume_level: 0.8

  - service: media_player.play_media
    data:
      entity_id: media_player.mpd
      media_content_type: music
      media_content_id: "http://127.0.0.1:8123/local/sound/dindong.mp3"

Please format your post as per the blue banner at the top of the page.

@yalic
After reboot, what is the initial state of binary_sensor.doorbell?

Look in the developers panel under states ( it has this symbol < >) and find your binary_sensor.doorbell. It should tell you its initial state. If it is set to on after reboot this may explain the behaviour you are seeing. If it is set to off and the button changes it to on then I’d expect the behaviour to be the other way around from what you report. How does the sensor change back to the off state?

How is the doorbell changing the state? Are you using MQTT? Need a little bit more information.