Tide Times Via Control Page

Posted on
Tue Sep 11, 2018 10:48 am
mwoodage offline
User avatar
Posts: 174
Joined: Dec 19, 2014
Location: Devon UK

Tide Times Via Control Page

Hello,
I'm looking for some advice on how i can get local (Devon UK) tide times to appear on a control page. We quite often go walking along the local sea front and manually look up tide times, it would be great to have them showing on my control home page so we can glance at them before going out.

I've been looking at the following web site which gives tide times as an API, but i've next to zero idea of how to get the detail on to a control page. Does anyone have a quick answer?
https://environment.data.gov.uk/flood-monitoring/doc/tidegauge

An idiots guide would be great.....

Thanks
Martin

Posted on
Tue Sep 11, 2018 3:39 pm
racarter offline
User avatar
Posts: 468
Joined: Jun 18, 2016
Location: North Yorkshire, UK

Re: Tide Times Via Control Page

I can't see any tide TIMES provided by that site; only tide LEVELS. If levels would suffice you can get them into a variable by running this Python script. You should run this as an external script because it accesses a website and may take a while to complete.

This script uses the example URL used on the web page; obviously you'd have to amend this to point at the station in which you're interested.

Code: Select all
#!/usr/bin/python
# encoding=utf8

import urllib2, json

url1 = 'http://environment.data.gov.uk/flood-monitoring/id/stations/E72639'

response = urllib2.urlopen(url1)

data = json.loads(response.read())

tideLevel = str(data["items"]["measures"]["latestReading"]['value'])

indigo.variable.updateValue(12345678, tideLevel)   # Replace 12345678 with the ID of your variable

Posted on
Wed Sep 12, 2018 1:08 pm
mwoodage offline
User avatar
Posts: 174
Joined: Dec 19, 2014
Location: Devon UK

Re: Tide Times Via Control Page

Hi recanter,
thank you so much for your help - i tried the script and I can get it to populate a variable - success.
However, as you mentioned it only gives me the tide level, not much help really, so I've been looking at other sites.

I've found this web site that gives tide times, however it does require you to sign up.
https://admiraltyapi.portal.azure-api.net

i've signed up and tried copying sections of the code they detail, together with the station code (0027) and the authorisation key, but can't get it to work - is there an easy way to get the tide times from this site.

Sorry to ask but it would be great if we can sort this out :D

Thanks again
Martin

Posted on
Wed Sep 12, 2018 3:04 pm
neilk offline
Posts: 714
Joined: Jul 13, 2015
Location: Reading, UK

Re: Tide Times Via Control Page

The following python script
Code: Select all
import httplib, urllib, base64

headers = {
    # Request headers
    'Ocp-Apim-Subscription-Key': '<your-api-key>',
}

params = urllib.urlencode({
})

try:
    conn = httplib.HTTPSConnection('admiraltyapi.azure-api.net')
    conn.request("GET", "/uktidalapi/api/V1/Stations/0027/TidalEvents?duration=1%s" % params, "{body}", headers)
    response = conn.getresponse()
    data = response.read()
    print(data)
    conn.close()
except Exception as e:
    print("[Errno {0}] {1}".format(e.errno, e.strerror))


generates the output

Code: Select all
[{"EventType":"LowWater","DateTime":"2018-09-12T02:10:42","IsApproximateTime":false,"Height":-0.11082818269453196,"IsApproximateHeight":false,"Filtered":false},{"EventType":"HighWater","DateTime":"2018-09-12T08:19:45.35","IsApproximateTime":false,"Height":4.1403555545034791,"IsApproximateHeight":false,"Filtered":false},{"EventType":"LowWater","DateTime":"2018-09-12T14:26:00","IsApproximateTime":false,"Height":-0.0073008353119995983,"IsApproximateHeight":false,"Filtered":false},{"EventType":"HighWater","DateTime":"2018-09-12T20:31:11.15","IsApproximateTime":false,"Height":4.2886176564094765,"IsApproximateHeight":false,"Filtered":false}]


