Can anyone steer me in the right direction please? I’m using the UKHO API and REST sensors to get the current local tidal height and the tidal events (high & low tide times & heights) for the next week. All’s well with how I do this but I’ve observed that there are some significant errors in their tidal forecast, which they accept. They have now provided me with new data and asked me to help validate it. As part of this I’d like to try substituting it into a copy of my tides dashboard. The test data consists of 2 csv files, one has nearly 53,000 pairs of <datetime><height> (every 10mins for a year), the other about 1,400 <datetime><height> pairs (1 per tide). I have two questions:
can I do something to these two datasets that allows me to interrogate them with my REST code (or something derived from it)?
failing that, what’s the best way of looking up data from them? I’ve read about SQL but it looks daunting to replicate what I’m doing with REST sensors. The FILE sensor clearly won’t work here.
This is an example of what I’m trying to replicate. The first gets the current height and what it will be in 90 mins.
If you want to do local REST queries, I believe you need a custom web application that can interpret the GET parameters (after the question mark in the URI) to scan the file and return the right information. If you’re comfortable writing code, you could probably write a short python daemon to do this, or maybe a PHP script running in a http server.
If you include a few lines of the file we might be able to help further with the 2nd question. Is the file CSV or XML formatted? If it’s the latter, you might be able to retrieve the entire file every time, and setup a value_template to query the fields previously filtered by the GET parameters.
Thanks for the reply. Both files are very simple csv format, but I can convert them to whatever works, e.g. json.
Any suggestions where I can read up on your first suggestion?
The next few days events data looks like this:
The first file is the same but with heights every 10 mins.
In the 2nd REST sensor, the API also returns “High water” or “low water” for each event, but this is absent in the test data set.
You could convert the file to JSON — in the right format — and do something similar to the examples on the HA Docs for REST sensor (e.g. at the bottom) but that would be a little tedious for a very large dataset.
You might consider using the command-line sensor to execute a grep | awk command against your file — using templates for the date fields — and return the proper value. I think the file needs to be in your configuration directory for this to work. A (very rough) example:
Edit: on review, that only works if the entry is the exact date and time; if this executes every minute you’d have to return a non-zero exit code so that no value is saved in between readings.
I’ve made progress with getting a value from the file that lists the 10 min heights like this:
First I create a trigger sensor that only updates on the 10 min intervals
(Somehow the date formatting changed when I converted the whole file).
I’m not sure why awk '{print($2)}' didn’t split the output at the comma delimiter rather than the space, but adding a value_template fixed it anyway.
I’d like to add the time tenmins as an attribute but can’t see how.
It took my aging brain far too long to resolve your great suggestion - for which many thanks. I’ll now look at what you suggest about using the REST sensor to parse the smaller file in JSON format.
EDITED to correct file location:
I’m not having any success using REST to read the json file though. The sensors are not created and the error messages in the log says Timeout while fetching data: http://192.1.64:8123/local/NewTidalEvents.json
I’ve tried using CURL in the terminal window to test this ie curl -X http://192.1.64:8123/local/NewTidalEvents.json
But i get Invalid method encountered: b'get /config/www/NewTidalEvents.json HTTP/1.1'
The problem seems to be in accessing a local file but I can’t find a description of how to do this in the RESTful docs.
This is what I’ve got:
NewTidalEvents.json:
To wrap up this thread, I hope it might be useful to some future reader to post my working solution to using the command_line sensor. This gets a day’s worth of records from the csv (starting from now) file and extracts the first one where the height falls below a threshold. It corrects where the records splits (on a comma not a space).