So, I just learned something about the RESTful Sensor. Although there’s no way to make the state (i.e., what comes out of value_template) a dictionary (even if it looks like a dictionary, it’s really always a string), the same is not true for the json_attributes. So, that sheds a new light on this!
One other comment about what you tried. I tried it myself - i.e., value_template: "{{ value_json.results }}"
- and the reason it doesn’t work is because the output is too long. States strings are limited to 255 characters.
So, the following you might find useful:
sensor:
- platform: rest
resource: https://api.sunrise-sunset.org/json?lat=XX.xxx&lng=YY.yy
name: "Sun Rise and Set"
value_template: "{{ value_json.status }}"
json_attributes:
- results
- platform: template
sensors:
sunrise:
friendly_name: "Sunrise"
value_template: "{{ states.sensor.sun_rise_and_set.attributes.results.sunrise }}"
sunset:
friendly_name: "Sunset"
value_template: "{{ states.sensor.sun_rise_and_set.attributes.results.sunset}}"
day_length:
friendly_name: "Day Length"
value_template: "{{ states.sensor.sun_rise_and_set.attributes.results.day_length }}"
I just tried this with my lat/lon and got:
{{ states('sensor.sun_rise_and_set') }}
{{ states('sensor.sunrise') }}
{{ states('sensor.sunset') }}
{{ states('sensor.day_length') }}
OK
5:19:08 AM
7:29:18 PM
14:10:10
EDIT: So those values were actually using the example lat/lon from the sunrise-sunset.org website. When I updated with my real lat/lon, I noticed the times are being reported in UTC, not local. Doesn’t matter for day_length, but it does for sunrise and sunset. So, although this can work, some more massaging is necessary to get the sunrise and sunset values into something actually useful.
EDIT 2: It’s probably best to add &formatted=0
to the end of the resource URL. That way the sunrise & sunset times will be full date/time strings, which can then be converted to whatever you want. Also, day_length will be in seconds, again easily convertible to whatever display format you want.
E.g.:
{{ states('sensor.sun_rise_and_set') }}
{{ states('sensor.sunrise') }}
{{ states('sensor.sunset') }}
{{ states('sensor.day_length') }}
{{ as_timestamp(states('sensor.sunrise'))|timestamp_custom('%I:%M:%S %p') }}
{{ as_timestamp(states('sensor.sunset'))|timestamp_custom('%I:%M:%S %p') }}
{{ states('sensor.day_length')|int|timestamp_custom('%H:%M:%S',false) }}
gets:
OK
2018-07-26T10:42:20+00:00
2018-07-27T01:16:23+00:00
52443
05:42:20 AM
08:16:23 PM
14:34:03