Once you have created an API key in the subscriptions part of the website, you can insert it in the script. This example is for station 0027, and a duration of "1" is just today.

That should give you a start to selecting the parts you want and populating the variables. I am way out of my depth but fancied trying to figure it out.

I also tried using GhostXML, but not sure how you can pass the API token as a header, if anyone can help with a pointer for that it becomes a whole lot easier.

I will keep playing, but suspect I am at the end of my knowledge / python skills.

Posted on
Wed Sep 12, 2018 3:22 pm
racarter offline
User avatar
Posts: 468
Joined: Jun 18, 2016
Location: North Yorkshire, UK

Re: Tide Times Via Control Page

Here's a script to look at the tide times for today and tomorrow and populate two variables for "Next Low Tide" and "Next High Tide". You'll have to enter your own subscription key, and change the station ID to one near you. (0184 is Whitby).

Good luck!

Code: Select all
#!/usr/bin/python
# encoding=utf8

import httplib, urllib, base64, json, datetime

headers = {
    # Request headers
    'Ocp-Apim-Subscription-Key': 'alu43453534u3l4',           #Enter your own key here
}

params = urllib.urlencode({
    # Request parameters
    'duration': '2',
})

try:
    conn = httplib.HTTPSConnection('admiraltyapi.azure-api.net')
    conn.request("GET", "/uktidalapi/api/V1/Stations/0184/TidalEvents?%s" % params, "{body}", headers)           #Change 0184 to the station you're interested in
    response = conn.getresponse()
    data = response.read()
    conn.close()
except Exception as e:
    indigo.server.log("[Errno {0}] {1}".format(e.errno, e.strerror))

tideData = json.loads(data)
lines = 0

for n in range(0, 7):
   eventTime = tideData[n]["DateTime"]
   tideTime = datetime.datetime.strptime(eventTime, '%Y-%m-%dT%H:%M:%S')
   if tideTime > datetime.datetime.now():
      tideTimeF = datetime.datetime.strftime(tideTime, '%Y-%m-%d %H:%M')
      if tideData[n]["EventType"] == "LowWater":
         indigo.variable.updateValue(12345, tideTimeF)   # Replace 12345 with the ID of your "Next Low Tide" variable
      else:
         indigo.variable.updateValue(67890, tideTimeF)   # Replace 67890 with the ID of your "Next High Tide" variable
      lines +=1
      if lines == 2:
         return

Posted on
Wed Sep 12, 2018 4:07 pm
mwoodage offline
User avatar
Posts: 174
Joined: Dec 19, 2014
Location: Devon UK

Re: Tide Times Via Control Page

This is fantastic, thank you both neil and recarter,

recanter, i've got this working and can get the variables to update using the Whitby code of 0184, but when i change it to my code 0027, it gives me an error of "embedded script :unconverted data remains .35"

I think the data being returned has an extra set of numbers (.35) - in my limited knowledge. I've tried adding in an extra %S into this line but can't make it work
tideTime = datetime.datetime.strptime(eventTime, '%Y-%m-%dT%H:%M:%S')

We're nearly there :D

Thanks
Martin
Attachments
Grab.png
Grab.png (53.29 KiB) Viewed 3642 times

Posted on
Wed Sep 12, 2018 6:03 pm
racarter offline
User avatar
Posts: 468
Joined: Jun 18, 2016
Location: North Yorkshire, UK

Re: Tide Times Via Control Page

The problem is that some locations include fractions of a second in the tide times. Pretty silly!

Replace the original with this line, which masks out the fractions of a second:

Code: Select all
tideTime = datetime.datetime.strptime(eventTime[:18], '%Y-%m-%dT%H:%M:%S')

Posted on
Thu Sep 13, 2018 12:11 am
mwoodage offline
User avatar
Posts: 174
Joined: Dec 19, 2014
Location: Devon UK

Re: Tide Times Via Control Page

Thank you so much racarter - it works perfectly :D

I now have tide times on my iPad control page

Thanks again
Martin

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 2 guests