ACtually, you can just put |to_json
at the end of the value template in your REST sensor.
I’ll give that a go.
EDIT: Yep. That did the trick. Thanks Steve.
- platform: rest
resource: http://www.bom.gov.au/australia/tides/scripts/getNextTides.php?aac=TAS_TP003&offset=false&tz=Australia%2FHobart
name: Hobart Tides
value_template: '{{ value_json.results|to_json }}'
headers:
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36
Content-Type: application/json
scan_interval: 21600 # every six hours should be plenty fast enoough.
- platform: template
sensors:
hobart_tide_update_time:
entity_id: sensor.hobart_tides
friendly_name: 'Last Updated'
value_template: >
{{ (states('sensor.hobart_tides')|from_json).current_time|int|timestamp_custom("%a %H:%M", true) }}
hobart_high_tide_time:
entity_id: sensor.hobart_tides
friendly_name: 'Next High Tide At'
value_template: >
{{ (states('sensor.hobart_tides')|from_json).next_high.time|int|timestamp_custom("%a %H:%M", true) }}
hobart_high_tide_height:
entity_id: sensor.hobart_tides
friendly_name: 'High Tide Height'
value_template: >
{{ (states('sensor.hobart_tides')|from_json).next_high.height }}
unit_of_measurement: m
hobart_low_tide_time:
entity_id: sensor.hobart_tides
friendly_name: 'Next Low Tide At'
value_template: >
{{ (states('sensor.hobart_tides')|from_json).next_low.time|int|timestamp_custom("%a %H:%M", true) }}
hobart_low_tide_height:
entity_id: sensor.hobart_tides
friendly_name: 'Low Tide Height'
value_template: >
{{ (states('sensor.hobart_tides')|from_json).next_low.height }}
unit_of_measurement: m
You may also want to consider using the JSON attributes property on that REST sensor. It’ll break all that stuff out into the sensor attributes if you want.
Just remember that if you can access fields on it, it’s not actually JSON, it’s an in-memory object that is constructed USING the JSON. The default “string” form is python’s native one, which is that weird, single-quoted JSON-like string. To get it to an ACTUAL JSON string, you need ‘to_json
’.
It’ll break all that stuff out into the sensor attributes if you want.
I didn’t think you could use nested json in restful sensor attributes?
Hrm…Maybe I’m wrong in that regard, but you have to be careful that the actual state doesn’t exceed 255 characters. The attributes don’t have that limitation.
Yeah I did check that. It was something like 130 characters for the current readings, so plenty of room for small changes.
Thanks guys for all the input really appreciate it. I’m sure I’ll be able to use your suggestions above to get it working.
In the interests of completeness another approach that I think is close, is to use command_line
combined with an inline python script. I’ve tried two versions;
- platform: command_line
command: python3 -c "import requests; print(requests.get('http://www.bom.gov.au/australia/tides/scripts/getNextTides.php?aac=VIC_TP020&offset=false&tz=Australia%2FMelbourne').json()['results'])"
name: flinders high
This tries to use python to dump the JSON i.e. json.dumps(str)
- platform: command_line
command: python3 -c "import requests; import json; str = requests.get('http://www.bom.gov.au/australia/tides/scripts/getNextTides.php?aac=VIC_TP020&offset=false&tz=Australia%2FMelbourne').json()['results']; print(json.dumps(str))"
name: flinders
Then use a template sensor, (which is NOT working) to display the values;
platform: template
sensors:
six:
value_template: '{{ states.sensor.flinders.next_high.time }} {{ states.sensor.flinders.next_high.height }}'
I feel this approach should work and is close, just need to work out the value_template
part.
Thanks again for you help.
No probs but I don’t feel I deserve that solution tag. it was @lolouk44 who solved the restful sensor problem.
I’ll mark both as the solution, yours has a complete working example for others coming here.
This was my first post on the forum and it was a really positive experience. Thanks again. To the three of you @tom_l, SteveDinn and @lolouk44
This is awesome. I was looking for this. I changed the location and it works. I also added this sensor:
hobart_tide_dir:
entity_id: sensor.hobart_tides
friendly_name 'Tide Direction'
value_template: >-
{% if (states('sensor.hobart_tides')|from_json).next_high.time|int < (states('sensor.hobart_tides')|from_json).next_low.time|int %}
Rising
{% else %}
Falling
{% endif %}
I couldn’t find anything from the API, but if there is a way to pull the previous tide high and low values, you could calculate the current tide since the tide cycle is of a predictable duration. If not, could you save the previous value?
This worked for me. Thanks for sharing.
Interesting. I stopped using it a while ago as I had no use for it. I thought the recently implemented BoM anti-scraping measures might have stopped it working.
Sorry I may have missed it but I can not find the details on BOM to change the location, I want to get the tides for Ettalong in NSW.
Would someone please be able to point me in the right direction
Also i was having a look at tides4fishing and really like the way they display the tides, is there any way to copy this over to home assistant.
To find what the URL needs to be for your location, go to http://www.bom.gov.au/australia/tides/ and select your location, then download one of the tide chart pdfs. The filename of the pdf will end with something like VIC_TP020
- substitute this with the URL and then change the timezone city as appropriate.
Hi Guys,
Cant get this URL to work for my location to save my life…
Anyone crack it for me? Will prob be bashing my head because its something simple…
Location: Camp Cove, NSW
LOCATION: NSW_TP011
TZ: Sydney
But that url is invalid and cannot figure out to save my life lol
Help appreciated
I’ve been able to get this working using that location:
rest:
- resource: "http://www.bom.gov.au/australia/tides/scripts/getNextTides.php?aac=NSW_TP011&offset=false&tz=Australia%2FSydney"
headers:
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36
Content-Type: application/json
sensor:
- name: "Sydney Low Tide Height"
value_template: "{{ value_json.results.next_low.height }}"
unit_of_measurement: "m"
- name: "Sydney High Tide Height"
value_template: "{{ value_json.results.next_high.height }}"
unit_of_measurement: "m"
- name: "Sydney Low Tide Time"
value_template: "{{ value_json.results.next_low.time | timestamp_local }}"
- name: "Sydney High Tide Time"
value_template: "{{ value_json.results.next_high.time | timestamp_local }}"
Hi Dom,
Thanks for this… Works a charm
Only issue I see is time/date format reporting the follwoing “2023-06-21T22:28:00+10:00” for sensor
Cheers
G
It should just work using your local time/date as set in HA.
The time/date just comes through from the BOM as epoch time, so you should be able to pipe that to any format you want - change timestamp_local to any custom timestamp you want. The first couple of posts in this threat should be helpful: Convert date and time template