How to determine if a certain radio station is playing for an automation condition

Hi there,

Here’s a quirky one. I have 5 x Google Homes and use TTS all around the house for announcing when gates open to when power is shutting off on zigbee sockets etc.

Now I have a radio station that that I play when the alarm is set on two of the media players. Problem is, if one of the other HA announcements goes off when the ‘alarm set radio station’ is playing the ‘announcement’ stops the radio station.

As a workaround at the moment, I’ve added a condition below my action list that checks to see if the alarm is ‘disarmed’ before proceeding with any announcements (alarm set, no one home, don’t speak), but due to the long list of actions, my Odroid C2 is taking 10~15s to get to the announcement which is too slow.

    - service: camera.snapshot
          entity_id: camera.front_porch
          filename: '/config/www/images/front_porch.jpg'
    - service: notify.hass_synochat
        message: "The doorbell was pushed at {{ states.sensor.time.state }}"
          file_url: "http://ipofhass:8123/local/images/front_porch.jpg"
    - service: notify.kodi
        title: "Home Assistant"
        message: "Someone is at the front door"
          displaytime: 7000
          icon: "info"
    - condition: state
      entity_id: alarm_control_panel.home_alarm
      state: 'disarmed'
    - service: tts.google_say
        - media_player.announce_four
        message: "Someone is at the front door"

What I’d love to do instead is check to see if one of the media players is playing my pre-programmed alarm radio station and if it is, do not proceed.

I’ve come up with this but don’t know whether that is right or what to do with it.

{{states.media_player.kitchen_home.attributes.media_content_type == '' }}"

Any feedback would be appreciated.

I answered similar query recently.
Look at this post and see if you can copy the same logic:

Thank you but I’m not quite sure how I’d use that to create a condition to test if a certain media player is playing a certain station.

Basically {{ state_attr('','media_content_id') }} will give you the ID of what’s playing.
So you can always check if it matches the radio you preset. And you can then use it to restore the radio playing once the announcement is finished:
Use a wait_template that waits for the media_player state to no longer be playing
Does that help?

Thanks. So would it look something like this? Sorry, bit confused as to where to define the ‘if’ statement in the condition. Your code does in fact tell me the station playing but how to I then say that if it is then stop?

  condition: template
  value_template: "{{ state_attr('','media_content_id') ''}}"


{{ state_attr('media_player.ensuite_speaker','media_content_id') == ''}}

Returns True when playing. Awesome! Thanks for your help.

Buggar! After all that, I realise that won’t work properly. I have a bunch of services under my actions when the doorbell is pushed including taking camera snapshots and sending notifications. Basically, I need a way to have the TTS notifications at the top of my service call list so they happen first but only if the alarm is disarmed or with your code for checking the radio station on one of the media players.

Problem is, I always want the other services called like the push notifications and camera snapshots regardless of the alarm state/radio station being played.

I’ll have to figure some way to have a script at the top that is actioned but I don’t want to have a build a script for every single TTS notification so that will take some thought.

You can move the condition as part of the actions. List the actions you always want to run, then add the condition and finally add the actions that should only run if the condition is true

Thanks, I’ve done that already and that’s my problem (a few posts up). TTS takes too long as that action is at the bottom of the list after the condition. I need to move it to the top. The condition will have to go into a notify script that calls values from the original action list. I’m trying to figure that out from Carlos’s code today. Believe he might be doing a video on that soon?