Control switches based on playback (stand-alone and Google audio-groups)

Hi all

I guess that almost every Home Assistant user can relate to making things work vs. make things work the best way. I’m trying to get this right - for once! :slight_smile:

I have a mixed setup of Audio-devices in my home:

  • 3* Google Home Mini’s
  • 1* Soundbard with Google Cast-support (So it’s powering on and off like it should)
  • 2* Legacy speakers with Google Chomecast Audio

The two legacy speakers is:

  • Kitchen-speaker NAD (power-control by switch.s20)
  • Office-speaker Marshall(power-control by switch.sonoff4)

Issue/Wish: Control the power of the legacy devices, for both “single” playback, and with support for group/team playback. (Power on if playback on Google Chromecast Audio, and power off after fx. 5 minuters)

Automation for single device is pretty easy (Click for code...)
automation:
  - alias: 'NAD Off Auto-control'
    id: NAD-off-chromecast
    hide_entity: true
    trigger:
      - platform: state
        entity_id: media_player.kitchen_speaker
        to: 'off'
        for:
          minutes: 5
      - platform: state
        entity_id: media_player.kitchen_speaker
        to: 'paused'
        for:
          minutes: 5
    condition:
      condition: and
      conditions:
        - condition: or
          conditions:
            - condition: state
              entity_id: media_player.kitchen_speaker
              state: 'off'
            - condition: state
              entity_id: media_player.kitchen_speaker
              state: 'paused'
        - condition: state
          entity_id: switch.s20
          state: 'on'
    action:
      service: switch.turn_off
      data:
        entity_id: switch.s20


  - alias: 'NAD On Auto-control'
    id: NAD-on-chromecast
    hide_entity: true
    trigger:
      - platform: state
        entity_id: media_player.kitchen_speaker
    condition:
          condition: or
          conditions:
            - condition: and
              conditions:
                - condition: state
                  entity_id: media_player.kitchen_speaker
                  state: 'playing'
                - condition: state
                  entity_id: switch.s20
                  state: 'off'
    action:
      service: switch.turn_on
      data:
        entity_id: switch.s20

Well… At least. Cause i’m also facing problems with the speaker turning off, if a manually turning it on, for other reasons. Fx. for bluetooh input, or audio-jack from my PC. Maybe you have input to this, too?



My “Groups” / Teams is called:

  • Team Mini (Our Google Home Mini’s)
  • Team Maxi (Soundbar and the two legacy speakers)
  • Team Battle (All of them)

I can’t came up with the best solution for the automation part, when it comes to the team-playback. I thought that it could be an idea to create an automation, which just synced the team-playback up to each single Google Audio-device, but i couldn’t get this to work.

I also tried this (see below), but it’s not working properly - and i was hoping for a better way to do this.

Switch control based on Maxi-group playback
alias: test1
trigger:
  platform: state
  entity_id: media_player.maxi_group
  to: 'playing'
condition:
  condition: and
  conditions:
    - condition: state
      entity_id: media_player.maxi_group
      state: 'playing'
    - condition: state
      entity_id: switch.sonoff4
      state: 'off'
action:
  service: switch.turn_on
  data:
    entity_id: switch.sonoff4

I’ve already looked through the forum multiple times, but i can’t find a solution which fitted my setup… Or at least, i can’t see how X, Y and Z solution could become helpful for my setup :slight_smile:

I’m not sure what exactly you’re after. What’s the issue with the legacy speakers?
I have a google cast on a soundbar which works flawlessly

You’re right. I totally forgot to point that out.

Does this help?

My automation works ok for playback on a single device, but the automation is a real mess, if i trying to make the same for all my “Teams”. The two “legacy” speakes is within the Team Maxi and Team Battle group - and i need the SonOff/S20 to power on, if playback is active.

The original idea was to correct the playback state on fx. media_player.office_speaker if media_player.maxi_group was playing, but i don’t think that it will work out… (Cause it will be overwritten by the correct state for media_player.office_speaker).I also though about making some template based sensors for this, but i’m worried that i’m “over doing” this.

So I’m asking for suggestions/solutions on how to control the power for the two legacy-devices, based on both single and team-playback.

It seems like you just need the correct conditions in your automations. I have a couple of similar examples I’ll try and post later (currently on my phone)

if you share your current automation and the list of media player entity names I can certainly lend you a hand

Here are a few examples to give you an idea of how you can acheive what I think you are trying to do. In these ‘media_player.house’ is a Google cast device group of all my cast capable devices. Currently my amplifier that is connected to one of the ChromeCast Audio’s is completely ‘dumb’ so gets switched via a IR blaster.

chromecast_audio_start.yaml

