I’m not good at English. Please understand.
Hello.
I’m trying to get weather json data from webpage.
URL changes every hour so “platform rest” cannot get the value.
Code is below.
{% set api_key = “something” %}
{% set url = “http://weather_homepage.com/weather?ServiceKey=” %}
{% set loc = “nx=120&ny=120” %}
{{ url }}{{ api_key }}&base_date={% if now().hour == 0 and now().minute < 40 %}{{ (as_timestamp(now()) - (243600)) | timestamp_custom("%Y%m%d", True) }}&base_time={{ (as_timestamp(now()) - (13600)) | timestamp_custom("%H", True) }}00{% elif now().hour > 0 %}{{ as_timestamp(now()) | timestamp_custom("%Y%m%d", True) }}&base_time={{ as_timestamp(now()) | timestamp_custom("%H", True) }}00{% endif %}&{{ loc }}&_type=json
It returns url “http://weather_homepage.com/weather?ServiceKey=something&base_date=20180330&base_time=0100&nx=120&ny=120&_type=json”
(http://local_server:8123/dev-template)
and then, navigate to url above, webpage shows Json value like below.
{“response”:{“header”:{“resultCode”:“0000”,“resultMsg”:“OK”},“body”:{“items”:{“item”:[{“baseDate”:20180328,“baseTime”:“0200”,“category”:“LGT”,“nx”:120,“ny”:120,“obsrValue”:0},]},“numOfRows”:1,“pageNo”:1,“totalCount”:1}}}
I want to see that in sensor.
- platform: template
sensors:
weather_village:
value_template: ‘{{ something.attributes.response.header.items.item[0][“obsrValue”] }}’
Below code not works.
- platform: rest
name: weather
scan_interval: 600
value_template: ‘{{ value_json }}’
resource: ‘{% set api_key = “something” %}
{% set url = “http://weather_homepage.com/weather?ServiceKey=” %}
{% set loc = “nx=120&ny=120” %}
{{ url }}{{ api_key }}&base_date={% if now().hour == 0 and now().minute < 40 %}{{ (as_timestamp(now()) - (243600)) | timestamp_custom("%Y%m%d", True) }}&base_time={{ (as_timestamp(now()) - (13600)) | timestamp_custom("%H", True) }}00{% elif now().hour > 0 %}{{ as_timestamp(now()) | timestamp_custom("%Y%m%d", True) }}&base_time={{ as_timestamp(now()) | timestamp_custom("%H", True) }}00{% endif %}&{{ loc }}&_type=json’
Is there any way possible?
If possible. please help me.