Scripts with service.data_template throw errors since 0.115

Hey there,

I realized that since 0.115 my scripts start throwing exceptions in the logs and I’m not quite sure. I have two scripts that build a list and pass them to a python_script. But since 0.115 it seems to not like the “>” anymore:

2020-09-24 22:14:29 ERROR (MainThread) [homeassistant.components.automation.apartment_evening_time_close_roller_shutters_turn_on_lights] While executing automation automation.apartment_evening_time_close_roller_shutters_turn_on_lights
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/automation/__init__.py", line 426, in async_trigger
    await self.action_script.async_run(
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 944, in async_run
    await asyncio.shield(run.async_run())
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 198, in async_run
    await self._async_step(log_exceptions=False)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 206, in _async_step
    await getattr(
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 552, in _async_choose_step
    await self._async_run_script(script)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 626, in _async_run_script
    await self._async_run_long_action(
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 375, in _async_run_long_action
    long_task.result()
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 944, in async_run
    await asyncio.shield(run.async_run())
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 198, in async_run
    await self._async_step(log_exceptions=False)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 206, in _async_step
    await getattr(
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 416, in _async_call_service_step
    await self._async_run_long_action(service_task)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 375, in _async_run_long_action
    long_task.result()
  File "/usr/src/homeassistant/homeassistant/core.py", line 1315, in async_call
    task.result()
  File "/usr/src/homeassistant/homeassistant/core.py", line 1350, in _execute_service
    await handler.func(service_call)
  File "/usr/src/homeassistant/homeassistant/components/script/__init__.py", line 215, in service_handler
    await script_entity.async_turn_on(
  File "/usr/src/homeassistant/homeassistant/components/script/__init__.py", line 320, in async_turn_on
    await coro
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 944, in async_run
    await asyncio.shield(run.async_run())
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 198, in async_run
    await self._async_step(log_exceptions=False)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 206, in _async_step
    await getattr(
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 385, in _async_call_service_step
    domain, service, service_data = async_prepare_call_from_config(
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 138, in async_prepare_call_from_config
    service_data.update(template.render_complex(config[conf], variables))
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 81, in render_complex
    return {
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 82, in <dictcomp>
    render_complex(key, variables): render_complex(item, variables)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 86, in render_complex
    return value.async_render(variables)
  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 5, in top-level template code
TypeError: '>' not supported between instances of 'NoneType' and 'int'

That’s the one of the script throwing this error:

lights_set_effect_if_online:
  sequence:
  - service: python_script.lights_set_effect
    data_template:
      lights: >
        {% set lights = namespace(list="") %}
        {%- for light in ["light.bedroom_bedside_lamp_andy", "light.bedroom_bedside_lamp_kristina", "light.dining_room_pendant_light_ambilight", "light.living_room_ceiling_light_ambilight", "light.living_room_big_lamp", "light.living_room_small_lamp", "light.office_desk_lamp"] -%}
          {%- if is_state(light, "on") -%}
            {% set lights.list = lights.list + light + "," %}
          {%- endif -%}
        {%- endfor -%}
        {{ lights.list }}
      effect: "{{ effect }}"

That’s how it’s called in an automation:

action:
  - choose:
    - conditions:
      - condition: state
        entity_id: group.everybody
        state: home
      sequence:
      - scene: scene.living_room_tv_mode
      - delay: 00:00:01
      - service: script.lights_set_effect_if_online
        data:
          effect: "Evening Time"
      - delay: 00:00:05
      - service: script.close_roller_shutters
    default:
    - service: script.close_roller_shutters

I checked the Blog post and the breaking changes section, but I couldn’t find something about that.

Is this a bug or am I hitting another breaking change that was introduced in an earlier release?

Thanks and greetings,

Andy!

Of course, after posting this, they don’t do this anymore… What the? Hopefully I can reproduce this again…

The error is message says that the > is between a none_type and an int. The none_type will be why it is failing, but the script you pasted doesn’t contain any operators on ints, so I would start with automation.apartment_evening_time_close_roller_shutters_turn_on_lights as mentioned in the error message.

1 Like

Hrrmm, I can’t spot anything wrong with the automation either which could be about an int, neither am I using a “>” there…

I posted the actions part of the automation above. The rest of it doesn’t have that much more…

- id: apartment_evening_time
  alias: "[Apartment] Evening time: Close roller shutters, turn on lights"
  trigger:
  - platform: state
    entity_id: sun.sun
    to: below_horizon
  condition: []
  action:
  - choose:
    - conditions:
      - condition: state
        entity_id: group.everybody
        state: home
      sequence:
      - scene: scene.living_room_tv_mode
      - delay: 00:00:01
      - service: script.lights_set_effect_if_online
        data:
          effect: "Evening Time"
      - delay: 00:00:05
      - service: script.close_roller_shutters
    default:
    - service: script.close_roller_shutters

The interesting thing is, that it feels like it still works. At least I don’t realize that something didn’t work… It’s “just” the exception