Ouvir rádios no google home por comandos de voz

Boas malta, como aqui ainda não vi nenhum tutorial para rádios no HA vou partilhar o meu.
O meu código é mais direccionado para comandos de voz no google home. Se tiverem mais do que 1 google home na app criam um grupo de altifalantes que vao aparecer automaticamente no HA para poderem ouvir a radio em simultâneo em todos os minis em perfeita sintonia.

script:

  radio_comercial_sala:
    alias: Radio Comercial sala
    sequence:
      - service: media_player.volume_set
        data:
          entity_id: media_player.sala
          volume_level: '0.20'
      - service: media_player.play_media
        data_template:
          entity_id: media_player.sala
          media_content_id: http://mcrscast1.mcr.iol.pt/comercial.mp3
          media_content_type: 'audio/mp4'

  radio_comercial_cozinha:
    alias: Radio Comercial cozinha
    sequence:
      - service: media_player.volume_set
        data:
          entity_id: media_player.cozinha
          volume_level: '0.20'
      - service: media_player.play_media
        data_template:
          entity_id: media_player.cozinha
          media_content_id: http://mcrscast1.mcr.iol.pt/comercial.mp3
          media_content_type: 'audio/mp4'

  radio_comercial_ambiente:
    alias: Radio Comercial ambiente
    sequence:
      - service: media_player.volume_set
        data:
          entity_id: media_player.som_ambiente
          volume_level: '0.20'
      - service: media_player.play_media
        data_template:
          entity_id: media_player.som_ambiente
          media_content_id: http://mcrscast1.mcr.iol.pt/comercial.mp3
          media_content_type: 'audio/mp4'

  rfm_sala:
    alias: RFM sala
    sequence:
      - service: media_player.volume_set
        data:
          entity_id: media_player.sala
          volume_level: '0.20'
      - service: media_player.play_media
        data_template:
          entity_id: media_player.sala
          media_content_id: http://19553.live.streamtheworld.com:3690/RFM_SC
          media_content_type: 'audio/mp4'

  rfm_cozinha:
    alias: RFM cozinha
    sequence:
      - service: media_player.volume_set
        data:
          entity_id: media_player.cozinha
          volume_level: '0.20'
      - service: media_player.play_media
        data_template:
          entity_id: media_player.cozinha
          media_content_id: http://19553.live.streamtheworld.com:3690/RFM_SC
          media_content_type: 'audio/mp4'

  rfm_ambiente:
    alias: RFM ambiente
    sequence:
      - service: media_player.volume_set
        data:
          entity_id: media_player.som_ambiente
          volume_level: '0.20'
      - service: media_player.play_media
        data_template:
          entity_id: media_player.som_ambiente
          media_content_id: http://19553.live.streamtheworld.com:3690/RFM_SC
          media_content_type: 'audio/mp4'

  m80_sala:
    alias: M80 sala
    sequence:
      - service: media_player.volume_set
        data:
          entity_id: media_player.sala
          volume_level: '0.20'
      - service: media_player.play_media
        data_template:
          entity_id: media_player.sala
          media_content_id: http://mcrscast.mcr.iol.pt/m80
          media_content_type: 'audio/mp4'

  m80_cozinha:
    alias: M80 cozinha
    sequence:
      - service: media_player.volume_set
        data:
          entity_id: media_player.cozinha
          volume_level: '0.20'
      - service: media_player.play_media
        data_template:
          entity_id: media_player.cozinha
          media_content_id: http://mcrscast.mcr.iol.pt/m80
          media_content_type: 'audio/mp4'

  m80_ambiente:
    alias: M80 ambiente
    sequence:
      - service: media_player.volume_set
        data:
          entity_id: media_player.som_ambiente
          volume_level: '0.20'
      - service: media_player.play_media
        data_template:
          entity_id: media_player.som_ambiente
          media_content_id: http://mcrscast.mcr.iol.pt/m80
          media_content_type: 'audio/mp4'

  vodafone_sala:
    alias: Vodafone sala
    sequence:
      - service: media_player.volume_set
        data:
          entity_id: media_player.sala
          volume_level: '0.20'
      - service: media_player.play_media
        data_template:
          entity_id: media_player.sala
          media_content_id: http://195.23.102.207/vodafone
          media_content_type: 'audio/mp4'

  vodafone_cozinha:
    alias: Vodafone cozinha
    sequence:
      - service: media_player.volume_set
        data:
          entity_id: media_player.cozinha
          volume_level: '0.20'
      - service: media_player.play_media
        data_template:
          entity_id: media_player.cozinha
          media_content_id: http://195.23.102.207/vodafone
          media_content_type: 'audio/mp4'

  vodafone_ambiente:
    alias: Vodafone ambiente
    sequence:
      - service: media_player.volume_set
        data:
          entity_id: media_player.som_ambiente
          volume_level: '0.20'
      - service: media_player.play_media
        data_template:
          entity_id: media_player.som_ambiente
          media_content_id: http://195.23.102.207/vodafone
          media_content_type: 'audio/mp4'

  radio_stop_sala:
    alias: Parar rádio sala
    sequence:
      - service: media_player.media_stop
        data:
          entity_id: media_player.sala

  radio_stop_cozinha:
    alias: Parar rádio cozinha
    sequence:
      - service: media_player.media_stop
        data:
          entity_id: media_player.cozinha

  radio_stop_som_ambiente:
    alias: Parar rádio ambiente
    sequence:
      - service: media_player.media_stop
        data:
          entity_id: media_player.som_ambiente

