File Sensor: Plain Text File

Greetings! I am trying to create a file sensor to pull the very last line of a flat text file. Here’s the output of the file… essentially, it’s the a log of a switch.

GPO 1 Lhhhh
GPO 1 Hhhhh

Lhhhh= On.
Hhhhh= Off.

How would I go about creating a sensor to check and see the state of the very last line of a text file? So far… I have:

  - platform: file
    name: SwitchLog
    file_path: /config/switch/switchsensor.txt

I’m just not sure where to go next.

Thanks for your help. :slight_smile:

That should create a sensor named sensor.switchlog which has the line you want.

What else are you looking for?

1 Like

Thanks Nick! I must have missed a step, as I don’t see the Sensor anywhere. Maybe my file path is off.

OK! So the plot thinkens.

On a full restart of Home Assistant, I see the following error in the log:

Logger: homeassistant.components.file.sensor
Source: components/file/
Integration: file ([documentation](, [issues](
First occurred: 10:39:34 AM (1 occurrences)
Last logged: 10:39:34 AM

'/config/switch/switchsensor.txt' is not an allowed directory

Here from my configuration:

    - /config/switch

And the sensor…

  - platform: file
    name: axia
    file_path: /config/switch/switchsensor.txt

The text file exists:

bash-5.0# ls

bash-5.0# pwd

Any ideas? Thanks!

For what it’s worth, I’ve tried whitelist_external_dirs and also allowlist_external_dirs to no avail.

@rickhall have you checked the file permissions? do “sudo chmod -R 777 XXXX” where XXXX is your file name or directory.

I am having a very similar issue. My file sensor in configuration.yaml is giving the “not an allowed directory” error too. My setup is in a python virtual environment.

File permissions/ownership were set properly, but I did chmod 777 just to be sure, but it still gives this error. I even located the files in the same directory as my configuration.yaml to no avail.

ERROR (MainThread) [homeassistant.components.file.sensor] '/home/homeassistant/.homeassistant/test.csv' is not an allowed directory

Here is the sensor:

  - platform: file
    name: testfilesensor
    file_path: /home/homeassistant/.homeassistant/test.csv

Any ideas?

whitelist it.

I guess this is one of those RTFM situations…

Thank you, it is working fine now.

1 Like

I’m trying to RTFM but can’t seem to get this working!

I’m in a docker environment and have

    - '/config/spin/'

In my sensors file I have:

  - platform: file
    name: spin
    file_path: /config/spin/spin.txt

but still I see

2021-05-26 22:46:10 ERROR (MainThread) [homeassistant.components.file.sensor] '/config/spin/spin.txt' is not an allowed directory

Any suggestions on what I have done wrong would be appreciated.


EDIT:: Everything was correct. My issue was I had two homeassistant: tags which meant the allowlist_external_dirs was being ignored … all good now!!

1 Like

I’m not sure what is going on, but in my case this worked:

    - '/sys'

while this did not:

    - '/sys/class/thermal/thermal_zone0'

The file_path of the sensor was:

file_path: '/sys/class/thermal/thermal_zone0/temp'

I set up such a sensor as well.
Is it possible to make it ready for long-term statistic? If yes, how?

1 Like


  - platform: integration
    source: <yourFileSensor>

Bit of a late reply, but I was having the same issue, and I think I see what is going wrong here.

/sys/class/thermal/thermal_zone0 is actually symbolically linked to /sys/devices/virtual/thermal/thermal_zone0 so if you set your allowed directory to point to /sys/devices/virtual/thermal/thermal_zone0, it should work. You can also change your file_path for the sensor, but that is optional.

This is still great to know, thanks!

This didn’t occur to me, even though I have encountered other applications before that abuse realpath() for cleaning up paths… whyyyyyy. :expressionless:

So I am trying to get something similar up and running, to pull a file from my city council on river gauge heights. I have the file being downloaded (and have automated that), and can see and open the file.
But I can not see a sensor of any kind. Like those above, not sure if it is directory related, or if I am doing something completely wrong.
I am looking in Developer Tools > States, for the sensor, as that is where the doco and other pages have said the sensor would be found, but nothing.

The following is from my configuration.yaml.

# file parser entry
  - name: river-guage-data-file
    platform: file
    filename: /downloads/river-guage-data.csv
    timestamp: true

# file sensor entry
  - platform: file
    name: River_Height
    file_path: /config/downloads/river-guage-data.csv
    value_template: '{{ value.split(",")[82] }}'
    unit_of_measurement: "m"

    - "/config"

Maybe try

    - "/config/downloads"

Other than that logs…

Even though I have had that at times, I wasn’t checking the logs in between each change.
Looks like it is unable to parse ‘-’ as it is expecting a number (float) (I also saw an error where it was complaining of an integer when expecting a float).

Is there a way to have it hold the last value if it sees “-”? And to make an integer a float, as it reads the file?
I mentioned, I am downloading a river guage file from the council, so I can not control the data format. And it looks like on a normal day, the data is updated speradicly (not every 5mins/hour) and then on a bad day (flooding), every reading. When not updated it posts “-”, and then when it does the number may be an int or a float. :frowning:

Error log:

2024-02-08 07:42:46.608 ERROR (MainThread) [homeassistant.components.sensor] Error adding entities for domain sensor with platform file

Traceback (most recent call last):

File “/usr/src/homeassistant/homeassistant/components/sensor/”, line 639, in state

numerical_value = int(value)


ValueError: invalid literal for int() with base 10: ‘“-”’

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

File “/usr/src/homeassistant/homeassistant/components/sensor/”, line 642, in state

numerical_value = float(value)


ValueError: could not convert string to float: ‘“-”’

Download the file with a script that processes the file.

Can you give some actual examples of the file when it works and when it doesn’t? (The last few lines only I guess, if it is long). Also, how are you downloading it at present?

Help would be awesome, as I am very green with HA.
I am using the downloader integration, which I then automated to run every 5 minutes.

# Downloader Integration setup
  download_dir: downloads

# file parser entry
  - name: river-gauge-data-file
    platform: file
    filename: /downloads/river-gauge-data.csv
    timestamp: true

The file is a csv file, and the river gauge I am after is after the 71st comma.
The data looks like this

So, as you can see it can be a mixture of “-” or floats. The columns that have all 0’s are rainfall, but none of them are for my area.

I was then trying to use the sensor - file, which reads the last line, as that would do for a trigger; as much as I would love to graph it in the UI, being able to set alarms and other things when the river height reaches the minor flood level is what i am after.
Thanks heaps