Template - possible bug, definite inconsistency

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'

Try this:

{% set minutes_remaining = minutes_remaining | int + (time_remaining.split(':')[2] | int) / 60 %}

Thanks but no, that didn’t work. Same error…

This doesn’t work at all in version 0.89:

{{ 123.45 | round(0, 'ceil') }}

I don’t believe the ceil (ceiling) option is supported.

When I remove the ceil option, the template works:

Naturally, round without the ceiling option doesn’t round the number the same way.

I’m calling this a bug then, either it should be supported in HA (why wouldn’t it be?) or it shouldn’t be supported in the template editor.

Is that reasonable?

Just tested it in version 0.91 and it works in the Template Editor.

Screenshot%20from%202019-06-05%2007-37-03

I may have missed it in the Release Notes (or it was never documented) but round got upgraded either in 0.90 or 0.91.

I guess the error you are getting is unrelated to round.

@123
I took out the ceil and it still doesn’t work!

EDIT: Yes, it doesn’t look like it is.

Probably because your error has nothing to do with round and ceil. My observation was irrelevant to your issue and only served to teach me that the round function changed between 0.89 and 0.91.

1 Like

The error is in a template that has a - sign. Your template does not contain that. I think your template is fine. Start looking elsewhere in your config. Where a minus sign is.

TypeError: unsupported operand type(s) for -: 'float' and 'str'
                                           ^
                                           |
                    Errors always tell you the issue

And just for clarification. This error will be a float and a string in that order as well. If it was the other way around (str-float), you’d get a concatenation error. EDIT: was thinking of + sign, it will be another unsupported operand error with minus sign.

Also, @123 ceil is a valid option for round in the beta version (0.94)

Ha! Ha! I thought that was typo in the error output!!! I thought it was intended to be :-!!

I’ll start looking, thanks…

EDIT: And I’ve found it! Thank you.

1 Like