Denon volume automation error

Not sure it’s a concern but I have an automation for my Denon receiver volume and although visually in the system it seems to work I get a lot of errors in the logs. The whole automation block is:

- alias: "Denon Set Volume"
  initial_state: 'on'
  trigger:
    platform: state
    entity_id: input_number.denon_volume
  action:
    - service: media_player.volume_set
      data_template:
        entity_id: media_player.denon_x2200w
        volume_level: >
          {{states('input_number.denon_volume') | int / 100}}

- alias: "Denon Update Volume Slider"
  initial_state: 'on'
  trigger:
    - platform: state
      entity_id: sensor.denon_volume
    - platform: homeassistant
      event: start
  condition:
    condition: state
    entity_id: media_player.denon_x2200w # receiver must be on to set the volume, without this would throw error on startup
    state: 'on'
  action:
    - delay:
        seconds: 3
    - service: input_number.set_value
      data_template:
        entity_id: input_number.denon_volume
        value: '{{ trigger.to_state.state }}'

The errors I’m seeing are and not sure if it’s caused from the way HA loads things or delays in loading:

2020-11-23 12:47:34 ERROR (MainThread) [homeassistant.components.automation.denon_update_volume_slider] Denon Update Volume Slider: Error executing script. Unexpected error for call_service at pos 2: Error rendering data template: UndefinedError: 'dict object' has no attribute 'to_state'
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 351, in async_render
    render_result = compiled.render(kwargs)
  File "/usr/local/lib/python3.8/site-packages/jinja2/environment.py", line 1090, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.8/site-packages/jinja2/environment.py", line 832, in handle_exception
    reraise(*rewrite_traceback_stack(source=source))
  File "/usr/local/lib/python3.8/site-packages/jinja2/_compat.py", line 28, in reraise
    raise value.with_traceback(tb)
  File "<template>", line 1, in top-level template code
  File "/usr/local/lib/python3.8/site-packages/jinja2/sandbox.py", line 407, in getattr
    value = getattr(obj, attribute)
jinja2.exceptions.UndefinedError: 'dict object' has no attribute 'to_state'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 144, in async_prepare_call_from_config
    service_data.update(template.render_complex(config[conf], variables))
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 91, in render_complex
    return {
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 92, in <dictcomp>
    render_complex(key, variables): render_complex(item, variables)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 96, in render_complex
    return value.async_render(variables)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 353, in async_render
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: UndefinedError: 'dict object' has no attribute 'to_state'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 253, in _async_step
    await getattr(
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 432, in _async_call_service_step
    domain, service, service_data = async_prepare_call_from_config(
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 146, in async_prepare_call_from_config
    raise HomeAssistantError(f"Error rendering data template: {ex}") from ex
homeassistant.exceptions.HomeAssistantError: Error rendering data template: UndefinedError: 'dict object' has no attribute 'to_state'
2020-11-23 12:47:34 ERROR (MainThread) [homeassistant.components.automation.denon_update_volume_slider] While executing automation automation.denon_update_volume_slider
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 351, in async_render
    render_result = compiled.render(kwargs)
  File "/usr/local/lib/python3.8/site-packages/jinja2/environment.py", line 1090, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.8/site-packages/jinja2/environment.py", line 832, in handle_exception
    reraise(*rewrite_traceback_stack(source=source))
  File "/usr/local/lib/python3.8/site-packages/jinja2/_compat.py", line 28, in reraise
    raise value.with_traceback(tb)
  File "<template>", line 1, in top-level template code
  File "/usr/local/lib/python3.8/site-packages/jinja2/sandbox.py", line 407, in getattr
    value = getattr(obj, attribute)
jinja2.exceptions.UndefinedError: 'dict object' has no attribute 'to_state'

This is because you also trigger on HA restart and then trigger.to_state.state will fail.

Try rwplacing {{ trigger.to_state.state }} with {{ states('sensor.denon_volume') | int }}

Thanks for the help. Looks like it errors if I leave out the single quotes around the brackets I originally had, and if I add them back in errors due to the double set of single quotes. Does doing like:

'{{ states("sensor.denon_volume") | int }}'

work? Or does it have to be different like

"{{ states('sensor.denon_volume') | int }}"

?

or

'{{ states(sensor.denon_volume) | int }}'

or

'{{ states(''sensor.denon_volume'') | int }}'

Note around the sensor.denon_volume it’s two single quotes on each side if that is an escape value or something.

Wasn’t sure how the single vs double quotes work.

The first two you posted are fine, either only single quotes inside the curly brackets and double quotes outside OR double quotes outside the curly brackets and single quotes inside

Thanks Burningstone. Much appreciated. Now a bunch of errors are no longer showing in my log.

1 Like