Put slider on inital volume value of receiver

I have an input_number configuration enabled to adjust the volume of my Denon AVR receiver.

Configuration.yaml

media_player:
  - platform: denonavr
    host: !secret denon_ip
    name: Denon AVR-x2100w
    show_all_sources: true
    timeout: 2

input_number:
  denon_volume:
    name: Volume
    initial: 0.3
    min: 0
    max: 1
    step: 0.01

Automations.yaml

- alias: Denon - Adjust Volume
  initial_state: true
  trigger:
    platform: state
    entity_id: input_number.denon_volume
  action:
    - service: media_player.volume_set
      data_template:
        entity_id: media_player.denon_avr_x2100w
        volume_level: '{{ trigger.to_state.state }}'

This is working great! But now I also want to have an automation that will adjust the volume slider itself when the receiver changes state. This however is not working… not sure how I can readout the attribute of the media player correctly to pass along the input_number?

Current try in automations.yaml

- alias: Denon - Adjust input number
  initial_state: true
  trigger:
    platform: state
    entity_id: media_player.denon_avr_x2100w
    to: 'playing'
  action:
    - service: input_number.set_value
      entity_id: input_number.denon_volume
      data_template:
        value: "{{ media_player.denon_avr_x2100w.volume_level }}"

Gives following exception in the log

ERROR (MainThread) [homeassistant.helpers.service] Error rendering data template: UndefinedError: 'media_player' is undefined

Anyone got this working?

here is my config for something similar:

> - alias: SetPanasonicVolumeSlider
>   trigger:
>     platform: state
>     entity_id: sensor.panasonic_current_volume
>   action:
>     service: input_number.set_value
>     data_template:
>       entity_id: input_number.panasonic_volume
>       value: '{{states.sensor.panasonic_current_volume.state}}'

i guess you’re just missing the “states.” prefix in the last line.

Now getting

ERROR (MainThread) [homeassistant.helpers.service] Error rendering data template: UndefinedError: 'homeassistant.core.State object' has no attribute 'volume_level'

With

- alias: Denon - Adjust input number
  initial_state: true
  trigger:
    platform: state
    entity_id: media_player.denon_avr_x2100w
    to: 'playing'
  action:
    - service: input_number.set_value
      data_template:
        entity_id: input_number.denon_volume
        value: "{{ states.media_player.denon_avr_x2100w.volume_level.state }}"

The media player does have the volume_level data property when I check the json structure… not sure how to use it here though…

Yeah, the thing is that volume_level ist not the state but only an sub-attribute.
If you look here: Automation Trigger - Home Assistant
you can see that they are accessing those attributes like this

state.attributes.battery

So based on that i’d guess that you need something like this:
states.media_player.denon_avr_x2100w.attributes.volume_level

@jo-me AWESOME! That did the trick… was not sure how to read out those attributes.

Now that it is working, I’m actually thinking this is not completely a correct setup.
Because I would rather want to register volume changes of the receiver itself as a trigger to the input_volume slider. Not sure if this can be monitored with a trigger though?

Yes that should also work. It is basically what the example in the link above does. Search for the snippet I posted on the page l linked and you’ll find an example on how to use an attribute as automation trigger.