Sensor data from file

I need some help. An external script is producing a file with some values in it. For example:

308
2
(* So 2 numbers with line break after it.)

I want to create an commandline sensor that reads that file and value_template it. But how to do this?. Couldn’t find an example. I read the value_template docs, but still nog able to figure out. Does have anyone an example to point me in the right direction?

Much appriciated.

Is your intention to process all the lines in the text file using 1 single sensor? I’m not sure if that’s possible.

If you want to set up a sensor to read 1 specific line, it could look like this:

- platform: command_line
  name: "sensor name"
  command: "curl -s /path/or/url/to/file.txt | awk 'NR==1'"
  unit_of_measurement: '%'

The value for NR is the line you want to read, so the first line in my example.
The unit_of_measurement can be anything you want, or you can leave it out if not needed.

1 Like

This example is exactly what I need.
I can create a sensor per line thats no problem. I will try this and let you know the result.

Many thnx!

Hi,

I tested it and using curl results in a error: “malformed url”. So i used tail instead. And that works. Are there any drawbacks using tail instead of curl?

Many thanks, for pointing me in the right direction!

You’re welcome! :wink:

tail, by default, will output the last 10 lines of a file, so I guess that would depend on how many lines are in your file. You could also specify how many lines you want to output with tail. Or you could use cat instead.

Anyway, I don’t think there’s any drawbacks to using tail so I’m glad to hear you got it to work!