Dummy Hardware "Read values from text file"

I´m looking for a component that can read values from a text file stored on the same Windows 10 machine as HA.

A new textfile is created every new day in a new directory, text file always has the same file name.

Example:
I need a command that can read the latest version of text file “Temp Grund.txt”

File contains information like HH:MM:SS,Value:

21:19:00,9.85
21:19:30,9.86
21:20:00,9.87

I want HA to understand that this is momentary temperature at a given sensor and also logging this information.

Any suggestions how to solve this? I am a begginer so please explain even the basics.

The command line sensor should be able to do it.

I am running it on a Linux server, but should work just fine on windows.

the challenge might be to read the new directory, but the command or script could work that out.
If you give some more information I am happy to whip something up for you.

1 Like

Thanks for your reply. This sounds hopeful.

Files are stored in new directory/sub directory each year/month/day in format below, one textfile for each sensor:

C:\Logger\2017\04\14\Temp Grund.txt
C:\Logger\2017\04\15\Temp Grund.txt
C:\Logger\2017\04\16\Temp Grund.txt
C:\Logger\2017\04\17\Temp Grund.txt

Information in textfile contains new sensor data every 30 second, this is printed on the bottom line of text file, like example below. First six digits are time like HH:MM:SS, all following digits are sensor data (temperature):

00:00:00,10.00
00:00:30,9.99
00:01:00,9.99
00:01:30,9.98
00:02:00,10.00
00:02:30,9.97
00:03:00,9.97
00:03:30,9.95
00:04:00,9.93

Just a tought, would it be easier to create some windows script to move this file to same directory every time?

Unfortunately I don’t have HA running on Windows, but this should give you the last line of the file most recently written to. (Tail is really hard in “DOS” but very easy in Powershell.)

powershell -command "& {gc ( gci C:\logger -recurse | sort LastWriteTime | select -last 1).fullname -tail 1}"

some explanation:

  • gci : get child item: find all files recursive (this might need more filters, *.txt or “no directory”)
  • sort by LastWriteTime
  • Select the last file
  • gc: get content of the file we just found in the gci () command
  • tail 1: only return the tail (last line)

you will still need to split the value from the time. But even this could be done in powershell, if the template proves to difficult.

This is what I think the sensor should be:

sensor:
  - platform: command_line
    name: Ground Temperature
    command: powershell -command "& {gc ( gci C:\logger -recurse | sort LastWriteTime | select -last 1).fullname -tail 1}"
    unit_of_measurement: "°C"
    scan_interval: 10
    value_template: '{{ ????? }}'

I think the value template should be easy for someone with a bit of template experience. I might give it a shot later, but I need to build and equivalent sensor on linux first. Hope this gets you going.

The above sensor should give you 00:04:00,9.93 as a result. {{TIME, VALUE}}

you could update the sensor every 10 seconds and still be close to your 30 seconds update of the file.
Or a fair bit slower, as the ground does not change its temperature that quickly …

OK, I could not figure out the template.
Maybe someone else can chip in.

But you can do this in poweshell:

(gc ( gci C:\logger -recurse | sort LastWriteTime | select -last 1).fullname -tail 1).split(",")[1]

this now splits the result at the “,” and returns the second part of the string. [0] = time, [1]= value
You won’t get the exact time the value got updated in the file, but you should still have the timestamp the sensor was refreshed.

Hope that helps

Thanks for your help.This is one big step closer to the solution.

With the line value_template: ‘{{ ??? }}’ I get error message to check my config, if I just delete this line I receive the value from the last line in text file in HA sensor as expected.

I can also run the command: powershell -command “& {gc ( gci C:\logger -recurse | sort LastWriteTime | select -last 1).fullname -tail 1}” in command promt to verify this.

But when I run your last suggestion I´ve got som error messages from powershell, se below (HA i doesn´t read any value with this command line):

Hi,
sorry, the " did throw it off.
This workds from command prompt:

powershell.exe -command "(gc ( gci C:\logger -recurse | sort LastWriteTime | select -last 1).fullname -tail 1).split(\",\")[1]"

note I removed the & and escaped the " with "

Great, this works perfect. Thank you datamonkey for your first class support, I really appreciate this!

I have added some more sensors from my Logger to HA, I only did write their unique name after C:\logger\ and your script find them without any problems.

I hope this topic will be useful for other users that have #Logger2020 used with #LoggerSoft or similar loggers that save data to text-file.

Once again, thank you very much for this support. Thanks to this I´m “on the road” with HA.

1 Like

Glad it all worked for you, always happy to give something back to this great community.

I still think that last step should be achievable with a template. If some template specialist comes along here, please have a go at it.

Happy logging

This command_line sensor has worked great for me under Windows, but now is Hassio is migrated to Ubuntu-server so hope to get some help how to translate this script for running with Hassio under Ubuntu.

It´s still the same file from Logger (windows program running with Wine under Ubuntu). Can I use part of the same command, what needs to be changed?