Send a command throught a pipe and retrieve the content of the pipe (response)

Hi, I use a pipe to control my unraid server from home assistant (docker)
I use the pipe for example to manage virtual machines:

- platform: command_line
    switches:
      vm:
        friendly_name: VM
        command_on: echo 'virsh start "Windows 10"' > /config/pipe
        command_off: echo 'virsh shutdown "Windows 10"' > /config/pipe

Now I need to make a binary sensor to see the status of a docker with this command:

echo 'docker inspect -f "{{.State.Status}}" db4100c3-9b8c-407d-ba31-088bad630b20' > /config/pipe

If I do cat pipe it tells me running or exited, and I want retrive this response and put it in the binary sensor, how can I do this?
I tried with this but nothing:

binary_sensor:
  - platform: command_line
    name: Server
    command: 'echo 'docker inspect -f "{{.State.Status}}" db4100c3-9b8c-407d-ba31-088bad630b20' > /config/pipe'
    device_class: connectivity
    payload_on: "running"
    payload_off: "exited"
    scan_interval: 10

I can see a number of issues here -
the first is that your single quotes:

'echo 'docker inspect

Would need to be escaped to prevent parsing errors:

'echo \'docker inspect

The next issue is here:

-f "{{.State.Status}}"

Because it’s in double curly braces, Hoe Assistant will attempt to treat that as a Jinja template, so this should also be escaped:

-f "\{\{.State.Status\}\}"

otherwise Jinja complains about a syntax error.
So I would try something like:

    command: 'echo \'docker inspect -f "\{\{.State.Status\}\}" db4100c3-9b8c-407d-ba31-088bad630b20\' > /config/pipe'

Thanks for the fixes, the big problem now is retriving the content of the pipe and use it for the sensor

Run the command outside of home assistant, and post the full output. I believe docker inspect returns JSON, so we should just be able to use value_template to extract what we need out of the JSON, so the payloads match.

EDIT:
Revisiting this - it looks the escaping is more complicated:

    command: 'echo "docker inspect -f \"{{ '''{{''' }}.State.Status{{ '''}}''' }}\" db4100c3-9b8c-407d-ba31-088bad630b20" > /config/pipe'

try that.

When I do docker inspect it tells me running or exited, I dunno if it is a json
I have difficulty in creating the sensor that can send the command and take the contents of the pipe, if you could help me I would be grateful

Can you explain why the echo or the pipe is necessary at all?

    command: 'docker inspect -f "{{ '''{{''' }}.State.Status{{ '''}}''' }}" db4100c3-9b8c-407d-ba31-088bad630b20'

should be all that is necessary.

because hassio runs on a docker, I need the pipe to “talk” with the os, like getting temps, disks utilization, managing vm and docker and so on

Well, instead of trying to fit all the command in the command_line config. Why not have the entire command in a shell file, and then simply call the shell file.

I was thinking about this, but it isn’t easy ahah, I’ll look forward for this

command: 'docker inspect -f "{{ '''{{''' }}.State.Status{{ '''}}''' }}" db4100c3-9b8c-407d-ba31-088bad630b20' && cat /config/pipe

or just use raw

command: 'docker inspect -f "{% raw %}{.State.Status}{% endraw %}" db4100c3-9b8c-407d-ba31-088bad630b20' && cat /config/pipe

But this will send the command throught the pipe? and then get the results?

It executes both commands, one after the other.

doesn’t work, I can’t do docker inspect directly from the the docker (ha), i need to send it to the pipe and then read the pipe to get the result
I tried this but the sensor remain unknown

command: echo 'docker inspect -f "{% raw %}{.State.Status}{% endraw %}" db4100c3-9b8c-407d-ba31-088bad630b20' > /config/pipe && cat /config/pipe

This is the other part of the pipe:

while true; do eval "$(cat /mnt/user/appdata/homeassistant/pipe)"; done

I need to get the result back to hassio

Did you try my suggestion?

Yes, it doesn’t work because I can’t call docker from inside a docker

Ummmm I don’t think that is correct.

Since the response comes from outside home assistant, why don’t you do it the other way?
Use either MQTT or a binary_sensor template using a webhook as a trigger.

Run the docker inspect command directly outside home assistant, and then send the result TO home assistant either other MQTT or the webhook.