Describe the problem as follows. I have coded general engineering controller for general heat generator using Jinja2 and variable component as object structure - sort of PASCAL native to HA automation environment. Generally, it works as follows: certain events or request change object’s fields, these changes trigger solo automation, which detects the field triggered, and based on that decides which script to call. Now, back to the issue. Every 2 seconds I call an update script on the object. I save the time of the call in the field ‘externals_update’. I also save the code of the calling action with the time stamp in the string ‘updates_in_que’ as follows: action1Qtime_stamp1#action2Qtimestamp2#…
This field I use to check, when calling script with action passed as a parameter, if this action is already being emposed on the object, so, if this is present in the string, I do not call the script executing specific action again, and finish the trigger naturally with no action. When the first instance of ‘action1’ is done, it erases itself from the string ‘updates_in_que’ as follows:
- It converts string to list using split(’#’). Thus I have the list of objects of type ‘action’Q’timestamp’.
- It converts every element of this list into list of 2 using split(‘Q’) and reorders whole thing into single list. Thus I have the list of type [‘action1’, ‘timestamp1’, ‘action2’, ‘timestamp2’, …
- It detects where action1 is in order to errase it and eleminates item I and I+1 from the list.
In most of the ocasions it work perfectly. But sometimes whole thing crushes because of the strange error the origine of which I struggle to understand. Basically, it says that when I chack item[i+1], if item[i] is o.k., than it does not exist. But I do the check for that bfore running the script. Below is the data of error and some scripts I run:
Logger: homeassistant.components.automation.inzhiniring_zapusk_oprosa_vneshnikh_ustroistv_sviazannykh_s_obektom_kontur_otopleniia_struktura
Source: helpers/service.py:190
Integration: Автоматизация (documentation, issues)
First occurred: 16:29:16 (3 occurrences)
Last logged: 16:29:17
Инжиниринг, запуск опроса внешних устройств связанных с объектом контур отопления (структура) : Choose at step 2: choice 1: Error executing script. Unexpected error for call_service at pos 1: Error rendering data template: UndefinedError: list object has no element 1
Инжиниринг, запуск опроса внешних устройств связанных с объектом контур отопления (структура) : Error executing script. Unexpected error for choose at pos 2: Error rendering data template: UndefinedError: list object has no element 1
While executing automation automation.inzhiniring_zapusk_oprosa_vneshnikh_ustroistv_sviazannykh_s_obektom_kontur_otopleniia_struktura
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 353, in async_render
render_result = compiled.render(kwargs)
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 79, in top-level template code
jinja2.exceptions.UndefinedError: list object has no element 1
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 188, in async_prepare_call_from_config
service_data.update(template.render_complex(config[conf], variables))
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 93, in render_complex
return {
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 94, in <dictcomp>
render_complex(key, variables): render_complex(item, variables)
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 93, in render_complex
return {
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 94, in <dictcomp>
render_complex(key, variables): render_complex(item, variables)
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 98, in render_complex
return value.async_render(variables)
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 355, in async_render
raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: UndefinedError: list object has no element 1
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 254, in _async_step
await getattr(
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 467, in _async_call_service_step
await self._async_run_long_action(service_task)
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 426, in _async_run_long_action
long_task.result()
File "/usr/src/homeassistant/homeassistant/core.py", line 1445, in async_call
task.result()
File "/usr/src/homeassistant/homeassistant/core.py", line 1480, in _execute_service
await handler.job.target(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 1033, in async_run
await asyncio.shield(run.async_run())
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 714, in async_run
await super().async_run()
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 246, in async_run
await self._async_step(log_exceptions=False)
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 254, in _async_step
await getattr(
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 603, in _async_choose_step
await self._async_run_script(script)
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 677, in _async_run_script
await self._async_run_long_action(
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 426, in _async_run_long_action
long_task.result()
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 1033, in async_run
await asyncio.shield(run.async_run())
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 246, in async_run
await self._async_step(log_exceptions=False)
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 254, in _async_step
await getattr(
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 436, in _async_call_service_step
domain, service_name, service_data = service.async_prepare_call_from_config(
File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 190, in async_prepare_call_from_config
raise HomeAssistantError(f"Error rendering data template: {ex}") from ex
homeassistant.exceptions.HomeAssistantError: Error rendering data template: UndefinedError: list object has no element 1
Script:
sequence:
- choose:
- conditions:
- condition: template
value_template: |-
{% set state = states[circulation_loop] %}
{% set ns = namespace( list = 'undef', res = 'undef') %}
{% set ns.list = state.attributes.updates_in_que.split('#') %}
{% set ns.res = [] %}
{% for i in range(ns.list|length) %}
{% set new_item = ns.list[i].split('Q') %}
{% set ns.res = ns.res + new_item %}
{% endfor %}
{{ ns.res|length >= 2 and ns.res|length % 2 == 0 }}
sequence:
- service: variable.set_variable
data:
variable: |-
{% set state = states[circulation_loop] %}
{{state.object_id}}
attributes:
que_processing: |-
{% set state = states[circulation_loop] %}
{{state.attributes.updates_in_que}}
- service: variable.set_variable
data:
variable: |-
{% set state = states[circulation_loop] %}
{{state.object_id}}
attributes:
updates_in_que: >-
{% set var = states[circulation_loop] %}
{% set ns = namespace( list = 'undef', res = 'undef', skip =
0, time_scale = 0 ) %}
{% set ns.time_scale = var.attributes.time_scale|float %}
{% set ns.list = var.attributes.updates_in_que.split('#') %}
{% set ns.res = [] %}
{% for i in range(ns.list|length) %}
{% set new_item = ns.list[i].split('Q') %}
{% set ns.res = ns.res + new_item %}
{% endfor %}
{% set ns.list = [] %}
{% for i in range(ns.res|length) %}
{% if ns.res[i] in ['upd_integral',
'start_tracing',
'set_idle',
'finish_set_vlv',
'upd_demand',
'upd_set_temp',
'upd_weather_temp',
'pump_on',
'pump_off',
'upd_curve_tilt',
'start_emrg_by_temp',
'stop_emrg_by_temp',
'upd_max_temp',
'upd_min_temp'] %}
{% if as_timestamp(now())|round - ns.res[i+1]|float > 10 %}
{% set ns.skip = 2 %}
{% endif %}
{% elif ns.res[i] in ['ext_upd',
'calibr',
'set_new_vlv_pos',
'set_off',
'set_sleep',
'set_unavlbl'] %}
{% if as_timestamp(now())|round - ns.res[i+1]|float > 3*ns.time_scale %}
{% set ns.skip = 2 %}
{% endif %}
{% endif %}
{% if ns.skip|int == 0 %}
{% set tmp = ns.res[i] %}
{% set new = [tmp] %}
{% set ns.list = ns.list+new %}
{% else %}
{% set ns.skip = ns.skip|int - 1 %}
{% endif %}
{% endfor %}
{% set ns.res = [] %}
{% for i in range (ns.list|length) if i % 2 == 0 %}
{% set tmp = ns.list[i] +'Q'+ns.list[i+1] %}
{% set new = [tmp] %}
{% set ns.res= ns.res+new %}
{% endfor %}
{{ns.res|join('#')}}
- service: variable.set_variable
data:
variable: |-
{% set state = states[circulation_loop] %}
{{state.object_id}}
attributes:
que_processing: none
default: []
mode: queued
max: 1000
The biggest issue is not, that it does not complete the current run - it stops calling this script ever again, unless reboted.
Does anyone have any idea why the error with list state might accure and if there is the way to restart calling sequence after the crush?