Reading a Text File using Command_line Sensor

Hi I am not very experienced with Hassio and as a result have used NodeRed to make up for my Hassio shortcomings. I would appreciate some help if anyone has some thoughts. I am running the latest Hassio on a VM.

I use NodeRed to write the output of my mqtt water meter to a file which is overwritten every hour. The file path in Nodered is /share/todaylog.txt. This file can be mounted on my windows pc. The text file only contains 1 line holding the current Water usage total for the day. ie 165.5.

The powershell below retrieves the value when run in powershell but when I try to incorporate it into my Yaml and add it as an Entity Card to Home Assistant it does not retrieve any values.The Card Just says unknown.
Can someone tell me what I am doing wrong?
How can I test if Hassio can access this share?

***** Lines added to my Yaml ****

- platform: command_line
  name: Todays Water Use
  command: powershell.exe -command "(gc ( gci //192.168.86.159/share/todaylog.txt - 
  recurse | sort LastWriteTime | select -last 1).fullname -tail 1)"
  unit_of_measurement: "L"
  scan_interval: 10

Never used it on Windows, so I am not sure if HA is able to find/locate Powershell. You might want to consider using the full path.

Next to that, I am wondering though why you wrote the command like you did since this gives the same result if the file only has one line:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command "gc //192.168.86.159/share/todaylog.txt"

in case the file has a blank line at the bottom, you might want to concider:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command "gc //192.168.86.159/share/todaylog.txt" -First 1

  • I searched the path online by the way, yours might be located somewhere else. I don’t know.

Thanks for responding. I did lift this PowerShell line from somewhere it did the job so I didn’t worry to much but yours is cleaner. The PowerShell Path is the same on my setup but unfortunately I still just get ‘unknown’ as the result in my Entity Card. I tested your line direct in CMD line and it worked fine. I have also tried just using Python to print the line instead of using PowerShell but this also does not work under YAML.
Does my Hassio need to actually see the path or does the script I provide do all the work and Hassio just collects the result? Here are the new commands I have tried.

 - platform: command_line
    name: Todays Water Use
    command: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe - 
    command "gc //192.168.86.159/share/todaylog.txt" -First 1
    unit_of_measurement: "L"
    scan_interval: 10

    
  - platform: command_line
    command: C:\Python27\python.exe -c 
    "print(open('//192.168.86.159/share/todaylog.txt').readlines())" 
    name: File value
    scan_interval: 10

I tried to dig in a little further and I assume your [command_line](https://www.home-assistant.io/integrations/sensor.command_line is part of sensor and not binary_sensor integration. The YAML example does not show.

I think HA just receives the response by the way. I have to be honest to tell that it is a bit guessing/trail and error now, but how about using return:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Command { return $(gc //192.168.86.159/share/todaylog.txt -First 1) }

The command above handles the result as a variable and returns the variable. Next to that, I removed all the quotes just to make sure that is not causing any issues. You might want to try to put the whole command in between quotes like this:

command: "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Command { return $(gc //192.168.86.159/share/todaylog.txt -First 1) }"

Thanks Recte

Yes the command line is nested under the Sensor: Tag.

I tried both versions of your code, the top one works when run directly in powershell, the one in double quotes errors. It also errors in Yaml. Complained of ‘unknown escape sequence’.

Ok so I have looked a bit harder and discovered the Core Logs under Supervisor.

It is reporting the following. It seems to drop the ‘backslash’ in the powershell path. Also says /bin/sh: gc: not found.

/bin/sh: C:WindowsSystem32WindowsPowerShellv1.0powershell.exe: not found 2020-07-08 07:35:19 ERROR (SyncWorker_0) [homeassistant.components.command_line.sensor] Command failed: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Command { return $(gc //[192.168.86.159/share/todaylog.txt](http://192.168.86.159/share/todaylog.txt) -First 1) } /bin/sh: gc: not found

My Hassio System is installed on Ubuntu running as an Oracle VM on a Windows 10 PC so it’s looking like it can’t resolve to the Powershell exe. Have you had any luck running a Windows Command yourself? Perhaps they need to be Linux based.

I tried copying the today.log.txt file under the Hassio /config directory and ran in Yaml

command: tail /config/todaylog.txt 

THIS WORKED! So I think I have 2 issue.
a) Hassio cant run Windows Commands,
b) Hassio cant see the Nodered Share.