[Google Cast] How to Trigger Automation based on Volume of Android TV?

I cant find how to trigger an Automation based on when the volume level changes on Android TV / Google cast device?

I know its possible, my Homeseer system does it.

thx

Yes it’s 100% possible, but you most likely need to define template sensors to relay if the volume is turned up or down. a simpler method may be to created scripts for volume + and - and then have the automation react to the execution of those scripts.

This is a simple example to turn off a light if the volume is adjusted

alias: Example
description: ""
triggers:
  - trigger: state
    entity_id:
      - media_player.your_cast _device
    attribute: volume_level
conditions: []
actions:
  - action: light.turn_off
    metadata: {}
    data: {}
    target:
      entity_id: light.your_light

Script Example

alias: New automation
description: ""
triggers:
  - trigger: state
    entity_id:
      - script.volume_down
conditions: []
actions:
  - action: light.turn_off
    metadata: {}
    data: {}
    target:
      entity_id: light.pc_spot
mode: single

Thank you, I could not find that. I really want to know what the volume level is… the number itself since I like to ensure its not turned up past 30, and in the morning past 20.

Its unfortunate that many of the default Integrations are half-baked.

I see now! Let me try to isolate the cast device’s volume level. HA knows the volume level, but it’s not a straight forward attribute.

Nevermind, it’s available

volume_level: 0.5400000214576721

{{ state_attr('media_player.cast_device', 'volume_level')}}

1 Like

awesome, thank you! I’ll report back once I have some stuff running

You can create a template sensor as an entity or simply template the trigger in the automation. Quite a few ways! Let me know :smile:

{{ state_attr('media_player.your_cast_device', 'volume_level') < 0.3 }}

OK, it created the template sensor entities. They show ‘Unavailable’ … Does it need to get new data before that status goes away?

template:
  - sensor:
      - name: "Living Room TV Volume"
        state: >
          {% set volume = state_attr('media_player.googletv_livingroom', 'volume_level') %}
          {% if volume is not none %}
            {{ (volume * 100) | round(0) }}
          {% else %}
            {{ states('sensor.living_room_tv_volume') }}
          {% endif %}
        unit_of_measurement: "%"
        icon: mdi:volume-high
        availability: >
          {{ has_value('media_player.googletv_livingroom') and state_attr('media_player.googletv_livingroom', 'volume_level') is not none }}

also looks like I can set the volume using this

action:
  - service: media_player.volume_set
    target:
      entity_id:
        - media_player.googletv_livingroom
    data:
      volume_level: 0.98

For the automation you can use a template trigger like this as a test

alias: New automation
description: ""
triggers:
  - trigger: template
    value_template: "{{ state_attr('media_player.googletv_livingroom', 'volume_level') < 0.3 }}"
conditions: []
actions:
  - action: light.turn_off
    metadata: {}
    data: {}
    target:
      entity_id: light.your_light
mode: single

Understood… I also wanted to see the volume level in an Entity. Easier to see a number vs a slider.

You’d round it this way if you are trying to template a sensor

{{ (state_attr('media_player.shield', 'volume_level') *100) | round(0)}}

1 Like

Yes, no need to add a UOM, just deal with comparing straight values.

A good way to review the logic outcome is the Template editor in Developer Tools

{{ (state_attr('media_player.googletv_livingroom', 'volume_level') *100) | round(0)}}
{{ (state_attr('media_player.googletv_livingroom', 'volume_level') *100) | round(0) < 18}}