Boolean switch, only activate automation when I manually click it?

First I’ll describe what I am trying to do.
I have a Boolean switch which is called Spotify. When I turn it on, it will fire up my Android TV box first, and then send a curl to activate a Tasker Task which is on the Android TV box, telling Spotify to start and play a playlist.
This part is working fine, I have also set it to detect when Spotify is paused, and it will turn off the Boolean switch. And this is the problem. Because when I turn off the Boolean switch I send another curl command to Tasker, telling it to close Spotify and turn off the Android TV box. This is what I want to do when I click the Boolean switch myself in Home Assistant. But I do not want this to happen when I manually pause the music in Spotify (which is detected by HA as a turned off switch and it executes the turn off automation).

So is there a way to make the turn off automation only run when I click it inside Home Assistant, and not activate when Home Assistant set the boolean switch to off (because I paused the music)?

This is the full automation. I am very new to this, so there might be a much prettier solution, but what I have come up with works, except what I describe above.

homeassistant:
  customize:
    switch.bedroom_spotify_tasker:
      hidden: true
    input_boolean.bedroom_spotify:
      icon: mdi:spotify

media_player:
  # https://home-assistant.io/components/media_player.spotify/
  - platform: spotify
    client_id: !secret spotify_client_id
    client_secret: !secret spotify_client_secret

input_boolean:
  # https://home-assistant.io/components/input_boolean/
  bedroom_spotify:
    name: Spotify
    initial: off
  livingroom_spotify:
    name: Spotify
    initial: off

switch:
  # https://home-assistant.io/components/switch.command_line/
  - platform: command_line
    switches:
      bedroom_spotify_tasker:
        command_on: 'curl -k "http://192.168.1.6:1817/?message=spot_on"'
        command_off: 'curl -k "http://192.168.1.6:1817/?message=spot_off"'
      bedroom_spotify_delay_tasker:
        command_on: 'curl -k "http://192.168.1.6:1817/?message=spot_on_delay"'
        command_off: 'curl -k "http://192.168.1.6:1817/?message=spot_off"'

automation:
  # Checks state of Spotify, if playing in Sovrum, set the boolean switch to ON, if not, set to OFF
  - alias: Bedroom - Input Boolean set to ON when detect PLAY
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: media_player.spotify
        to: 'playing'
    condition:
      - condition: template
        value_template: '{{ states.media_player.spotify.attributes.source == "Sovrum" }}'
    action:
      - service: input_boolean.turn_on
        entity_id: input_boolean.bedroom_spotify
  - alias: Bedroom - Input Boolean set to OFF when detect PAUSED
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: media_player.spotify
        to: 'paused'
    condition:
      - condition: template
        value_template: '{{ states.media_player.spotify.attributes.source == "Sovrum" }}'
    action:
      - service: input_boolean.turn_off
        entity_id: input_boolean.bedroom_spotify

  - alias: Bedroom - Input Boolean turn off Spotify + TV when set to OFF
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: input_boolean.bedroom_spotify
        to: 'off'
    condition:
      - condition: state
        entity_id: switch.bedroom_tv
        state: 'on'
    action:
      - service: homeassistant.turn_off
        entity_id: switch.bedroom_spotify_tasker
      - delay: '00:00:03'
      - service: homeassistant.turn_off
        entity_id: switch.bedroom_mi_box

  # Checks state of TV switch, if OFF, start TV + delay 3 sec + start Spotify
  - alias: Bedroom - TV + Spotify ON
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: input_boolean.bedroom_spotify
        to: 'on'
    condition:
      - condition: state
        entity_id: switch.bedroom_tv
        state: 'off'
    action:
      - service: homeassistant.turn_on
        entity_id: switch.bedroom_mi_box
      - delay: '00:00:03'
      - service: homeassistant.turn_on
        entity_id: switch.bedroom_spotify_delay_tasker

  # Checks state of TV switch, if ON, start Spotify
  - alias: Bedroom - Spotify ON
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: input_boolean.bedroom_spotify
        to: 'on'
    condition:
      - condition: state
        entity_id: switch.bedroom_tv
        state: 'on'
    action:
      - service: homeassistant.turn_on
        entity_id: switch.bedroom_spotify_tasker

Add a condition telling the “Off” Automation to only fire if the input boolean is turned on?

Im pretty sure that would result in the same thing? Because the switch is turned on until music is paused, then it turns off, which would fire the automation? So there would be no difference from now? Or am i missing anything?

@joq3 If I understand this correctly, you want to see the correct state of the Spotify playing state but you only want the on and off commands to send from the input_boolean within HA. Here’s what I’d try:

First get rid of the switch, you shouldn’t need it anymore. Then use a template sensor with a value template to determine the playing state from Spotify player.

Finally, you make 4 automations for your input_boolean.

First one, triggers when you turn your Input_boolean on, condition that your sensor shows Spotify is paused and has an action to send the curl command. This would only send the command on, when it’s current state is off.

