When I arrive home, play spotify ONLY if it was playing when I left?

Hello,

I’m trying to implement something simple:

  1. when I leave home
  2. if spotify is playing
  3. pause it
  4. when I come home
  5. ONLY if spotify was playing when I left <------
  6. play it

I’m trying to figure out how to implement line 5. My idea was to keep a variable/entity, with a check on step 2. Something like on step 2, assign “true” value to spotify_playing_when_left, so that value can be checked on step 5 and work from there.

Any thoughts on how to solve this?

Input_boolean?

Could you provide a little code snippet / general guidelines on how to go about this?

  1. when I leave home
  2. if spotify is playing
  3. pause it and turn on the Input Boolean
  4. when I come home
  5. ONLY if Input Boolean is on
  6. play it and turn off the Input Boolean

Post the entities you are using (for detecting presence and playing media) and then we can provide a working example.

Here’s the current code.

I’ll try making an input boolean and adjusting via GUI. Just a question before I do that, what’s the best place for new “manual entities”? (If this new input_boolean can be called that)

I’m trying to keep my configuration.yaml clean, so if you have any tips regarding this situation. The most specific thing I’m interested in is the best way to reload the config, without doing a full HA restart, same as I can do with templates, group entities etc.

alias: "Convenience: Spotify Automation"
description: ""
trigger:
  - platform: state
    entity_id:
      - person.user
    to: not_home
    from: home
    for:
      hours: 0
      minutes: 0
      seconds: 0
    id: user-left-home
  - platform: state
    entity_id:
      - person.user
    id: user-came-home
    from: not_home
    to: home
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: user-left-home
          - condition: device
            device_id: 9190989df60ebd064682a4ac54abf405
            domain: media_player
            entity_id: media_player.spotify_username
            type: is_playing
        sequence:
         # Below block, so it doesn't pause playback on my phone in the car, because of left-home delay
          - if:
              - condition: not
                conditions:
                  - condition: state
                    entity_id: media_player.spotify_username
                    attribute: source
                    state: "phone"
            then:
              - service: media_player.media_pause
                data: {}
                target:
                  entity_id: media_player.spotify_username
          #- HELP HERE - assign "true" value to spotify_playing_when_left_home
      - conditions:
          - condition: trigger
            id: user-came-home
          # If music was playing in the car, and I come home within 5 minutes of pausing it (leaving the car), pass condition
          - condition: template
            value_template: >
              {{ (as_timestamp(now()) -
              as_timestamp(states.media_player.spotify_username.last_changed))
              | round(0) < 300 }}
            #- HELP HERE - or spotify_playing_when_left_home == true
        sequence:
          - if:
              - condition: state
                entity_id: switch.pc
                state: not_home
            then:
              - wait_for_trigger:
                  - platform: state
                    entity_id:
                      - device_tracker.pc
                    from: not_home
                    to: home
                timeout: "60"
                continue_on_timeout: false
          - service: media_player.select_source
            data:
              source: pc
            target:
              entity_id: media_player.spotify_username
          - service: media_player.media_play
            data: {}
            target:
              entity_id: media_player.spotify_username
    default: []
mode: single

This short automation works on my setup. (Input_boolean.away_mode is set to “on” when I go out, and controls various other stuff as well.)

- id: '1662559531062'
  alias: Test
  description: ''
  trigger:
  - platform: state
    entity_id:
    - input_boolean.away_mode
  condition: []
  action:
  - choose:
    - conditions:
      - condition: state
        entity_id: input_boolean.away_mode
        state: 'on'
      - condition: state
        entity_id: media_player.kitchen
        state: playing
      sequence:
      - service: input_boolean.turn_on
        data: {}
        target:
          entity_id: input_boolean.test
      - service: media_player.media_pause
        data: {}
        target:
          entity_id: media_player.kitchen
    - conditions:
      - condition: state
        entity_id: input_boolean.away_mode
        state: 'off'
      - condition: state
        entity_id: binary_sensor.test
        state: 'on'
      sequence:
      - service: media_player.media_play
        data: {}
        target:
          entity_id: media_player.kitchen
      - service: input_boolean.turn_off
        data: {}
        target:
          entity_id: input_boolean.test
    default: []
  mode: single

But I see my life is much less complicated than yours! :grinning: