I have a template which works in the dev template editor but not when used as part of a script.
Can anyone help where I am going wrong please (or confirm there is bug somewhere)?
First of all the simplest case to prove what I want will work in principle, this works,
- service: script.set_new_sitting_room_light_off_time
data:
extension: 4
Now the interesting bit…
This works in the template editor,
data_template:
extension: >
{% set time_remaining = state_attr('timer.sitting_room_motion', 'remaining') %}
{% set minutes_remaining = time_remaining.split(':')[1] | int %}
{% set minutes_remaining = minutes_remaining + (time_remaining.split(':')[2] | int) / 60 %}
{{ minutes_remaining | round(0, 'ceil') | string }}
But when run in in HA
data_template:
extension: >
{% set time_remaining = state_attr('timer.sitting_room_motion', 'remaining') %}
{% set minutes_remaining = time_remaining.split(':')[1] | int %}
{% set minutes_remaining = minutes_remaining + (time_remaining.split(':')[2] | int) / 60 %}
{{ minutes_remaining | round(0, 'ceil') | string }}
it generates this error,
TypeError: unsupported operand type(s) for -: 'float' and 'str'
(As an aside I have tried with and without the | string
with the same effect but I think I am right anyway when I say that anything enclosed in {{ }}
is automatically converted to a string so it isn’t necessary?)
Thanks for any help and here is the entire Traceback in case it helps
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/homeassistant/core.py", line 1150, in _safe_execute
await self._execute_service(handler, service_call)
File "/usr/local/lib/python3.7/site-packages/homeassistant/core.py", line 1163, in _execute_service
await handler.func(service_call)
File "/usr/local/lib/python3.7/site-packages/homeassistant/components/script/__init__.py", line 114, in service_handler
context=service.context)
File "/usr/local/lib/python3.7/site-packages/homeassistant/components/script/__init__.py", line 172, in async_turn_on
kwargs.get(ATTR_VARIABLES), context)
File "/usr/local/lib/python3.7/site-packages/homeassistant/helpers/script.py", line 131, in async_run
await self._handle_action(action, variables, context)
File "/usr/local/lib/python3.7/site-packages/homeassistant/helpers/script.py", line 210, in _handle_action
action, variables, context)
File "/usr/local/lib/python3.7/site-packages/homeassistant/helpers/script.py", line 299, in _async_call_service
context=context
File "/usr/local/lib/python3.7/site-packages/homeassistant/helpers/service.py", line 89, in async_call_from_config
domain, service_name, service_data, blocking=blocking, context=context)
File "/usr/local/lib/python3.7/site-packages/homeassistant/core.py", line 1141, in async_call
self._execute_service(handler, service_call))
File "/usr/local/lib/python3.7/site-packages/homeassistant/core.py", line 1163, in _execute_service
await handler.func(service_call)
File "/usr/local/lib/python3.7/site-packages/homeassistant/components/script/__init__.py", line 114, in service_handler
context=service.context)
File "/usr/local/lib/python3.7/site-packages/homeassistant/components/script/__init__.py", line 172, in async_turn_on
kwargs.get(ATTR_VARIABLES), context)
File "/usr/local/lib/python3.7/site-packages/homeassistant/helpers/script.py", line 131, in async_run
await self._handle_action(action, variables, context)
File "/usr/local/lib/python3.7/site-packages/homeassistant/helpers/script.py", line 210, in _handle_action
action, variables, context)
File "/usr/local/lib/python3.7/site-packages/homeassistant/helpers/script.py", line 299, in _async_call_service
context=context
File "/usr/local/lib/python3.7/site-packages/homeassistant/helpers/service.py", line 80, in async_call_from_config
config[CONF_SERVICE_DATA_TEMPLATE], variables))
File "/usr/local/lib/python3.7/site-packages/homeassistant/helpers/template.py", line 62, in render_complex
for key, item in value.items()}
File "/usr/local/lib/python3.7/site-packages/homeassistant/helpers/template.py", line 62, in <dictcomp>
for key, item in value.items()}
File "/usr/local/lib/python3.7/site-packages/homeassistant/helpers/template.py", line 63, in render_complex
return value.async_render(variables)
File "/usr/local/lib/python3.7/site-packages/homeassistant/helpers/template.py", line 191, 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 9, in top-level template code
TypeError: unsupported operand type(s) for -: 'float' and 'str'