Extracting plain text from remote URL?

Hello all - I have a value that is accessible by URL, but the response is just a plain text file, not JSON, so I can’t use the REST component.

I can easily write my own code to fetch a URL and search for a regular expression, or even cobble together something with curl and grep, but I just wondered if there was anything out there to save me reinventing the wheel?

The URL is here if anyone wants to see the format.

Many thanks!

The REST sensor can work with plain text, too. Just use value instead of value_json in your value_template. And for searching, you can use regex_findall_index. E.g., if you wanted to get the value after Temperature, you could do something like this:

{{ (value|regex_findall_index('Temperature:\s*[0-9.]*\s*[CF]')).split(' ',1)[-1].strip() }}

Ah, brilliant - thanks. I’m not sure that’s much clearer in the end than my use of the command_line sensor:

curl -s <URL> | grep -m 1 'Temperature:' | cut -d' ' -f 3

but it certainly involves fewer processes! Many thanks!

1 Like