I have an automation to trigger on the volume of a Bose SoundTouch speaker; if the volume surpasses 70% it turns it down to 60% (I have a toddler who loves to max out the volume). To do this, I have the following configuration:
- alias: "Volume Too High in the Master Bedroom"
initial_state: true
hide_entity: true
trigger:
- platform: template
value_template: "{{ states.media_player.master_bedroom.attributes.volume_level > 0.7 }}"
action:
- service: media_player.volume_set
entity_id: media_player.master_bedroom
data_template:
volume_level: "0.6"
It does work as intended. However, I get the below error associated with this in my logs; I’m wondering if there was a cleaner way I need to configure the above?
Error during template condition: UndefinedError: 'mappingproxy object' has no attribute 'volume_level'
I spoke too soon, your way still worked and that last error was gone, but a different error crops up now. Is either anything to really worry about?
Error doing job: Exception in callback <function async_track_state_change.<locals>.state_change_listener at 0x7fc07f1597b8>
Traceback (most recent call last):
File "uvloop/cbhandles.pyx", line 64, in uvloop.loop.Handle._run
File "/usr/local/lib/python3.7/site-packages/homeassistant/helpers/event.py", line 82, in state_change_listener
event.data.get('new_state'))
File "/usr/local/lib/python3.7/site-packages/homeassistant/core.py", line 333, in async_run_job
target(*args)
File "/usr/local/lib/python3.7/site-packages/homeassistant/helpers/event.py", line 103, in template_condition_listener
template_result = condition.async_template(hass, template, variables)
File "/usr/local/lib/python3.7/site-packages/homeassistant/helpers/condition.py", line 331, in async_template
value = value_template.async_render(variables)
File "/usr/local/lib/python3.7/site-packages/homeassistant/helpers/template.py", line 140, in async_render
return self._compiled.render(kwargs).strip()
File "/usr/local/lib/python3.7/site-packages/jinja2/asyncsupport.py", line 76, in render
return original_render(self, *args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/jinja2/environment.py", line 1008, in render
return self.environment.handle_exception(exc_info, True)
File "/usr/local/lib/python3.7/site-packages/jinja2/environment.py", line 780, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.7/site-packages/jinja2/_compat.py", line 37, in reraise
raise value.with_traceback(tb)
File "<template>", line 1, in top-level template code
TypeError: '>' not supported between instances of 'NoneType' and 'float'
Error during template condition: UndefinedError: 'mappingproxy object' has no attribute 'volume_level'
and the second error:
TypeError: '>' not supported between instances of 'NoneType' and 'float'
appear to be the result of a problem with media_player.master_bedroom. The problem is that, sometimes, volume_level does not exist.
It explicitly says that in the first error and the second one refers to NoneType which would the result of state_attr() unable to find the attribute volume_level.
Either way, it’s not good.
I don’t have enough experience with the media_player component to guess why this may be happening. Hopefully someone else can jump in and explain what’s going on here.
I hadn’t quite thought of it that way, but now that you say it, that makes sense. I was thinking of it from a syntax perspective; as in, I was trying to tell it to do something that was unsupported or depreciated (as this error didn’t log until 0.88 or 0.89). But perhaps the original way I had it, as well as your suggestion were correct; HA just couldn’t see what it was expecting in regards to the media_player properties.
Thanks again by the way; not only did I learn a new way to look at what the logs were telling me, but I learned a different way to build the template and action. I’m interested in learning more about the media_player properties and seeing if it’s the Bose SoundTouch (which I’d love an excuse to replace) or not; if anyone else wants to chime in, I’m all ears.
I think I figured it out (without completely understanding WHY it is “fixed”); I had been allowing HA to discover the media_player devices on its own rather than declaring them in configuration.yaml under media_player: individually. I was reading up on the SoundTouch component page and decided to declare each of the four speakers by their static IP (via DHCP reservation). Once I restarted HA, the errors went away; I’ll keep an eye on it.