RESTful sensor - Multiple keys with the same name

Pretty new to RESTful sensors and also JSON for that matter… I am trying to create a sensor to get high/low tide times and heights using the Storm Glass API https://stormglass.io/. The difficulty is that the two high and two low tides each day are described in JSON with keys of the same name. The API call returns:

{  
   "extremes":[  
      {  
         "height":-0.030679245283018908,
         "timestamp":"2019-05-14 03:35:52+00:00",
         "type":"low"
      },
      {  
         "height":1.469320754716981,
         "timestamp":"2019-05-14 09:54:05+00:00",
         "type":"high"
      },
      {  
         "height":0.08932075471698109,
         "timestamp":"2019-05-14 15:58:19+00:00",
         "type":"low"
      },
      {  
         "height":1.639320754716981,
         "timestamp":"2019-05-14 22:12:32+00:00",
         "type":"high"
      }
   ],
   "meta":{  
      "cost":1,
      "dailyQuota":50,
      "end":"2019-05-15 00:00",
      "lat":27.7621,
      "lng":-15.5644,
      "requestCount":4,
      "start":"2019-05-14 00:00",
      "station":{  
         "distance":41,
         "lat":28.1,
         "lng":-15.4,
         "name":"las palmas",
         "source":"UHSLC"
      }
   }
}

The values I care about are tied to the height, timestamp and type keys under extremes.

This sensor definition…

# Maspalomas tides
- platform: rest
  name: Masplomas Tides
  resource: "https://api.stormglass.io/v1/tide/extremes/point?lat=27.8&lng=-15.6&start=2019-05-14&end=2019-05-15"
  headers:
      Authorization: !secret storm_glass_key
  json_attributes:
    - extremes
    - meta
  value_template: '{{ value_json.tides }}'
  scan_interval: 14400

…results in,


…where the different key/value pairs under extremes are not captured.

Any suggestions as to how to get the two high and two low tide data sets each day?

Thanks!

I believe this result:

extremes: [object Object], [object Object], [object Object], [object Object]

is due to a limitation in how json_attributes is implemented. The value of extremes is a list and not a simple numeric or string or even a dictionary.

To provide supporting evidence, I carried out an experiment using an MQTT sensor configured similarly to your REST Sensor.

  - platform: mqtt
    name: 'Test Tides'
    state_topic: test/tides
    value_template: '{{value_json.tides}}'
    json_attributes:
     - extremes
     - meta

I published the JSON dictionary you posted above to test/tides and sensor.test_tides produced the same result you experienced. The extremes attribute contained [object Object], etc and meta contained useful information.

I converted the value of extremes from a list to a dictionary.

{  
   "extremes":{
      "morning_low": 
      {  
         "height":-0.030679245283018908,
         "timestamp":"2019-05-14 03:35:52+00:00",
         "type":"low"
      },
      "morning_high": 
      {  
         "height":1.469320754716981,
         "timestamp":"2019-05-14 09:54:05+00:00",
         "type":"high"
      },
      "evening_low": 
      {  
         "height":0.08932075471698109,
         "timestamp":"2019-05-14 15:58:19+00:00",
         "type":"low"
      },
      "evening_high": 
      {  
         "height":1.639320754716981,
         "timestamp":"2019-05-14 22:12:32+00:00",
         "type":"high"
      }
   },
   "meta":{  
      "cost":1,
      "dailyQuota":50,
      "end":"2019-05-15 00:00",
      "lat":27.7621,
      "lng":-15.5644,
      "requestCount":4,
      "start":"2019-05-14 00:00",
      "station":{  
         "distance":41,
         "lat":28.1,
         "lng":-15.4,
         "name":"las palmas",
         "source":"UHSLC"
      }
   }
}

That was sufficient to allow it to be properly imported into the attribute. It supports the theory that values in list format are handled differently (as in not at all).

1 Like

Thanks so much for putting in the effort to test. I really appreciate it. Your answer looks definitive.

Guessing I’ll need to parse the JSON outside of HA first…

Hello,
I’ve made improvement in WorldTideInfo in order to reduce the number of times the credit are used.
Hope if it an help
https://community.home-assistant.io/t/world-tide-info-v2-api-graph/260063