- alias: S- Set Music Master Speaker and Mantain Follow Music When The Only One Is Playing
trigger:
- platform: state
entity_id:
- media_player.living_room_sonos
- media_player.master_room_sonos
- media_player.kitchen_sonos
- media_player.first_corridor_sonos
to:
- "idle"
- "playing"
- "paused"
- "off"
- minutes: /5
platform: time_pattern
condition: []
action:
- choose:
# IF - only kitchen sonos is playing - set it as master speaker
- conditions:
- condition: state
state: "playing"
entity_id: media_player.kitchen_sonos
- condition: not
conditions:
- condition: state
state: "playing"
entity_id: media_player.living_room_sonos
- condition: state
state: "playing"
entity_id: media_player.master_room_sonos
- condition: state
state: "playing"
entity_id: media_player.first_corridor_sonos
sequence:
- service: input_select.select_option
entity_id: input_select.music_controller
data:
option: kitchen_sonos
# ELIF other rooms needs to be set up as well
##################################################
# SKIPPING SIMILAR CODE
##################################################
add_sonos_into_speaker_group:
mode: queued
alias: Add Sonos Speaker Into the Speaker Group
fields:
target_player:
description: "Sonos player name that need to be added into the group"
example: "media_player.master_room_sonos"
sequence:
- condition: template
value_template: >
{% if target_player is not none and target_player != false and target_player != '' %}
true
{% else %}
false
{% endif %}
# The target player must not be playing anything
- condition: template
value_template: >
{% if states(target_player) != 'playing' %}
true
{% else %}
false
{% endif %}
# First set the target player to the same volume as the controller
# Play:3 sounds level needs to be offset for setting up Play:1/Playbars
- service: media_player.volume_set
data_template:
entity_id: >
{% if target_player is not none %}
{{ target_player }}
{% endif %}
volume_level: >
{% for state in states.media_player if state.entity_id == 'media_player.' + states('input_select.music_controller') %}
{% if states('input_select.music_controller') != 'kitchen_sonos' and target_player == 'media_player.kitchen_sonos' %}
{{ state.attributes.volume_level + 0.1 }}
{% elif states('input_select.music_controller') == 'kitchen_sonos' and target_player != 'media_player.kitchen_sonos' %}
{{ state.attributes.volume_level - 0.1 }}
{% else %}
{{ state.attributes.volume_level }}
{% endif %}
{% endfor %}
# Now join the player into the group twice in case sometimes it didn't manage to join in for certain cases
- service: sonos.join
data_template:
master: media_player.{{ states('input_select.music_controller') }}
entity_id: >
{% if target_player is not none %}
{{ target_player }}
{% else %}
media_player.living_room_sonos
{% endif %}
remove_sonos_from_speaker_group:
alias: Remove Sonos Speaker From the Speaker Group and Update the Master Speaker
mode: queued
fields:
target_player:
description: "Sonos player that need to be removed from the group"
example: "media_player.master_room_sonos"
sequence:
- condition: template
value_template: >
{% if target_player is not none and target_player != false and target_player != '' %}
true
{% else %}
false
{% endif %}
# The target player must be playing
- condition: template
value_template: >
{% if states(target_player) == 'playing' %}
true
{% else %}
false
{% endif %}
# The target is not the soundbar that is playing TV sound
- condition: template
value_template: >
{% if target_player is not none and state_attr(target_player, 'media_title') != 'TV' %}
true
{% else %}
false
{% endif %}
# Update the master speaker in the group
- service: input_select.select_option
entity_id: input_select.music_controller
data:
option: >
{% set ns = namespace() %}
{% set ns.primary_speaker = 'none' %}
{% set ns.secondary_speaker = 'none' %}
{# set the pri_speaker and sec_speaker #}
{% for speaker in state_attr(target_player, "sonos_group") %}
{% if loop.index == 1 %}
{% set ns.primary_speaker = speaker|regex_replace(find='media_player.', replace='', ignorecase=False) %}
{% elif loop.index == 2 %}
{% set ns.secondary_speaker = speaker|regex_replace(find='media_player.', replace='', ignorecase=False) %}
{% endif %}
{% endfor %}
{# use the second speaker as master speaker if target speaker is currently the master #}
{% if target_player == ('media_player.' + ns.primary_speaker) and ns.secondary_speaker != 'none' %}
{{ ns.secondary_speaker }}
{% else %}
{{ ns.primary_speaker }}
{% endif %}
# The target must be the slave to be removed from the group
- condition: template
value_template: >
{% if target_player != 'media_player.' + states('input_select.music_controller') %}
true
{% else %}
false
{% endif %}
- service: sonos.unjoin
data:
entity_id: >
{% if target_player is not none and target_player != false and target_player != '' and target_player != 'None'%}
{{ target_player }}
{% else %}
media_player.living_room_sonos
{% endif %}
pause_sonos_if_sole_speaker_group:
alias: Pause the Sonos Speaker if it is a Sole Speaker Group
mode: queued
fields:
target_player:
description: "Sonos player that need to be paused"
example: "media_player.master_room_sonos"
sequence:
- condition: template
value_template: >
{% if target_player is not none and target_player != false and target_player != '' %}
true
{% else %}
false
{% endif %}
# check it is a sole speaker
- condition: template
value_template: >
{% for speaker in state_attr(target_player, "sonos_group") %}
{% if loop.index == 1 %}
{% if loop.length == 1 %}
true
{% else %}
false
{% endif %}
{% endif %}
{% endfor %}
- service: media_player.media_pause
data:
entity_id: >
{% if target_player is not none and target_player != false and target_player != '' and target_player != 'None'%}
{{ target_player }}
{% else %}
media_player.living_room_sonos
{% endif %}
利用这些script,给每个房间根据人体传感器和音乐跟随开关写上人来和人走的自动化
#################################################################
#
# Sonos Speakers Grouping/Ungrouping Based On Motion Sensor
#
#################################################################
- alias: S-LR Living Room Group Its Speaker If People Present
trigger:
- platform: state
from: "off"
to: "on"
entity_id:
- binary_sensor.living_room_sofa_motion_sensor_motion
- binary_sensor.living_room_tv_motion_sensor_motion
condition:
- condition: state
entity_id: input_boolean.follow_music
state: "on"
# The controller player must be playing music (not paused)
- condition: template
value_template: >
{% if states('media_player.' + states('input_select.music_controller')) == 'playing' %}
true
{% else %}
false
{% endif %}
action:
# Add this room speaker into the group
- service: script.add_sonos_into_speaker_group
data:
target_player: media_player.living_room_sonos
- alias: S-LR Living Room Ungroup/Pause Its Speaker If No People
trigger:
- platform: state
from: "on"
to: "off"
for: 00:10:00
entity_id:
- binary_sensor.living_room_sofa_motion_sensor_motion
- binary_sensor.living_room_tv_motion_sensor_motion
- minutes: /5
platform: time_pattern
condition:
- condition: state
entity_id:
- binary_sensor.living_room_sofa_motion_sensor_motion
- binary_sensor.living_room_tv_motion_sensor_motion
for: 00:10:00
state: "off"
action:
- choose:
# IF - music follower is on
- conditions:
- condition: state
entity_id: input_boolean.follow_music
state: "on"
sequence:
# Remove this room speaker from the group
- service: script.remove_sonos_from_speaker_group
data:
target_player: media_player.living_room_sonos
# Pause this room speaker in case it is the last item in the group
# or music follower is off
# Remove this room speaker from the group
- service: script.pause_sonos_if_sole_speaker_group
data:
target_player: media_player.living_room_sonos
优秀,主要是得有sonos设备才行吖。我看到HA文档的media_player服务中,有media_player.join和media_player.unjoin 两个服务,不知道是不是可以用google home mini来搞事情,没看到可以玩音乐跟随的,google的群组功能和这个不一样,当然本身也是为了听个响。