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:

sensor:
  - 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/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

Here from my configuration:

  whitelist_external_dirs:
    - /config/switch

And the sensor…

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

The text file exists:

bash-5.0# ls
switchsensor.txt

bash-5.0# pwd
/config/switch

Any ideas? Thanks!

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