###############################################################################
#                                                                             #
#                       I N P U T _ B O O L E A N                             #
#                                                                             #
############################################################################### 
input_boolean:
  ligar_radio:
    name: Rádio Status

###############################################################################
#                                                                             #
#                     I N P U T _ D A T E T I M E                             #
#                                                                             #
###############################################################################
input_datetime:
  radio_on:
    name: "Ligar rádio às"
    has_date: false
    has_time: true

###############################################################################
#                                                                             #
#                     I N P U T _ N U M B E R                                 #
#                                                                             #
###############################################################################
input_number:
  sala_volume:
    icon: mdi:volume-medium
    name: Volume
    min: 1
    max: 100
    step: 1

  cozinha_volume:
    icon: mdi:volume-medium
    name: Volume
    min: 1
    max: 100
    step: 1

  ambiente_volume:
    icon: mdi:volume-medium
    name: Volume
    min: 1
    max: 100
    step: 1
###############################################################################
#                                                                             #
#                               S E N S O R                                   #
#                                                                             #
###############################################################################
sensor:
  - platform: template
    sensors:
      sala_volume:
        value_template: >
          {% if is_state('media_player.sala', 'on')  %}
            {{ (state_attr('media_player.sala', 'volume_level') * 100) | int }}
          {% endif %}
        friendly_name: Receiver Volume Level
        icon_template: mdi:volume-medium

  - platform: template
    sensors:
      cozinha_volume:
        value_template: >
          {% if is_state('media_player.cozinha', 'on')  %}
            {{ (state_attr('media_player.cozinha', 'volume_level') * 100) | int }}
          {% endif %}
        friendly_name: Receiver Volume Level
        icon_template: mdi:volume-medium

  - platform: template
    sensors:
      ambiente_volume:
        value_template: >
          {% if is_state('media_player.som_ambiente', 'on')  %}
            {{ (state_attr('media_player.som_ambiente', 'volume_level') * 100) | int }}
          {% endif %}
        friendly_name: Receiver Volume Level
        icon_template: mdi:volume-medium

###############################################################################
#                                                                             #
#                           A U T O M A Ç Õ E S                               #
#                                                                             #
###############################################################################   
automation:

  - alias: Ligar rádio
    initial_state: true
    trigger:
      - platform: template
        value_template: "{{ states('sensor.time') == (states.input_datetime.radio_on.attributes.timestamp | int | timestamp_custom('%H:%M', False)) }}"
    condition:
      condition: and
      conditions:
      - condition: state
        entity_id: input_boolean.ligar_radio
        state: 'on'
      - condition: state
        entity_id: device_tracker.bruno_melo
        state: home
      - condition: state
        entity_id: 'binary_sensor.workday_sensor'
        state: 'on'
    action:
      - service: media_player.volume_set
        data:
          entity_id: media_player.som_ambiente
          volume_level: '0.15'
      -  service: media_player.play_media
         data_template:
          entity_id: media_player.som_ambiente
          media_content_id: http://mcrscast1.mcr.iol.pt/comercial.mp3
          media_content_type: 'audio/mp4'


  - alias: Desligar rádio na ausencia
    initial_state: true
    trigger:
      - platform: state
        entity_id: device_tracker.bruno_melo
        from: home
        to: not_home
    action:
      - service: script.turn_on
        entity_id: 
          - script.radio_stop_sala
          - script.radio_stop_cozinha

#zigbee botao ikea controlo volume

#  - alias: Diminuir som sala
#    initial_state: true
#    trigger: 
#      platform: state
#      entity_id: sensor.0x000d6ffffebcd992_click
#      to: 'off'
#    action:
#      - service: media_player.volume_set
#        entity_id: media_player.sala
#        data_template:
#          volume_level: '{{ states.media_player.sala.attributes.volume_level - 0.06 }}'

#  - alias: Aumentar som sala
#    initial_state: true
#    trigger: 
#      platform: state
#      entity_id: sensor.0x000d6ffffebcd992_click
#      to: 'on'
#    action:
#      - service: media_player.volume_set
#        entity_id: media_player.sala
#        data_template:
#          volume_level: '{{ states.media_player.sala.attributes.volume_level + 0.06 }}'


#  - alias: Diminuir som cozinha
#    initial_state: true
#    trigger: 
#      platform: state
#      entity_id: sensor.0x000d6ffffebcd992_click
#      to: 'off'
#    action:
#      - service: media_player.volume_set
#        entity_id: media_player.cozinha
#        data_template:
#          volume_level: '{{ states.media_player.cozinha.attributes.volume_level - 0.06 }}'

