Here is what I currently have to do:
command: " {% ... -%} {%- ... -%} {%- ... -%} {%- ... -%} {{ ... }}"
instead of
command: >-
{% ... -%}
{%- ... -%}
{%- ... -%}
{%- ... -%}
{{ ... }}
Update:
No, this does not work too.
command: "{% set SENSOR_SETTINGS = 'sensor.xxx' -%}{{state_attr(SENSOR_SETTINGS,'command_ip_address')}}"
Although this is a one-line template.
2022-07-22 04:53:07 ERROR (SyncWorker_32) [homeassistant.components.command_line.sensor] Error rendering command template: UndefinedError: 'SENSOR_SETTINGS' is undefined
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 421, in async_render
render_result = _render_with_context(self.template, compiled, **kwargs)
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1906, in _render_with_context
return template.render(**kwargs)
File "/usr/local/lib/python3.10/site-packages/jinja2/environment.py", line 1301, in render
self.environment.handle_exception()
File "/usr/local/lib/python3.10/site-packages/jinja2/environment.py", line 936, in handle_exception
raise rewrite_traceback_stack(source=source)
File "<template>", line 1, in top-level template code
File "/usr/local/lib/python3.10/site-packages/jinja2/sandbox.py", line 393, in call
return __context.call(__obj, *args, **kwargs)
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2054, in wrapper
return func(hass, *args[1:], **kwargs)
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1371, in state_attr
if (state_obj := _get_state(hass, entity_id)) is not None:
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 921, in _get_state
return _get_template_state_from_state(hass, entity_id, hass.states.get(entity_id))
File "/usr/src/homeassistant/homeassistant/core.py", line 1308, in get
return self._states.get(entity_id.lower())
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1932, in _fail_with_undefined_error
raise ex
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1923, in _fail_with_undefined_error
return super()._fail_with_undefined_error(*args, **kwargs)
jinja2.exceptions.UndefinedError: 'SENSOR_SETTINGS' is undefined
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/components/command_line/sensor.py", line 161, in update
rendered_args = args_compiled.render(args_to_render)
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 393, in render
).result()
File "/usr/local/lib/python3.10/concurrent/futures/_base.py", line 446, in result
return self.__get_result()
File "/usr/local/lib/python3.10/concurrent/futures/_base.py", line 391, in __get_result
raise self._exception
File "/usr/src/homeassistant/homeassistant/util/async_.py", line 64, in run_callback
future.set_result(callback(*args))
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 423, in async_render
raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: UndefinedError: 'SENSOR_SETTINGS' is undefined
Bypassing:
command: >-
echo ;
{% set SENSOR_SETTINGS = 'sensor.xxx' -%}{{ state_attr(SENSOR_SETTINGS,'command_ip_address') }}