Radio - change speakers

Hi i have radio
when i try to change speaker the result the music stop to play and no change to another speker
what the problem ?
image

Automation:


- action:
  - service: script.radio_start
  alias: Radio Channel Change
  condition:
  - condition: state
    entity_id: input_boolean.radio_play
    state: 'on'
  id: radio_channel_change
  trigger:
  - entity_id: input_select.radio_station
    platform: state
- action:
  - service: script.radio_change_speakers
    data_template:
      previous_speakers: '{{ trigger.from_state.state }}'
  alias: Radio Speaker Change
  condition:
  - condition: state
    entity_id: input_boolean.radio_play
    state: 'on'
  id: radio_speaker_change
  trigger:
  - entity_id: input_select.radio_speakers
    platform: state

scripts:


radio_change_speakers:
  alias: Change Radio Speakers
  sequence:
    - service: media_player.media_stop
      data_template:
        entity_id: >
         {% if previous_speakers == "Livingroom" %} media_player.livingroom_speaker
         {% elif previous_speakers == "Kitchen" %} media_player.kitchen_speaker
         {% elif previous_speakers == "Bedroom" %} media_player.bedroom_speaker
         {% elif previous_speakers == "Everywhere" %} media_player.home_group
         {% endif %}
    - service: input_boolean.turn_off
      entity_id: input_boolean.radio_play

This maybe why?

You stop the previous speaker but your script doesn’t start playing on the newly selected speaker

1 Like

Ok
How you suggest to solve it?

Well you need to add an action to play on the selected speaker

not sure what is meant to happen with, and whether you need it when you switch speakers.
(You may need to remove it)

You’ve not provided the code for script.radio_start, but assuming it starts playing the selected radio channel on the selected speakers, you simply need to add it to the list of actions:

radio_change_speakers:
  alias: Change Radio Speakers
  sequence:
    - service: media_player.media_stop
      data_template:
        entity_id: >
         {% if previous_speakers == "Livingroom" %} media_player.livingroom_speaker
         {% elif previous_speakers == "Kitchen" %} media_player.kitchen_speaker
         {% elif previous_speakers == "Bedroom" %} media_player.bedroom_speaker
         {% elif previous_speakers == "Everywhere" %} media_player.home_group
         {% endif %}
    - service: input_boolean.turn_off
      entity_id: input_boolean.radio_play
    - service: script.radio_start

Thanks :grinning:
its work like a charm

1 Like

why do you check for the previous station? Only a check for the new station is needed?

  - alias: 'Play Radio individual'
    id: 'Play Radio individual'
#    initial_state: 'on'
    trigger:
      platform: state
      entity_id:
        - input_boolean.googlehome_woonkamer
        - input_boolean.googlehome_hall
        - input_boolean.googlehome_master_bedroom
        - input_boolean.googlehome_hobbykamer
        - input_boolean.googlehome_office
        - input_boolean.googlehome_dorm_marte
      to: 'on'
    condition:
      condition: template
      value_template: >
        {{is_state('input_boolean.radio_on','on')}}
    action:
      - service: media_player.play_media
        data_template:
          entity_id: >
            media_player.{{trigger.to_state.object_id}}
          media_content_id: >
            {{states('sensor.radio_station')}}
          media_content_type: 'music'

or cut up into 2:

script:
  play_radio:
    alias: Play radio
    sequence:
      - condition: template
        value_template: >
          {{not is_state('sensor.media_players_active','None')}}
      - service: media_player.volume_set
        data_template:
          entity_id: >
            {{states('sensor.media_players_active')}}
          volume_level: >
            {{ states('input_number.radio_volume')|float }}
      - service: media_player.play_media
        data_template:
          entity_id: >
            {{states('sensor.media_players_active')}}
          media_content_id: >
            {{states('sensor.radio_station')}}
          media_content_type: 'music' #'audio/mp4'

and automation:

  - alias: 'Play Radio'
    id: 'Play Radio'
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: input_boolean.radio_on
        to: 'on'
      - platform: state
        entity_id: sensor.media_players_active
    condition:
      condition: template
      value_template: >
        {{is_state('input_boolean.radio_on','on')}}
    action:
      - delay:
          seconds: 1
      - service: script.stop_last_active_radios # using variable
#      - service: script.stop_radio
      - delay:
          seconds: 1
      - service: script.play_radio