Reading from a text file

how can I read the contents of a txt file into a sensor.

Hi,

Try this:

if I read that corectly thats reading from a remote site I need to read from a local file

It is just an example that you can adapt for your needs. It seems to me if you replace it with a path to your local file, it should work.

fails
ERROR:homeassistant.components.sensor.command_line:Command failed: python3 -c “import requests; print(requests.get(’\Users\dauth\Dropbox\Apps\TaskAgent\store.txt’).text)”

figured it out
requests doesnt handle local files
installed requests_file with pip install requests_file

then added this to config

sensor:

  • platform: command_line
    command: py -c “import requests; from requests_file import FileAdapter; s = requests.Session(); s.mount(‘file://’, FileAdapter()); print(s.get( ‘file:///Users/dauth/Dropbox/Apps/TaskAgent/store.txt’).text)”
    name: File value

now the contents of the file now show up in the state of the sensor.

and now I will celebrate my victory by going to bed. ugh 4 am lol

1 Like

If the file is local, you don’t need another package to read a file, if you want to simplify the script.

python3 -c "print(open('<FILENAME>').readlines())"

1 Like

lol googled like mad last night and that never came up.
thanks will test after work