Automation toilet scenario with spotcast and Door sensor

Hi
I want create some automation to my door sensor (xioami Aqara) with spotcast(play Google mini) .

If someone going to open the door.
Paly some playlist via Spotcast.

Until here I know how do it.

The problem:
I want open door (1) close door (2) open again (3) close again (4) and the music was stop to play

This the real world scenario.

How I do it?

For inspiration: SOLVED...Bathroom Lights dim automatically. Great...but what if I need them to be full brightness right now?!?!

Thanks about your response
Thats what i create
I still not understand what i need to add to create the seconed toilet open

timer:

  speaker:
    duration: '00:15:00'
- id: '1576619298836'
  alias: Toilet speaker turn on
  description: ''
  trigger:
  - entity_id: binary_sensor.door_window_sensor_158d00035ac62a
    from: 'off'
    platform: state
    to: 'on'
  condition: []
  action:
  - data:
      entity_id: media_player.googlehome4082
      random_song: true
      uri: spotify:playlist:37i9dQZF1DXcxacyAXkQDu
    service: spotcast.start
  - data:
      entity_id: timer.speaker
    service: timer.start
- id: '1576625280153'
  alias: Toilet speaker turn off
  description: ''
  trigger:
  - event_data:
      entity_id: timer.speaker
    event_type: timer.finished
    platform: event
  condition: []
  action:
  - data:
      entity_id: media_player.googlehome4082
    service: media_player.turn_off

Configuration.yaml:

input_boolean:
  toilet_occupied:
    name: Toilet occupied

Automations:

- id: '1576619298836'
  alias: Toilet speaker turn on
  description: ''
  trigger:
  - entity_id: binary_sensor.door_window_sensor_158d00035ac62a
    from: 'off'
    platform: state
    to: 'on'
  condition:
  - condition: state
    entity_id: input_boolean.toilet_occupied
    state: false
  action:
  - data:
      entity_id: media_player.googlehome4082
      random_song: true
      uri: spotify:playlist:37i9dQZF1DXcxacyAXkQDu
    service: spotcast.start
  - data:
      entity_id: input_boolean.toilet_occupied
    service: input_boolean.turn_on
- id: '1576625280153'
  alias: Toilet speaker turn off
  description: ''
  trigger:
  - entity_id: binary_sensor.door_window_sensor_158d00035ac62a
    from: 'off'
    platform: state
    to: 'on'
  condition:
  - condition: state
    entity_id: input_boolean.toilet_occupied
    state: true
  action:
  - data:
      entity_id: media_player.googlehome4082
    service: media_player.turn_off
  - data:
      entity_id: input_boolean.toilet_occupied
    service: input_boolean.turn_off 
1 Like