I recently embarked on a project to integrate my Aqara motion sensor and light with Sonos speakers in my living room and dining area. The goal was to create a seamless automation where pressing a Sonoff switch once would play my favorite radio station across both rooms, and pressing it again would pause the music. While the initial setup worked, I encountered an issue where the Sonos group wasn’t synchronizing perfectly, which was a bit frustrating but also an exciting challenge to solve.
I started by configuring the automation in Home Assistant using YAML, specifying the media_player entities for both rooms. However, the first press played the radio in one room but not the other, and the second press didn’t reliably pause both. After some research, I realized the issue might be with how the media_player.play_media service was being called. I adjusted the automation to ensure both players received the command simultaneously and added a delay to allow the devices to respond.
Here’s the refined automation setup that finally worked:
yaml
alias: Sonos_Sync_PlayPause
description: Synchronize Sonos group play/pause with Aqara switch
trigger:
- platform: device
device_id: e391905f9ebb5fbbc30fae26e6a248af
domain: zha
type: remote_button_short_press
action: - service: media_player.play_media
target:
entity_id: media_player.living_room, media_player.dining_room
data:
media_content_id: FV:2/2
media_content_type: favorite_item_id
metadata:
title: My Favorite Radio - delay: 00:00:02
- service: media_player.media_play
target:
entity_id: media_player.living_room, media_player.dining_room
condition:
This tweak ensured both Sonos players received the play command at the same time and then started playing in sync. Pressing the switch again now reliably pauses both devices. The key was ensuring the commands were sent to both players simultaneously and allowing a small delay for the devices to process the request.
This experience taught me the importance of precise timing and ensuring all devices receive commands in the correct sequence. It also highlighted the power of Home Assistant in creating custom automations that truly enhance home comfort. If anyone has similar challenges, I’d be happy to share more tips or troubleshoot together!