Automation trigger volume change

get rid of the to:‘on’:

trigger:
  - platform: state
    entity_id: media_player.tv
condition:
  - condition: template
    value_template:> 
      {% set from = trigger.from_state.attributes.volume_level | float%}
      {% set to = trigger.to_state.attributes.volume_level | float %}
      {{ to > from }}
action:
  - service: script.radio540

@petro I did, is not working. It’s a Samsung TV and i have to option to set the volume up and down from HA
As a service i have available in HA: media_player.volume_up and media_player.volume_down
i don’t have volume.level if that matters

Then this method wont work. You need to have a volume level to link to a slider bar.

You mean a slider insted of these:
Capture

I have a fell that i am missing something i have a : media_player.volume_set that can take a volume_level parameter

I have a sony android TV and I also have a nodemcu which trasmits IR to my tv via HTTP and mqqt commands.
It works, but the wife hates it as there is a delay from when you press the volume button and when it actually happens.

automation looks like this:

  • action:
    • data:
      entity_id: script.volumeup
      service: script.volumeup
    • data:
      entity_id: script.volumeup
      service: script.volumeup
    • data:
      entity_id: script.volumeup
      service: script.volumeup
      alias: Volume UP Sony to Sound System
      condition:
    • condition: template
      value_template: “{% set from = trigger.from_state.attributes.volume_level | float
      \ %}\n {% set to = trigger.to_state.attributes.volume_level | float %}\n
      \ {{ to > from }}”
      id: ‘1518382654591’
      trigger:
    • entity_id: media_player.sony_bravia_tv
      platform: state
  • action:
    • data:
      entity_id: script.volumedown
      service: script.volumedown
    • data:
      entity_id: script.volumedown
      service: script.volumedown
    • data:
      entity_id: script.volumedown
      service: media_player.volume_down
      alias: Volume DOWN Sony to Sound System
      condition:
    • condition: template
      value_template: “{% set from = trigger.from_state.attributes.volume_level | float
      \ %}\n {% set to = trigger.to_state.attributes.volume_level | float %}\n
      \ {{ to < from }}”
      id: ‘1518463933759’
      trigger:
    • entity_id: media_player.sony_bravia_tv
      platform: state

and scripts look like this
volumeup:
sequence:

  • service: mqtt.publish
    data_template:
    topic: ir_server/send
    payload: 3,10EF58A7,32,2
    volumedown:
    sequence:
  • service: mqtt.publish
    data_template:
    topic: ir_server/send
    payload: 3,10EF708F,32,2

hope this helps.

In general, if you are trying to detect or use volume level changes, it cannot be done if your media_player does not have the .level attribute.

@savage87 i have the script ready it uses a broadlink on IR instead. The problem is triggering the script when i press the volume up button on TV as @petro said guess is because i don’t have a volume slider instead i have buttons like the picture shows. Your Sony TV has a slider i presume?

Thanks for sharing this. I’ve adapted your code slightly just to detect volume changes. From this I keep the volume of my LG TV and my NVIDIA Shield TV in sync.

So when I use my TV remote to adjust the LG TV volume, the Nvidia Shield copies the volume level of the TV!!!

automation:
  - alias: Detect volume change on LG TV and sync to NVIDIA Shield TV
    trigger:
      - platform: state
        entity_id: media_player.lg_webos_smart_tv
    condition:
      condition: template
      value_template: >
        {% set from = trigger.from_state.attributes.volume_level | float %}
        {% set to = trigger.to_state.attributes.volume_level | float %}
        {{ to != from }}
    action:
      - service: media_player.volume_set
        data_template:
          entity_id: media_player.shield
          volume_level: "{{states.media_player.lg_webos_smart_tv.attributes.volume_level }}"
4 Likes

@petro
Dear i am using andorid box connected to homeassistant with android adb debug (https://github.com/hassio-addons/addon-adb)
i would like to use volume slider for it
can you please guide me,
thanks in adv.

Thnx so much! Helped me coordinate some sonos speakers that otherwise I couldnt using the tv remote control. Your Yaml didn’t work for me for some reason so I will leave this as an example for anyone else who has the same problem and google foos

- id: 'some number'
  alias: Detect volume change on Sonos LR and sync to Sonos LR2
  trigger:
  - platform: state
    entity_id: media_player.living_room
  condition:
  - condition: template
    value_template: >
      {% set from = trigger.from_state.attributes.volume_level | float %}
      {% set to = trigger.to_state.attributes.volume_level | float %}
      {{ to != from }}
  action:
    - service: media_player.volume_set
      data_template:
        entity_id: media_player.living_room_2
        volume_level: "{{states.media_player.living_room.attributes.volume_level }}"

Does anybody know how one could include mute in this sort of scenario?

Wow, thank you so much for this. My Sonos playbar sits behind my TV, and therefore the IR receiver is blocked, and this automation syncs it to my TV volume perfectly! Works instantly. Even better than when playbar get controlled via IR.

Hello,
How to change to be true also when volume is 0 for down and 100 for up ?
Thank you

it’s already doing that?

Hello,
When the Volume of TV is 0, the AMP volume controlled by IR remain bigger than zero, the condition needs to be also true for events with zero volume to bring also the AMP volume to zero, the same problem with volume up, the TV is at 100 and volume ion AMP is not at max level.

Not sure what I am doing wrong here, I can control the volume of my TV with HA (using the Sony Bravia Integration) and I can get it take action when I have specific volume thresholds set in the automation however when I am looking for changes in volume the automation conditions never pass.

For now I am just testing it I can get it working with flashing the lights in the bedroom, is there any way I can output the values used in the value template to a file? Just so I can debug what values it’s even using in the trigger.from and trigger.to states?

The automation below will trigger if I disable the condition and the volume goes over 15. However it is really slow and the light wont toggle for a good 5 seconds after the volume increases. The delay makes me wonder if that is why the condition fails.

Can anyone spot what I am doing wrong as the automation has worked for a number of people above.

trigger:
  - platform: state
    entity_id:
      - media_player.sony_kd_43x75wl_3
    enabled: true
  - platform: numeric_state
    entity_id:
      - media_player.sony_kd_43x75wl_3
    attribute: volume_level
    above: 0.15
    enabled: false
condition:
  - condition: template
    value_template: |-
      value_template: >
            {% set from = trigger.from_state.attributes.volume_level | float %}
            {% set to = trigger.to_state.attributes.volume_level | float %}
            {{ to != from }}
    enabled: true
action:
  - type: toggle
    entity_id: bedroom_sidelight
    domain: light
mode: restart