Script exception - TypeError: unsupported operand type(s) for *: 'NoneType' and 'int'

Hey guys.

I have an automation which runs a script on volume change of my AVR and sends an notification to my Android TV (Nvidia shield).

Sadly, sometimes it throws an conversation exception.

Script:

alias: VolumeOSD
sequence:
  - condition: device
    device_id: 32fb178b6d500a6c3d8485db757b214f
    domain: media_player
    entity_id: media_player.fernseher
    type: is_on
  - service: notify.android_tv_wohnzimmer
    data:
      title: Lautstärke
      message: >-
        {{ ((state_attr('media_player.lautsprecher', 'volume_level') *
        100)|round(1)) }}
      data:
        duration: 1
        position: top-right
        fontsize: large
        transparency: 0%
        color: indigo
        interrupt: 0
        icon:
          url: http://192.168.0.8:8123/local/speaker.png
mode: single
icon: mdi:amplifier

Exception:

2022-03-13 22:39:02 ERROR (MainThread) [homeassistant.components.script.volumeosd] VolumeOSD: Error executing script. Error for call_service at pos 2: Error rendering data template: TypeError: unsupported operand type(s) for *: 'NoneType' and 'int'
2022-03-13 22:39:02 ERROR (MainThread) [homeassistant] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 407, in async_render
    render_result = _render_with_context(self.template, compiled, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1814, in _render_with_context
    return template.render(**kwargs)
  File "/usr/local/lib/python3.9/site-packages/jinja2/environment.py", line 1291, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.9/site-packages/jinja2/environment.py", line 925, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<template>", line 1, in top-level template code

I think it’s an problem when the media_player (denon AVR) is switched on or off.

Can you please help me to do some error try & catch on volume_level?

thx
pOpY

{{ ((state_attr('media_player.lautsprecher', 'volume_level') | int * 100)|round(1)) }}

Not 100% sure, but i think you have to tell its an INT before you can do any math to it.
In the Devtools this has a valid output, your code has the warning.
Thats the best way to test templates anyway :wink:

Also, you should provide a default value, as the requested attribute might be unavailable (NoneType). And - when doing int * 100, the result won’t have any decimals, so round(1) makes no sense. I’d guess that volume_level might be a float.

So use

{{ state_attr('media_player.lautsprecher', 'volume_level') | float(default=0) * 100 | round(1)  }}
1 Like

Thank you all!
I think the “float(default=0)” does the trick and marked as solution.
Will try that at night.

With my template in dev tools and switched of AVR:

{{ ((state_attr('media_player.lautsprecher', 'volume_level') * 100)|round(1)) }}

result = my exception from OP:

TypeError: unsupported operand type(s) for *: 'NoneType' and 'int'

And your fixed template:

{{ state_attr('media_player.lautsprecher', 'volume_level') | float(default=0) * 100 | round(1)  }}

gives the correct result:

0.0

thanks a lot
pOpY

@Dujith @m0wlheld
My wife recorded the following picture last night from the tv:


Sadly it’s an bad picture quality but it showed 28.499999999999999.
This was triggered by the automation above.

Any hint’s why round(1) does’nt work?

thx

Could be, that the | operator only affects the preceeding 100. Use brackets (like you did beforehand):

{{ (state_attr('media_player.lautsprecher', 'volume_level') | float(default=0) * 100) | round(1)  }}

thanks a lot.
Tottally forgot about the brackets i have added in my first version :slight_smile:

I think that would fix it.