Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

command_line sensor with templated command: inconsistency (still) #75601

Open
ildar170975 opened this issue Jul 22, 2022 · 16 comments
Open

command_line sensor with templated command: inconsistency (still) #75601

ildar170975 opened this issue Jul 22, 2022 · 16 comments

Comments

@ildar170975
Copy link

ildar170975 commented Jul 22, 2022

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:
#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

@probot-home-assistant
Copy link

command_line documentation
command_line source
(message by IssueLinks)

@ildar170975
Copy link
Author

ildar170975 commented Jul 22, 2022

Case 8:
DOES NOT WORK

  - platform: command_line
    name: test_command_line_ls_1
    command: >-
      {% set KEYS = "a" -%}
      ls -{{KEYS}}
    value_template: "{{ value }}"
    command_timeout: 15
    scan_interval: 9999

Output is:
unknown

Error in the log:

2022-07-22 04:10:23 WARNING (MainThread) [homeassistant.helpers.template] Template variable warning: 'KEYS' is undefined when rendering 'set KEYS = "a" -%} ls -{{KEYS}}'
2022-07-22 04:10:23 ERROR (SyncWorker_23) [homeassistant.components.command_line] Command failed: {% set KEYS = "a" -%} ls -

Case 9:
DOES NOT WORK

  - platform: command_line
    name: test_command_line_ls_2
    command: >-
      {% set COMMAND = "ls" -%}
      {{COMMAND}} -a
    value_template: "{{ value }}"
    command_timeout: 15
    scan_interval: 9999

Output:
unknown

Error in the log:

2022-07-22 04:11:32 WARNING (MainThread) [homeassistant.helpers.template] Template variable warning: 'COMMAND' is undefined when rendering 'set COMMAND = "ls" -%} {{COMMAND}} -a'
2022-07-22 04:11:32 ERROR (SyncWorker_27) [homeassistant.components.command_line] Command failed: {% set COMMAND = "ls" -%} -a

Case 10:
DOES WORK - OK

  - platform: command_line
    name: test_command_line_ls_3
    command: >-
      ls {% set KEYS = "automations.yaml -la" -%}{{KEYS}}
    value_template: "{{ value }}"
    command_timeout: 15
    scan_interval: 9999

Output:
-rw-r--r-- 1 911 911 620 Jul 21 05:25 automations.yaml

@github-actions github-actions bot added the stale label Aug 21, 2022
@ildar170975
Copy link
Author

bump

@github-actions github-actions bot removed the stale label Aug 24, 2022
@github-actions
Copy link

There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates.
Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment 👍
This issue has now been marked as stale and will be closed if no further activity occurs. Thank you for your contributions.

@github-actions github-actions bot added the stale label Nov 22, 2022
@ildar170975
Copy link
Author

Up

@github-actions github-actions bot removed the stale label Nov 24, 2022
@issue-triage-workflows
Copy link

There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates.
Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment 👍
This issue has now been marked as stale and will be closed if no further activity occurs. Thank you for your contributions.

@ildar170975
Copy link
Author

Up

@github-actions github-actions bot removed the stale label Feb 22, 2023
@issue-triage-workflows
Copy link

There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates.
Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment 👍
This issue has now been marked as stale and will be closed if no further activity occurs. Thank you for your contributions.

@ildar170975
Copy link
Author

up

@issue-triage-workflows
Copy link

There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates.
Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment 👍
This issue has now been marked as stale and will be closed if no further activity occurs. Thank you for your contributions.

@ildar170975
Copy link
Author

up

@github-actions github-actions bot removed the stale label Aug 22, 2023
@issue-triage-workflows
Copy link

There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates.
Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment 👍
This issue has now been marked as stale and will be closed if no further activity occurs. Thank you for your contributions.

@ildar170975
Copy link
Author

up

@issue-triage-workflows
Copy link

There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates.
Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment 👍
This issue has now been marked as stale and will be closed if no further activity occurs. Thank you for your contributions.

@ildar170975
Copy link
Author

Still.
If the command option starts with a string - it works.
If it starts with a template - does not.

@adorobis
Copy link

Same here on the 2024.3.0 version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants