Template Syntax error when using {{ }} within command_line nested in quotes

Goal: creating a sensor from Command_Line that allows me to display on my dashboard a list of the containers I have running on another host.

After much troubleshooting, I have come to believe that the use of {{ }} within my command line, even though nested in quotes, makes Home Assistant believe I’m trying to create a template when I’m just passing through the command. Not sure where to go from here.

command_line:
  - sensor:
      name: DockerPS
      command: "ssh -i /config/.ssh/truenas -o StrictHostKeyChecking=no [email protected] /mnt/tank/jailmaker/jlmkr.py exec containername docker ps --format '{{ .Names }}'"

First, I ssh into my host running the docker containers within a jail. I can pass through the docker ps command with the option to format the output in names only. The {{ }} seems to be required by the Docker documentation. I get this error.

Error rendering command template: TemplateSyntaxError: unexpected '.'

Running this full command inside HA shell outputs the list of names perfectly. The sensor, when configured in configuration, removing --format '{{ .Names }}' outputs the full Docker PS return which would work if I could figure out a way to parse non json outputs into just the names of the containers. I even tried formatting the output into json but doesn’t seem to be recognized as json in Home Assistant. Any help greatly appreciated!

Try:

command_line:
  - sensor:
      name: DockerPS
      command: "ssh -i /config/.ssh/truenas -o StrictHostKeyChecking=no [email protected] /mnt/tank/jailmaker/jlmkr.py exec containername docker ps --format '{{ '{{' }} .Names {{ '}}' }}'"

https://jinja.palletsprojects.com/en/3.1.x/templates/#escaping

1 Like

Brilliant thank you! Works perfectly and a good reminder that I need to RTFM.

For anyone looking for the exact code used, had to move the quotes a bit more to be this:

command_line:
  - sensor:
      name: DockerPS
      command: ssh -i /config/.ssh/truenas -o StrictHostKeyChecking=no [email protected] "/mnt/tank/jailmaker/jlmkr.py exec containername docker ps --format '{{ '{{' }} .Names {{ '}}' }}'"