Command Line Sensor Not Capturing Output Despite Manual Command Working

Hello everyone,

I’m facing a persistent issue with a command_line sensor in my Home Assistant configuration and, despite extensive troubleshooting, I can’t get it to work correctly. The sensor entity seems to be registered, the command appears to be triggered, but the sensor’s state does not reflect the expected command output.

My Goal : Create a sensor to get the state of the Matterbridge add-on using the command ha addons info a1b84aa7_matterbridge | grep 'state' | awk '{print $2}' | tr -d '"'.

My current yaml configuration

command_line:
  - sensor:
      name: etat_matterbridge
      unique_id: etat_matterbridge
      command: "echo START_MATTERBRIDGE_STATE && ha addons info a1b84aa7_matterbridge | grep 'state' | awk '{print $2}' | tr -d '\"' 2>&1 && echo END_MATTERBRIDGE_STATE"
      scan_interval: 60
      command_timeout: 15 # Added during debugging

When I go into Developper/States, the sensor state is :

START_MATTERBRIDGE_STATE END_MATTERBRIDGE_STATE

No error in the logs, just this.

(We should be seeing the command return in the middle. It also doesn’t work without these “start” and “end” parts, It’s just the result of a lot of testing)

When I run the command " ha addons info a1b84aa7_matterbridge | grep ‘state’ | awk ‘{print $2}’ | tr -d ‘"’ " in the terminal module, the return is « started » or « stopped » accordingly.

I use HAOS in a VM, and I’ve read some posts pointing out this note in the documentation: :

 Note

If you are using Home Assistant Operating System, the commands are executed in the homeassistant container context. So if you test or debug your script, it might make sense to do this in the context of this container to get the same runtime environment.

As I’m not trying to get out of the homeassistant container to get things like the PC specs or such, I think my command should be working.

Also, I tested the “badlogin” sensor example in the documentation but this time it’s working!

My yaml :

command_line:
  - sensor:
      name: Badlogin
      command: "grep -c 'Login attempt' home-assistant.log"
      scan_interval: 60

(You will see that I adjusted the path part to fit my directories structure)

What am I missing?

Thank you for your time.

Unfortunately it looks like you’re wrong about that assumption. It looks like the “ha” command isn’t available in the homeassistant docker container as it is in HAOS.

Evidence (I used a different addon for testing as I don’t have matterbridge installed, but the test is valid):

SSH directly to HAOS example:

PS C:\Users\marti> ssh -p 22222 root@hass-dev
Welcome to Home Assistant OS.

Use `ha` to access the Home Assistant CLI.
ha addons info 3f6f8ac8_smartbed-mqtt | grep state
state: stopped

So, within HAOS, your command will likely be fine. Let’s try in the homeassistant container. First let’s get the container id:

docker ps
CONTAINER ID   IMAGE                                                      COMMAND               CREATED       STATUS                 PORTS                                     NAMES
c493190262d7   homeassistant/amd64-addon-openwakeword:1.10.0              "/init"               4 hours ago   Up 4 hours (healthy)                                             addon_core_openwakeword
44c6a21b2b01   homeassistant/amd64-addon-piper:1.5.2                      "/init"               4 hours ago   Up 4 hours (healthy)                                             addon_core_piper
d2ecb4b405b9   homeassistant/amd64-addon-whisper:2.4.0                    "/init"               4 hours ago   Up 4 hours (healthy)                                             addon_core_whisper
22a42bae386d   homeassistant/amd64-addon-nginx_proxy:3.11.1               "/init"               4 hours ago   Up 4 hours             0.0.0.0:443->443/tcp, :::443->443/tcp     addon_core_nginx_proxy
a3c8d55d45f6   ghcr.io/hassio-addons/ssh/amd64:20.0.2                     "/init"               4 hours ago   Up 4 hours                                                       addon_a0d7b954_ssh
5a77e3adb8f0   ghcr.io/hassio-addons/vscode/amd64:5.19.1                  "/init"               4 hours ago   Up 4 hours (healthy)                                             addon_a0d7b954_vscode
7fa46ae048f8   ghcr.io/home-assistant/amd64-hassio-multicast:2025.02.0    "/init"               4 hours ago   Up 4 hours                                                       hassio_multicast
5aa9f6966a7c   ghcr.io/home-assistant/amd64-hassio-audio:2025.02.0        "/init"               4 hours ago   Up 4 hours                                                       hassio_audio
f83a2d01da03   ghcr.io/home-assistant/amd64-hassio-dns:2025.02.0          "/init"               4 hours ago   Up 4 hours                                                       hassio_dns
8c9d8bad91f1   ghcr.io/home-assistant/amd64-hassio-cli:2025.04.0          "/init"               4 hours ago   Up 4 hours                                                       hassio_cli
c1e1f019773d   ghcr.io/home-assistant/qemux86-64-homeassistant:2025.4.2   "/init"               10 days ago   Up 4 hours                                                       homeassistant
46894158db1c   ghcr.io/home-assistant/amd64-hassio-supervisor:latest      "/init"               12 days ago   Up 4 hours                                                       hassio_supervisor
e861e1f9abad   ghcr.io/home-assistant/amd64-hassio-observer:2025.02.0     "/usr/bin/observer"   3 weeks ago   Up 4 hours             0.0.0.0:4357->80/tcp, [::]:4357->80/tcp   hassio_observer

