tyjtyj
(Justin T.)
February 17, 2019, 11:00am
1
How to sent jinja in URL ?
sample url
https://api.data.gov.sg/v1/environment/2-hour-weather-forecast?date_time=2019-01-17T18:13:00
sensor:
- platform: rest
resource: "https://api.data.gov.sg/v1/environment/2-hour-weather-forecast?date_time={{ now().strftime('%Y-%m-%dT%H:%M:00') }}"
method: GET
value_template: '{{value_json.item.forecasts.0.forecast}}'
DEBUG
2019-02-17 19:24:22 DEBUG (SyncWorker_0) [homeassistant.components.sensor.rest] Updating from https://api.data.gov.sg/v1/environment/2-hour-weather-forecast?date_time=%7B%7B%20now().strftime(%22%25Y-%25m-%25dT%25H:%25M:00%22)%20%7D%7D
ajoyce
(Al)
February 17, 2019, 11:05am
2
REST sensor doesn’t support templating in the URL unfortunately - however you can achieve this with a command_line sensor instead:
- platform: command_line
name: forecast
value_template: '{{value_json.item.forecasts.0.forecast}}'
command: >-
curl '"'https://api.data.gov.sg/v1/environment/2-hour-weather-forecast?date_time={{ now().strftime("%Y-%m-%dT%H:%M:00") }}"'"
tyjtyj
(Justin T.)
February 17, 2019, 11:31am
3
Got this error instead
Error loading /config/configuration.yaml: mapping values are not allowed here
ajoyce
(Al)
February 17, 2019, 11:45am
4
Can you post your configuration.yaml?
tyjtyj
(Justin T.)
February 17, 2019, 11:52am
5
ajoyce:
n
some indentation issue
- platform: command_line
name: forecast
value_template: '{{value_json.item.forecasts.0.forecast}}'
command: >-
curl '"'https://api.data.gov.sg/v1/environment/2-hour-weather-forecast?date_time={{ now().strftime("%Y-%m-%dT%H:%M:00") }}"'"
```
It looks correct now but I still trying to get the data from json
ajoyce
(Al)
February 17, 2019, 12:08pm
6
Sorry about the indentation error! What you have there is correct.
The problem is your value_template - it doesn’t match up at all with the returned JSON from the request you’re sending. The request just returns a load of places and coordinates:
{
"area_metadata":[
{
"name": "Ang Mo Kio",
"label_location":{
"latitude": 1.375,
"longitude": 103.839
}
},
{
"name": "Bedok",
"label_location":{
"latitude": 1.321,
"longitude": 103.924
}
},
{
"name": "Bishan",
"label_location":{
"latitude": 1.350772,
"longitude": 103.839
}
You need to go back and double check your GET request
tyjtyj
(Justin T.)
February 17, 2019, 1:30pm
7
Got it running
- platform: command_line
name: sgneaweb
command: >-
curl "'"https://api.data.gov.sg/v1/environment/2-hour-weather-forecast?date_time={{ now().strftime("%Y-%m-%dT%H:%M:00") }}"'"
value_template: >-
{%- for entry in value_json['items'][0].forecasts -%}
{%- if entry.area == 'Boon Lay' -%}{{ entry.forecast }}{%- endif -%}
{%- endfor -%}