core: command_line sensor with templated command: inconsistency (still)
The problem
Last year I opened an issue here and one day it was closed automatically. Probably I should have monitored it once a week to prevent it become stale and closed. After almost a year a situation is almost the same. Details are below.
Here is the old issue: https://github.com/home-assistant/core/issues/58847
Templates in command-line sensors do not work as they do in template sensors.
Case 1: This does not contain templates and DOES WORK:
- platform: command_line
name: "test command line2"
command: "echo hello 123"
scan_interval: 9999
Output is:
hello 123
Case 2: This contains templates and DOES WORK:
- platform: command_line
name: "test command line"
command: "echo hello {{ states('sun.sun') }}"
scan_interval: 9999
Output is:
hello below_horizon
Case 3: This contains templates and DOES NOT WORK:
- platform: command_line
name: "test command line3"
command: >-
{% if is_state("input_boolean.test_boolean","on") -%}
{%- set KEY = "m" -%}
{%- else -%}
{%- set KEY = "h" -%}
{%- endif -%}
{{ "free -" + KEY }}
scan_interval: 9999
Output is:
unknown
Error in a log:
2022-07-22 03:16:39 ERROR (SyncWorker_15) [homeassistant.components.command_line.sensor] Error rendering command template: TemplateSyntaxError: Encountered unknown tag 'else'.
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 370, in ensure_valid
self._compiled_code = self._env.compile(self.template) # type: ignore[no-untyped-call]
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2158, in compile
cached = self.template_cache[source] = super().compile(source)
File "/usr/local/lib/python3.10/site-packages/jinja2/environment.py", line 768, in compile
self.handle_exception(source=source_hint)
File "/usr/local/lib/python3.10/site-packages/jinja2/environment.py", line 936, in handle_exception
raise rewrite_traceback_stack(source=source)
File "<unknown>", line 1, in template
jinja2.exceptions.TemplateSyntaxError: Encountered unknown tag 'else'.
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 415, in async_render
compiled = self._compiled or self._ensure_compiled(limited, strict)
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 601, in _ensure_compiled
self.ensure_valid()
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 372, in ensure_valid
raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: TemplateSyntaxError: Encountered unknown tag 'else'.
Case 4: This contains templates and DOES WORK (last year it worked only if “state=ON”):
- platform: command_line
name: "test command line4"
command: >-
free -
{%- if is_state("input_boolean.test_boolean","on") -%}
{%- set KEY = "m" -%}
{%- else -%}
{%- set KEY = "h" -%}
{%- endif -%}
{{ KEY }}
scan_interval: 9999
Output is:
total used free shared buff/cache availableMem: 3.8G 1.4G 873.9M 10.3M 1.5G 2.1GSwap: 975.0M 12.0K 975.0M
or
total used free shared buff/cache availableMem: 3898 1471 877 10 1550 2168Swap: 975 0 975
Case 5: This contains templates and DOES WORK (last year it worked only if “state=ON”):
- platform: command_line
name: "test command line5"
command: >-
free -
{%- if is_state("input_boolean.test_boolean","on") -%}
m
{%- else -%}
h
{%- endif %}
scan_interval: 9999
Output is:
total used free shared buff/cache availableMem: 3.8G 1.4G 879.4M 10.3M 1.5G 2.1GSwap: 975.0M 12.0K 975.0M
or
total used free shared buff/cache availableMem: 3898 1468 880 10 1551 2172Swap: 975 0 975
Case 6: This contains templates and DOES NOT WORK:
- platform: command_line
name: "test command line6"
command: "{{ 'free -h' }}"
scan_interval: 9999
Output is:
unknown
Error in a log:
2022-07-22 03:26:23 ERROR (SyncWorker_22) [homeassistant.components.command_line] Command failed: {{ 'free -h' }}
Case 7: This contains templates and DOES WORK (last year it worked only if “state=ON”):
- platform: command_line
name: "test command line9"
command: >-
free -{{ 'm' if is_state("input_boolean.test_boolean","on") else 'h' }}
scan_interval: 9999
Output is:
total used free shared buff/cache availableMem: 3.8G 1.4G 877.9M 10.3M 1.5G 2.1GSwap: 975.0M 12.0K 975.0M
or
total used free shared buff/cache availableMem: 3898 1468 877 10 1553 2171Swap: 975 0 975
What version of Home Assistant Core has the issue?
2022.7.6
What was the last working version of Home Assistant Core?
No response
What type of installation are you running?
Home Assistant Container
Integration causing the issue
command_line
Link to integration documentation on our website
https://www.home-assistant.io/integrations/command_line
Diagnostics information
No response
Example YAML snippet
No response
Anything in the logs that might be useful for us?
No response
Additional information
No response
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 3
- Comments: 16
Still. If the
commandoption starts with a string - it works. If it starts with a template - does not.