Another “can’t get command line sensor” to work

  • I am running HA on HA Green in Supervisor mode.
  • I have a file integration that correctly adds a date/time stamp to a file /config/text.txt
  • I want to read the last 5 entries into a sensor so I can display these in a card on a dashboard
  • I have the following code in my configuration.yaml

homeassistant:
  allowlist_external_dirs:
    - “/config”

I’ve tried
“/config/“
too

sensor:
  - platform: command_line
    name: downstairs_log
    command: "tac /config/test.txt | head -n 5 || echo 'Error reading file'"
    value_template: "{{ value }}"
    scan_interval: 60
  • I have tested the command in SSH and it works as expected
  • I have checked the logs for references to “command_line” and “downstairs_log” and can’t see anything unusual, even with command line debug logging on.
  • I can see this in the logs “ 2025-04-25 07:09:06.047 INFO (MainThread) [homeassistant.components.sensor] Setting up command_line.sensor”
  • When I go to the entities and search for sensor.download_log with no other filters, I can’t find it as I would expect.

Any suggestions on what to do next?

Thanks

Matt

Edit:

  • Core 2025.4.3
  • Supervisor 2025.04.0
  • Operating System 15.2
  • Frontend 20250411.0

This confuse many new users.
The addons are in fact containers, which means they are actually another server than HA and it could actually also be on another hardware device.
the commands might be installed in the addon container, but not be present in the HA container.

Likewise folders might be mapped differently in the different containers.

Easiest way to check the command is by adding a monitor and keyboard to the HA server.
When you get the HA prompt, then type login and hit enter.
Next you need to run the docker command to execute your line on the homeassistant container, like docker container exec homeassistant <your command>

The documentation also have this note:

Execution

The command is executed within the configuration directory.

/config should not be needed and might actually make it fail if HA use a chroot.

Thanks for your replies. I have tried using

command: "tac test.txt | head -n 5 || echo 'Error reading file'"

and still no success.

I did try to connect directly to HA Green as you suggested, but TBH I wasn’t really sure what I was doing. I could login to HA and I literally typed what you said (not sure if that is correct)

docker container exec homeassistant "tac test.txt | head -n 5 || echo 'Error reading file'"

also

docker container exec homeassistant tac test.txt | head -n 5 || echo 'Error reading file'

all I got were errors. I am not sure if that’s becuase I took what you wrote too literally or if there is something else I have done wrong.

Also, I am definitely confused about the difference between direct connection to HA Green and using SSH to connect.

Maybe it is your test.txt file that is the issue.
I do not have that and ran docker container exec homeassistant tac scripts.yaml | head -n 5 and that works.

HOWTO: How to access the Home Assistant OS host itself over ssh

Is the file name test.txt or text.txt? You mention the latter in your description, but the former is what’s in your sensor yaml.

Definitely test.txt. Text.txt was a typo, sorry

The sensor is in configuration.yaml

I have read that HA Green does not use docker containers. Is that correct?

You are running HAOS and HA core is a docker container in that.

I have an update from my testing.

I went into Integrations\File and added an entry for a sensor with attributes
file based sensor
path “/config/test.txt”
value template = {{ value }}

This sensor works. I can correctly read the last line of the file in the sensor. So there must be nothing wrong with the file itself.

I purchased a Home Assistant Green from Nabucasa. HA comes preinstalled. My understanding is that there is no Docker instance.

could this be a HA green issue?

I tried the exact test you did.

docker container exec homeassistant tac scripts.yaml | head -n 5

all that happened was the help file popped up.

Note, I have a ha > prompt on my screen. I tried exiting out of this but it just seems to reboot back to this screen. I have been reading that maybe the green is locked down and wont allow me to do what you suggested.

so back to the problem, how can I read from a file?

I resolved my problem with a work around.

Objective was to log all visits of my lawnmower man in a file and display the last 5 visits on my dashboard. I would have access to the file if I needed more

Problem: it seems I can’t read from the file using command_line with my HA Green. It would be good if someone can confirm that. If true, there should be a warning on the purchase “warning, advanced concepts cannot be executed. This is the price you pay to have a protected environment”

My solution was to create 2x storage solutions. I kept the file for long term storage and deployed a second input_text helper to manage the 5 events on the dashboard.

Next step is to store the “days since last visit” against each entry

you did not type login first

I have not used command_line sensor for a while, so I am a bit rusty on that part, but the file sensor require a setting to allow reading the files and it is listed as a note on the page for the sensor.
Maybe there is missing a note on the command_line sensor too.

The setting is allowlist_external_dirs.

If you can’t, it has nothing to do with a Green or not. Any system running HA OS would not be able to do it. But I have to little experience with command line sensor to confirm.