lazloman
(Lorenzo THurman)
February 6, 2022, 9:13pm
1
Here’s my code in my sensors.yaml:
- platform: rest
resource: https://api.sunrise-sunset.org/json?lat=xx.yyy&lng=-aa.bbb&formatted=0
verify_ssl: false
name: Daylight Length
json_attributes_path: "$.results"
json_attributes:
- results
value_template: "{{ value_json['results']}}"
I’m getting, “Error adding entities for domain sensor with platform rest”
I’ve gone through the docs and read a few related posts here, what am I doing wrong?
Thanks
raphi
(Raphael)
February 6, 2022, 10:08pm
2
This is how a response looks like:
{
"results": {
"sunrise": "2022-02-06T07:40:02+00:00",
"sunset": "2022-02-06T17:52:02+00:00",
"solar_noon": "2022-02-06T12:46:02+00:00",
"day_length": 36720,
"civil_twilight_begin": "2022-02-06T07:11:46+00:00",
"civil_twilight_end": "2022-02-06T18:20:18+00:00",
"nautical_twilight_begin": "2022-02-06T06:37:59+00:00",
"nautical_twilight_end": "2022-02-06T18:54:06+00:00",
"astronomical_twilight_begin": "2022-02-06T06:04:46+00:00",
"astronomical_twilight_end": "2022-02-06T19:27:18+00:00"
},
"status": "OK"
}
I see two problems in your code:
json_attributes should list the attributes you want to gather - in your example this should be something like sunrise, sunset, solar_noon, …
Value templates returns a dictionary (=multiple results) but it should be only one value, in your case you could use “{{ value_json[‘status’]}}” (or whatever attribute you prefer)
lazloman
(Lorenzo THurman)
February 7, 2022, 12:57am
3
Thanks for the reply. I’ve restructured my code:
- platform: rest
resource: https://api.sunrise-sunset.org/json?lat=xx.yyy&lng=-yy.xxx&formatted=0
verify_ssl: false
name: Daylight Length
json_attributes_path: "$.results"
json_attributes:
- day_length
value_template: "{{ value_json['day_length']}}"
Now I get this error:
" * Template variable warning: 'dict object' has no attribute 'day_length' when rendering '{{ value_json['day_length']}}'"
koying
(Chris B)
February 7, 2022, 8:45am
4
value_template: "{{ value_json['results']['day_length']}}"