Now login to the container:

docker exec -i -t c1e1f019773d bash
homeassistant:/config# ha
bash: ha: command not found

The ha command isn’t valid in this container. You could demonstrate this by setting the command_line integration to debug and look at the logs after executing the command line sensor.

You can set the log level to debug on the fly by going to Developer Tools > Actions > YAML mode

service: logger.set_level
data:
  homeassistant.components.command_line: debug

Don’t forget to change the mode from debug when you are done testing.

Hi!

Thank you for your help!

Just to be sure to understand what you suggest me to do.

I do not have home assistant in a docker container but HAOS installed in a VM as a full OS. Maybe it’s something else I dont understand about home assistant and Home assistant OS is nothing more then a docker container OS.

For the debug and the log checking, I already have this in my configuration.YAML

logger:
  default: info
  logs:
    homeassistant.components.command_line: debug

Is it the same as yours?

With this, I can see the command being “loaded” but no error.

The line look just like that :

DEBUG (MainThread) [homeassistant.components.command_line] Running command: echo START_MATTERBRIDGE_STATE && stdbuf -o0 ha addons info a1b84aa7_matterbridge | grep ‘state’ | awk ‘{print $2}’ | tr -d ‘"’ 2>&1 && echo END_MATTERBRIDGE_STATE

Thank you again

Unfortunately, I’m not suggesting that you do anything other than accept that what you’re trying to do won’t work.

Home assistant as well as all the add-ons run as docker containers in HAOS.

Your debug settings are just fine. I would assume they would show the raw output of the command but I haven’t tested it.

In either case the the documentation statement about testing within the home assistant container is accurate.

ha is not a valid command in that container.

Well. Thank you for those informations.

Do you know any other ways I could achieve what I’m trying to do?

The final goal is to monitor the matterbridge to repair it when it crash.

To repair it, I only need to restore a backup file by copying and rename it.

So the sequence would be like :

If stopped then
Restore the back up file
Restart the addon

You can restart an add-on through an automation action. I’m not sure how you would restore the backup file, however. Maybe ask in the matter bridge GitHub or the specific thread here on the forum.

Wait, do you simply want to know if the Addon is running or not?

If so, the answer is pretty simple. Go to Settings > Devices (that’s the tricky part). Your addon will be listed there. Click the “Device” and expand the disabled entities under Sensors. You’ll find a binary sensor called “Running”. Just enable that and you’re free to use that to monitor the state of the addon

You can use the above sensor to trigger any automation. The restore backup part might be a bit tricky, but I’m pretty sure the action: hassio.restore_partial should work.

Thank you so much for the tips! It’s very useful to know and easy to do.

For the part with the backup to restore, I have made a command line doing just a rm + cp (with renaming) that I will try to have running in the automation.

I hope it goes well. Have you ever tried something like this?

Not via command line, and definitely not by restoring a backup automatically.

My automation based on this sensor is pretty simple. In my case, I use it to start up Z2MQTT after a power loss (in case auto start doesn’t work).
In short, it goes something like:

When: External ping sensor is on for 5 minutes
And: Z2M running state is off
Then: Start Z2M addon

Just to be clear - HAOS is a bit of an umbrella term and it includes the OS plus HA plus Supervisor, and the latter two run under Docker. So while you don’t install Docker yourself, it is part of the HAOS install.

And as mentioned elsewhere, every Addon runs in its own Docker container, and so they are all isolated from each other. Therefore you need to find another way.

Portainer is a tool that can view container status, and it has an API, and there is an addon, so that might work for you.

Similarly, Glances can see container execution status and it supports an API to get all of the containers, so you could use that addon.

Hello,

Thanks for this information. It’s clearer in my head now.

I’ve been quiet lately because I’m testing another approach. I noticed that by connecting via SSH, I have access to the entire HAOS root directory and, therefore, I can modify all the files wherever they are.

So, I’m trying to create a command that will make an SSH connection to run my commands as root, but from an automation.

So far, it’s going well. I can connect with Putty from my PC and run my commands from the system root.

All that’s left is to make the same connection with a command and execute it from an automation.

I’ll also look at the two add-ons you mentioned. They might be useful.

Thanks again.