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:
On a full restart of Home Assistant, I see the following error in the log:
Logger: homeassistant.components.file.sensor
Source: components/file/sensor.py:43
Integration: file ([documentation](https://www.home-assistant.io/integrations/file), [issues](https://github.com/home-assistant/home-assistant/issues?q=is%3Aissue+is%3Aopen+label%3A%22integration%3A+file%22))
First occurred: 10:39:34 AM (1 occurrences)
Last logged: 10:39:34 AM
'/config/switch/switchsensor.txt' is not an allowed 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
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.
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.
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.
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/init.py”, 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/init.py”, 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?
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