Second one, triggers when you turn off your input_boolean, condition that sensor shows Spotify is playing and has an action to send the curl command. Again, it won’t trigger if the state is paused.

Third one, triggers when your Spotify sensor is playing and turns your input boolean on.

Fourth one, triggers when Spotify sensor is paused, and turns your input_boolean off.

Those last two shouldn’t trigger the first two because of the sensor. Hope that makes sense. It’s late here so my brain might have missed something. :wink:

Wow, it’s early where I am and my brain isn’t working well either. I’m not completely sure I understand. So the switch which sends the Curl’s is unnecessary? I can replace this by sending the curl in an automation action?
But what I don’t understand is, as I want to get the state of Spotify to determine the state of the boolean_switch, when paused (set to off) and when playing (set to on). Wouldn’t the off automation be activated when the boolean switch is set to off when detecting paused?

No it shouldn’t because of the condition in automation #2. Here’s the flow:

Scenario 1:

You turn off Spotify remotely. Automation 4 triggers because Spotify sensor changes to “paused”. Turns Input_Boolean off and updates your state correctly.

Automation #2 triggers now because input_boolean set to off but stops because condition of Spotify sensor “playing” isn’t met so doesn’t do anything.

Scenario 2:

Spotify sensor shows “playing”, you manually turn input_boolean off. Automation #2 triggers and condition is met that Spotify sensor displays “playing” and executes curl action.

Yes, that’s right, you wouldn’t require any switches. Just use the input_boolean and sensor instead.

Give it a try, I’m pretty confident it will work for you.

Is the template sensor really needed? He could use the state of the media_player instead?!

condition: state
entity_id: media_player.spotify
state: 'playing'

Or is there something I misunderstood?

Just tried rebuilding the automation according to your tips, here is the complete automation part:
https://hastebin.com/uvalobitar.pas

It almost works. However there is an issue where sometimes Spotify did not start playing, yet the switch stays ON, like it is playing. Same thing with it staying off when it is playing.
I have noticed it doesn’t always pickup the state.
So if I start playing Spotify on my Android TV, and I restart Home Assistant, the Spotify switch will not be turned on, even if I can see clearly that the state of TV is on, and Spotify is playing and on my source “Sovrum”. So it fulfills everything, yet the switch isn’t moved.
But if I pause Spotify, and then play, it turns the switch on.

So I have an issue, but I cannot figure out what is wrong, as it is working 90% of the time.

@joq3 This is more along the lines of what I was thinking. You may have to edit it a little bit, since I’m not sure what “Sovrum” is. Also I added Home Assistant to fire the last 2 automations on HA start:

automation:
  - alias: 'Bedroom Spotify Input Boolean Turns On Music' 
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: input_boolean.bedroom_spotify
        to: 'on'
    condition:
      - condition: and
        conditions:
          - condition: state
            entity_id: switch.bedroom_tv
            state: 'off'
          - condition: state
            entity_id: sensor.spotify
            state: 'paused'
    action:
      - service: homeassistant.turn_on
        entity_id: switch.bedroom_mi_box
      - delay: '00:00:03'
      - service: shell_command.bedroom_spotify_tasker_on

  - alias: 'Bedroom Spotify Input Boolean Turns Off Music'
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: input_boolean.bedroom_spotify
        to: 'off'
    condition:
      - condition: and
        conditions:
          - condition: state
            entity_id: switch.bedroom_tv
            state: 'on'
          - condition: state
            entity_id: sensor.spotify
            state: 'playing'
    action:
      - service: shell_command.bedroom_spotify_tasker_off


  - alias: 'Bedroom Spotify Sensor Turns on Input Boolean'
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: sensor.spotify
        to: 'playing'
      - platform: homeassistant
        event: start 
    condition:
      - condition: and
        conditions:
          - condition: state
            entity_id: switch.bedroom_tv
            state: 'on'
          - condition: state
            entity_id: sensor.spotify
            state: 'playing'
    action:
      - service: input_boolean.turn_on
        entity_id: input_boolean.bedroom_spotify

  - alias: 'Bedroom Spotify Sensor Turns off Input Boolean'
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: sensor.spotify
        to: 'paused'
      - platform: homeassistant
        event: start 
    condition:
      - condition: and
        conditions:
          - condition: state
            entity_id: sensor.spotify
            state: 'paused'
    action:
      - service: input_boolean.turn_off
        entity_id: input_boolean.bedroom_spotify

That isn’t any different from what I posted? You have just added another condition to the sensor part, which is the same as the trigger. This does no difference for me.

      - condition: state
        entity_id: sensor.spotify
        state: 'playing'

Also

  - platform: homeassistant
    event: start 

will cause the automation to run after restarting HA, launching TV and Spotify.

This is getting too hard!

EDIT: The best working version is the one I posted in the first post, except that it will turn off everything when you pause the music. Other than that it will change the state of the switches correctly. Sadly not usable.

It is different. I did change more than just those two things. Yeah, HA takes a lot of trial and error. I find, it takes A LOT of patience, but generally you can get it to do exactly what you need, just takes time. All the best!