So I’ve been using a set of template sensors for calculating my net electrical production/consumption and for some reason since updating to 0.115 they’ve been flaky. By flaky I mean that they don’t all seem to evaluate and I’m not sure why. Right now, I’m running 0.115.1 and my energy net meters look like this:
However, the equation used for the Sense Net device looks like this:
Running the first if statement through the Developer Tools–>Template yields False which is correct and I shouldn’t end up with an “Unknown” value. Also, when I put the second statement into the Developer Tools–>Template it shows a good value. This was working great until 0.115 (for months by the way).
Log Details (ERROR)
Logger: homeassistant
Source: helpers/template.py:285
First occurred: 6:14:08 PM (4 occurrences)
Last logged: 6:14:08 PM
Error doing job: Task exception was never retrieved
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/components/template/template_entity.py", line 264, in _async_template_startup
result_info = async_track_template_result(
File "/usr/src/homeassistant/homeassistant/helpers/event.py", line 792, in async_track_template_result
tracker.async_setup()
File "/usr/src/homeassistant/homeassistant/helpers/event.py", line 518, in async_setup
self._info[template] = template.async_render_to_info(variables)
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 306, in async_render_to_info
render_info._result = self.async_render(variables, **kwargs)
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 285, in async_render
return compiled.render(kwargs).strip()
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 4, in top-level template code
TypeError: unsupported operand type(s) for -: 'str' and 'str'
Looks like it has to do with the async changes that were recently introduced. I’ll try the int anyway.
An entity’s state is always a string value but round() can round up a numeric string value. So if you have a numeric string like '34.6' then round will convert it to an integer value 35.
However, what it cannot do is round a non-numeric string like unavailable. It will simply report the original string unchanged.
Your template subtracts two rounded state values. If those two states are unavailable that’s what will cause the subtraction to fail (because they’re both strings). That’s what the error message means by:
`TypeError: unsupported operand type(s) for -: ‘str’ and ‘str’
Enhance your template so it prevents calculation if the state values are unavailable.
His icon template is fine. It should always return one or the other result regardless of the state on each sensor because he’s casting them as ints. If the int cast fails, the result will be zero. He’s getting the error because he’s trying to round a string in his value_template.
No, I think it makes sense. I have to be more careful with my usage of round() and I need to employ the use of availability_templates on all my templated switches, sensors, and whatnot. I’ve already begun.
Was that a change in the past 12 months or so? I know that casting a string to an int used to cause an exception as you helped me fix it once upon a time