alias: ChromeCast Audio start automations
initial_state: 'on'
trigger:
  - platform: state
    entity_id: media_player.lounge_speakers
    to: 'playing'
  - platform: state
    entity_id: media_player.house
    to: 'playing'
action:
  - service: switch.turn_on
    entity_id: switch.onkyo_amp
  - delay: '00:00:03'
  - service: switch.turn_on
    entity_id: switch.onkyo_amp_cd

chromcast_audio_off_automation.yaml

alias: ChromeCast Audio off automations
initial_state: 'on'
trigger:
  - platform: state
    entity_id: media_player.lounge_speakers
    to: 'off'
    for:
      minutes: 1
  - platform: state
    entity_id: media_player.house
    to: 'off'
    for:
      minutes: 1
condition:
  condition: and
  conditions:
    - condition: state
      entity_id: media_player.lounge_tv
      state: 'off'
    - condition: state
      entity_id: media_player.lounge_speakers
      state: 'off'

    - condition: or
      conditions:
        - condition: state
          entity_id: media_player.house
          state: 'off'
        - condition: state
          entity_id: media_player.house
          state: 'unavailable'

action:
  - service: switch.turn_off
    entity_id: switch.onkyo_amp

that same amplifier is connected to my TV so I get better sound (surround sound) so I also control it to switch relative to my smart TV

tv_on_automation.yaml

alias: TV on automations
initial_state: 'on'
trigger:
  - platform: state
    entity_id: media_player.lounge_tv
    from: 'off'
action:
  - service: switch.turn_on
    entity_id: switch.onkyo_amp
  - delay: '00:00:03'
  - service: switch.turn_on
    entity_id: switch.onkyo_amp_v1

tv_off_automation.yaml

alias: TV off automations
initial_state: 'on'
trigger:
  - platform: state
    entity_id: media_player.lounge_tv
    to: 'off'
    for:
      minutes: 1
condition:
  condition: and
  conditions:
    - condition: state
      entity_id: media_player.lounge_tv
      state: 'off'
    - condition: state
      entity_id: media_player.lounge_speakers
      state: 'off'

    - condition: or
      conditions:
        - condition: state
          entity_id: media_player.house
          state: 'off'
        - condition: state
          entity_id: media_player.house
          state: 'unavailable'

action:
  - service: switch.turn_off
    entity_id: switch.onkyo_amp

the reason I have some stuff in there about media_players being ‘unavailable’ is due to a HA bug that sometimes reports Google Homes and Audio’s (generally the groups of these) as being ‘unavailable’

2 Likes

Hi

The automation for a single device is listed in first post :slight_smile:

^ Click it, in the first post.

Here is the details.

Chromecast in Office is included in the following media players (A single, and two groups)

media_player.office_speaker
media_player.maxi_group
media_player.mega_group

The device which control the power to the speaker is: switch.sonoff4

So if the media_player playing, it needs to power on sonoff4.

If it’s off/paused for at least X (let’s say 5) minutes, it should power off sonoff - but all mediaplayers needs to be state off/paused/unavailable, and that’s the thing i find really hard.

Do you need anyting else?

@sparkydave - Thanks! I already tried something simular, but i can’t make up a condition-sellection there i’m sure that the power should be powered off, and that’s caused by the mix of off, paused and unavailable states.

For the trigger part, it could be something like this for the power on solution, but it’s already looks pretty messy, if you ask me :slight_smile:

  - alias: 'Marshall Off Auto-control'
    initial_state: 'on'
    id: marshall-off-chromecast
    hide_entity: true
    trigger:
      - platform: state
        entity_id: media_player.office_speaker
        to: 'off'
        for:
          minutes: 5
      - platform: state
        entity_id: media_player.maxi_group
        to: 'off'
        for:
          minutes: 5
      - platform: state
        entity_id: media_player.mega_group
        to: 'off'
        for:
          minutes: 5
      - platform: state
        entity_id: media_player.office_speaker
        to: 'paused'
        for:
          minutes: 5
      - platform: state
        entity_id: media_player.maxi_group
        to: 'paused'
        for:
          minutes: 5
      - platform: state
        entity_id: media_player.mega_group
        to: 'paused'
        for:
          minutes: 5

And for the condition part, i don’t think that off will make it - and what if one media_player (group/single) is paused, another is off and the last is unavailable? In that case i can’t use this (see below) and just add up condition: or things.

    condition:
      condition: and
      conditions:
        - condition: state
          entity_id: media_player.office_speaker
          state: 'off'
        - condition: state
          entity_id: media_player.maxi_group
          state: 'off'
        - condition: state
          entity_id: media_player.mega_group
          state: 'off'