Automate Sonos: Connect with AVR

Hello,

Just trying to get my head around the automation components - very simple example that I’d like to implement;

Sonos: Connect changes to ‘playing’ state
Trigger Harmony Hub “Sonos - Living Room” activity (this is handling correct inputs)

This is the config that I’ve used;

# Sonos
media_player:
  - platform: sonos
    hosts:
      - 192.168.1.226 # GuestRoom
      - 192.168.1.114 # Kitchen
      - 192.168.1.136 # LivingRoom

# Harmony Hub
remote:
  - platform: harmony
    name: Harmony Hub
    host: 192.168.1.82

# Automation
automation:
  alias: Turn on Receiver When Sonos Connect Plays
  initial_state: True
  hide_entity: False
  trigger:
    platform: state
    entity_id: media_player.living_room
    from: 'paused'
    to: 'playing'
  action:
    service: remote.send_command
    data_template:
      entity_id: remote.harmony_hub
      device: 24593350
      command: remote.turn_on

Log entry;

17-02-20 14:20:37 ERROR (MainThread) [homeassistant.core] Invalid service data for remote.send_command: required key not provided @ data['device']. Got None

Any assistance with this would be amazing :smiley:

Cheers, Rich

Hi Rich,
I have this exact automation and it looks like the remote.send_command bit is incorrect. Here is what I have:

  - service: remote.turn_on
    data:
      entity_id: remote.harmony
      activity: 22745209

That is perfect, thank you so much!

Expanding on this slightly, have you built any intelligence/conditions into your config?

i.e.

  • If somebody’s already watching TV ignore the trigger
  • Turning off the AVR, do you turn on pause or count for an idle time before offing the activity?

Sure I can work this but if you’re doing the same thing I’d be interested to hear your experience :slight_smile:

Yes I use conditions based around the state of the harmony quite a bit.
For example in my own ‘sonos turn on’ script I check to see if the Harmony is turned off before i go changing it.

alias: "Sonos turned on, turn it on in living room"
trigger:
  - platform: state
    entity_id: media_player.kitchen
    state: 'playing'
condition:
- condition: state
  entity_id: sensor.alarm_status
  state: 'unset' #most likely not in bed
- condition: state
  entity_id: sensor.harmony
  state: 'PowerOff' #nothing on TV
action:
  - service: script.turn_on_sonos

sensor.harmony is a template sensor that contains the current activity, defined as below

- platform: template
   sensors:
     harmony:
      value_template: '{{ states.remote.harmony.attributes.current_activity }}'
      friendly_name: 'Living Room'
1 Like