Rest_command and help with parsing the response

I am trying to use use rest_command (RESTful command) to GET tide data for my location. I have the basics working and it is returning the following data in the response variable (tide_data)

{'content': '{\n\t"results": {\n\t\t"current_time": 1745554800,\n\t\t"next_high": {\n\t\t\t"time": 1745569620,\n\t\t\t"height": 1.72\n\t\t},\n\t\t"next_low": {\n\t\t\t"time": 1745591460,\n\t\t\t"height": 0.46\n\t\t}\n\t}\n}', 'status': 200}

I have been able to use “{{ tide_data.status }}” to return 200

I can also use “{{ tide_data.content }}” to return

{
"results": {
"current_time": 1745554800,
"next_high": {
"time": 1745569620,
"height": 1.72
},
"next_low": {
"time": 1745591460,
"height": 0.46
}
}
}

But I can’t work out how to access / map the data within the results of the content.

I have tried “{{ tide_data.content.results.current_time }}” and many variants of this!

I think I am doing something stupid so any help appreciated.

I think I know what is happening. Looks like tide_data.content is a string not a json object that I can use.

Is this correct and is there anything I can do to use rest_command?

I have used rest and have made good progress.

rest:
  - resource: http://www.bom.gov.au/australia/tides/scripts/getNextTides.php?aac=NSW_TP007&offset=false&tz=Australia%2FSydney
    method: GET
    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: 43200 # Every 12 hours. Additioanlly using automation to trigger at high tide or low tide
    sensor:
      - name: "Tide Update Time"
        value_template: "{{ value_json.results.current_time | int | timestamp_custom('%I:%M %p',true) }}"
      - name: "Tide Update Time Stamp"
        value_template: "{{ value_json.results.current_time | int }}"        
      - name: "Tide High Time"
        value_template: "{{ value_json.results.next_high.time | int | timestamp_custom('%I:%M %p',true) }}"
      - name: "Tide High Time Stamp"
        value_template: "{{ value_json.results.next_high.time | int }}"        
      - name: "Tide High Height"
        value_template: "{{ value_json.results.next_high.height }}"
        unit_of_measurement: "m"
      - name: "Tide Low Time"
        value_template: "{{ value_json.results.next_low.time | int | timestamp_custom('%I:%M %p',true) }}"
      - name: "Tide Low Time Stamp"
        value_template: "{{ value_json.results.next_low.time | int }}"        
      - name: "Tide Low Height"
        value_template: "{{ value_json.results.next_low.height }}"
        unit_of_measurement: "m"

Review from the solution onwards from 2019 to what appears to be a very similar question.

Thanks for the link. Much appreciated. I do have it working now using the rest sensor platform.

I was trying to get it to work using rest_command but overall I think rest sensor platform is better for my use case.

For some reason the BOM API detects rest_command as scraping when using content_type: application/json

When using rest I can use content-type in the header and it works.

1 Like

The REST command is to send an instruction (command), not for retrieving data, like toggling a light via a device’s API. Using one of the two REST sensor platforms is the right way for what you have.

1 Like

Thanks @parautenbach - all working using rest sensors plus an automation that updates the sensors at every high or low tide to pick up the next set of tide data.

I was experimenting with rest_command so thanks for clarifying that it is not the best way.

2 Likes