Automation to mute radio when commercials play

I’ve recently purchased a Google Home Mini and am playing Pandora through it. They seem to jack up the volume on the ads. So I’m trying to fix that. Here’s the code I’ve got working (via Automations), but watching the logs show that it’s firing every second! There’s gotta be a better way. I looked into AppDaemon and that’s way over my head. Any ideas? (Btw this the format that the Automation helper tool puts it in… sorry the order is confusing!)
Basically what it does is watch to see if the media_duration value is < 60 seconds. If it is, then it’s an ad and mutes it. I was trying to use booleans but that didn’t really work.

Thanks!

- action:
 - data:
     entity_id: media_player.kitchen_speaker
     is_volume_muted: 'yes'
   service: media_player.volume_mute
 - data:
     entity_id: input_boolean.pandora_ad
   service: input_boolean.turn_on
 alias: Pandora Ad
 condition:
 - below: '60'
   condition: numeric_state
   entity_id: media_player.kitchen_speaker
   value_template: '{{ states.media_player.kitchen_speaker.attributes.media_duration
     }}'
 id: '1513043268546'
 trigger:
 - entity_id: media_player.kitchen_speaker
   platform: state
- action:
 - data:
     entity_id: input_boolean.pandora_ad
   service: input_boolean.turn_off
 - data:
     entity_id: media_player.kitchen_speaker
     is_volume_muted: 'no'
   service: media_player.volume_mute
 alias: Pandora Ad Off
 condition:
 - above: '60'
   condition: numeric_state
   entity_id: media_player.kitchen_speaker
   value_template: '{{ states.media_player.kitchen_speaker.attributes.media_duration
     }}'
 id: '1513050117670'
 trigger:
 - entity_id: media_player.kitchen_speaker
   platform: state

You could try a NUMERIC STATE as the trigger instead of the condition.

automation:
  trigger:
    platform: numeric_state
    entity_id: sensor.temperature
    # Optional
    value_template: '{{ state.attributes.battery }}'
    # At least one of the following required
    above: 17
    below: 25

    # If given, will trigger when condition has been for X time.
    for:
      hours: 1
      minutes: 10
      seconds: 5

I tried that at first, actually. It doesn’t work as a trigger, for some reason. :frowning:

- action:
  - data:
      entity_id: switch.chrpresent_lights_3
    service: switch.toggle
  alias: Testing Pandora
  condition: []
  id: '1513095070762'
  trigger:
  - below: '60'
    entity_id: media_player.kitchen_speaker
    platform: numeric_state
    value_template: ''{{ states.media_player.kitchen_speaker.attributes.media_duration }}''

You have double single quotes round the value_template, should be ----> value_template: ‘{{ states.media_player.kitchen_speaker.attributes.media_duration }}’

However not sure how much difference it will make.

I was looking at that. The automation editor is a little funky, I think.

This looks more promising:

value_template: '{{ states.media_player.kitchen_speaker.attributes.media_duration
      | int }}'

Adding the | int part seems to fix an unusual error that I was getting:
WARNING (MainThread) [homeassistant.helpers.condition] Value cannot be processed as a number: '221'

For stuff like this, I sometime use the templates page from the front end and just put what you want to test in to see what it returns.

Like

{{ states.media_player.kitchen_speaker.attributes.media_duration
      | int }}

or

{% if states('media_player.kitchen_speaker.attributes.media_duration') | float > 60 %}
  It is an ad!
{%endif %}

Ultimately this is what I ended up using (and it works well):

- action:
  - data:
      entity_id: media_player.kitchen_speaker
      is_volume_muted: 'yes'       
    service: media_player.volume_mute
  alias: Pandora -> Ad On (Mute)
  condition: []
  id: '1513095070762'
  trigger:
  - below: '60'
    entity_id: media_player.kitchen_speaker
    platform: numeric_state
    value_template: '{{ states.media_player.kitchen_speaker.attributes.media_duration
      | int }}'

- action:
  - data:
      entity_id: media_player.kitchen_speaker
      is_volume_muted: 'no' 
    service: media_player.volume_mute  
  alias: Pandora -> Ad Off (Un-Mute)
  condition: []
  id: '1513043268546'
  trigger:
  - above: '60'       
    entity_id: media_player.kitchen_speaker
    platform: numeric_state
    value_template: '{{ states.media_player.kitchen_speaker.attributes.media_duration
      | int }}'
1 Like

I like the idea of using if statements! I might have to redo this…!
Thanks

I get most my examples here

I hate to necro this, (but since it so close to Halloween anyway), can we put a “delay” statement for the remaining duration of the media to keep it from firing every second?

you sure can!

1 Like

it probably has something to do with how HA changed the way scripts are handled. I think you can make this script mode single and it won’t run more than once if you put a 30 sec delay at the bottom.