#  - alias: Aumentar som cozinha
#    initial_state: true
#    trigger: 
#      platform: state
#      entity_id: sensor.0x000d6ffffebcd992_click
#      to: 'on'
#    action:
#      - service: media_player.volume_set
#        entity_id: media_player.cozinha
#        data_template:
#          volume_level: '{{ states.media_player.cozinha.attributes.volume_level + 0.06 }}'

#  - alias: Ligar radio de manha
#    initial_state: true
#    trigger:
#      - platform: time
#        at: "07:15:00"
#    condition:
#      - condition: state
#        entity_id: 'binary_sensor.workday_sensor'
#        state: 'on'
#    action:
#      - service: media_player.volume_set
#        data:
#          entity_id: media_player.sala
#          volume_level: '0.30'
#      -  service: media_player.play_media
#         data_template:
#          entity_id: media_player.sala
#          media_content_id: http://mcrscast1.mcr.iol.pt/comercial.mp3
#          media_content_type: 'audio/mp4'

  - alias: "Sala set Volume"
    initial_state: 'on'
    trigger:
      platform: state
      entity_id: input_number.sala_volume
    action:
      - service: media_player.volume_set
        data_template:
          entity_id: media_player.sala
          volume_level: >
            {{states('input_number.sala_volume') | int / 100}}

  - alias: "Sala Update Volume Slider"
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: sensor.sala_volume
      - platform: homeassistant
        event: start
    condition:
      condition: state
      entity_id: media_player.sala # receiver must be on to set the volume, without this would throw error on startup
      state: 'on'
    action:
      - delay:
          seconds: 3
      - service: input_number.set_value
        data_template:
          entity_id: input_number.sala_volume
          value: '{{ trigger.to_state.state }}'


  - alias: "Cozinha set Volume"
    initial_state: 'on'
    trigger:
      platform: state
      entity_id: input_number.cozinha_volume
    action:
      - service: media_player.volume_set
        data_template:
          entity_id: media_player.cozinha
          volume_level: >
            {{states('input_number.cozinha_volume') | int / 100}}

  - alias: "cozinha Update Volume Slider"
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: sensor.cozinha_volume
      - platform: homeassistant
        event: start
    condition:
      condition: state
      entity_id: media_player.cozinha # receiver must be on to set the volume, without this would throw error on startup
      state: 'on'
    action:
      - delay:
          seconds: 3
      - service: input_number.set_value
        data_template:
          entity_id: input_number.cozinha_volume
          value: '{{ trigger.to_state.state }}'

  - alias: "Ambiente set Volume"
    initial_state: 'on'
    trigger:
      platform: state
      entity_id: input_number.ambiente_volume
    action:
      - service: media_player.volume_set
        data_template:
          entity_id: media_player.som_ambiente
          volume_level: >
            {{states('input_number.ambiente_volume') | int / 100}}

  - alias: "ambiente Update Volume Slider"
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: sensor.som_ambiente_volume
      - platform: homeassistant
        event: start
    condition:
      condition: state
      entity_id: media_player.som_ambiente # receiver must be on to set the volume, without this would throw error on startup
      state: 'on'
    action:
      - delay:
          seconds: 3
      - service: input_number.set_value
        data_template:
          entity_id: input_number.ambiente_volume
          value: '{{ trigger.to_state.state }}'

###############################################################################
#                                                                             #
#                         G R O U P S                                         #
#                                                                             #
###############################################################################      
#group:

#  som_ambiente:
#    name: som Ambiente
#    entities:
#      - media_player.cozinha
#      - media_player.sala

Espero que vos seja util.

2 Curtidas

Legal !
Estava procurando por essa solução, tira uma duvida, como eu ativo ou inicio o script ?
Muito obrigado !

Podes fazer de 2 maneiras. Ou por comando de voz no Google home mini através de rotinas ou então no lovelace com um card onde metes o script. Tens outras automações onde tens o código mais elaborado. Mas o meu principal objectivo é não depender do acesso ao HA para poder tocar rádio.

Sobre a rotina como eu chamo o script ?
Valew

Na app home vais a configurações, depois mais configurações, assistente.
Depois vais a automação residencial e procuras os scripts que criaste para as rádios. Memórizas o nome direito do script para criar as rotinas.

Para as rotinas vais a opção em baixo rotinas e crias a rotina. Metes a frase que queres dizer depois vais a adicionar ação e escreves ligar rádio comercial (aqui metes o nome do teu script) guardas e já deve de funcionar.

Me desculpe Max !
Como eu crio o script dentro do Google ? eu criei dentro do Home Assistent. Pode me ajudar por favor.
Muito obrigado.

Vais ter de fazer a integração do Google home mini no Home assistant. Para isso basta seguir este tópico a partir do manual setup…

Vi o tópico e achei muito interessante a solução!

Eu tenho essa funcionalidade sem utilizar o home assistant, usando o aplicativo TuneIn que integra com o Google home e Alexa. Mas pra funcionar, a radio tem que estar disponível no tunein. Funciona bem, mas é passivel de erros pois no seu exemplo se pedir pra sintonizar na “radio comercial” existe mais de uma com esse nome. :smile:

Sua solução é bem interessante… vou testar!

1 Curtida