Error rendering command template: TemplateSyntaxError: unexpected '.'

I’m trying to run the following shell command but keep coming up with the below error message;

Shell Command;

ssh -i /.ssh/id_rsa -o 'StrictHostKeyChecking=no' ######@10.10.10.195 'sudo docker ps --format "table {{.ID}},{{.Names}},{{.Image}}" > /home/######/docker/homeassistant/csv/container.csv'

Error Message:

Error rendering command template: TemplateSyntaxError: unexpected '.'

It seems to be the period in the table names that is causing the issue (i think) as removing them allows the command to run but just pulls back the commas in between. If I run the code from within the container it works fine as is and gives the desired result, it just won’t run from the Home Assistant shell command.

Is there a workaround or alternative way to execute this as I want it to run on a schedule.

Full error code from HA;

Error rendering command template: TemplateSyntaxError: unexpected '.'
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 318, 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 1469, in compile
    cached = self.template_cache[source] = super().compile(source)
  File "/usr/local/lib/python3.8/site-packages/jinja2/environment.py", line 638, in compile
    self.handle_exception(source=source_hint)
  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 "<unknown>", line 1, in template
jinja2.exceptions.TemplateSyntaxError: unexpected '.'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/shell_command/__init__.py", line 48, in async_service_handler
    rendered_args = args_compiled.async_render(
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 362, in async_render
    compiled = self._compiled or self._ensure_compiled(limited)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 539, in _ensure_compiled
    self.ensure_valid()
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 320, in ensure_valid
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: TemplateSyntaxError: unexpected '.'

HA might be trying to treat the {{ }} as jinja templates. Try putting the command in a file named docker_script.sh on the host, make it executable with sudo chmod +x docker_script.sh and try running it with ./docker_script.sh

#!/bin/bash
#This goes in a script file on the host.
sudo docker ps --format "table {{.ID}},{{.Names}},{{.Image}}" > /home/######/docker/homeassistant/csv/container.csv

Then change your shell command to:

ssh -i /.ssh/id_rsa -o 'StrictHostKeyChecking=no' ######@10.10.10.195 '/path/to/docker_script.sh'

Thanks, had to add sh at the beginning of the file path in the shell command but seems to be working

1 Like