RestAPI Template parsing

Hello there

I’m unable to parse some easy restful API calls and I dont know why.
http://transport.opendata.ch/v1/locations?query=8500010

Ill try to extract different values from this request but It’s not working. The error i get is:
UndefinedError: ‘value_json’ is undefined

I tried so many different things but nothing seems to work:

resource: http://transport.opendata.ch/v1/locations?query=8500010
name: Stationboard
value_template: "{{ value_json.stations}}"

I tried with

value_template: “{{ value_json.stations.name}}”
value_template: “{{ value_json[0].stations}}”

and so on.
My goal is to read out some Train Departures like this:
http://transport.opendata.ch/v1/connections?from=Lausanne&to=Genève
So this is way bigger and thats the one i plan to use, but aswell im not able to get any response.

Further, If this should work someday how can i parse the Date I recive? f.g.
* departure: "2021-05-04T18:12:00+0200",

It would be nice if we have the european style: 04-05-2021 and if its not too much, because it queries always the next connection, i just need the time settings without the +0200 like 18:12:00

Thanks for the help!

The stations key contains a list. Try this:

value_template: "{{ value_json.stations[0].name }}"

Hi Taras.
Thanks!!
Edit
It worked. it was in the developer tools causing problems.

do you have a tipp for my date and time parsing problem perhaps?

I did now query every point one by one. Is there a possibility to read out multiple informations without using value_template for any?

Because if i want to get the 3 connection to one place i end up with 9 entries (departure, arrival, delay) and for every station it gets more and more :slight_smile:

As for now, My API Calls are done for today (shit its only 0:45)

so as far, API query does work. I get the time back of the arrival.

  • platform: rest

    resource: http://xxxxxx
    name: sensorname

    value_template: "{{ value_json.connections[0].to.arrival }}

The Timeformat change i could test aswell in the developer template settings. Thats working aswell:

{{as_timestamp(states(‘sensor.sensorname’)) | timestamp_custom(‘%A %-d. %b – %X’ ‘true’) }}

But now i dont know how to integrate this into my config.yaml
How do i get this in there.

Copy-paste this into the Template Editor and experiment with it. It shows several ways to convert and manipulate a time string.

{% set ds = '2021-05-04T18:12:00+0200' %}
Time string: {{ ds }}
{% set ts = ds | as_timestamp %}
Timestamp: {{ ts }}

Local time: {{ ts | timestamp_local }}

European time format: {{ ts | timestamp_custom('%d-%m-%Y %H:%M:%S') }}
{% set dto = now().fromtimestamp(ts) %}
Date Time Object: {{ dto }}
Year: {{ dto.year }}
Month: {{ dto.month }}
Day: {{ dto.day }}
Hour: {{ dto.hour }}
Minute: {{ dto.minute }}
Second: {{ dto.second }}
ISO Day of the Week: {{ dto.isoweekday() }}

Screenshot showing the template’s results.

thanks for the tip. i already have my time set right, but i dont know how to enter this command into the config file and where

{{as_timestamp(states(‘sensor.traveltoxxx’)) | timestamp_custom(’%A %-d. %b – %X’ ‘true’) }}

And thats my actual config:

  • platform: rest

    resource: http://xxxx

    name: traveltoxxx

    value_template: “{{ value_json.connections[0].to.arrival }}”

Any ideas?

Please format your code with the </> button, or with three backticks (see Rule 11). That preserves indentation and avoids problems like replacement of quotes with “smart quotes”.

Try this:

- platform: rest
  resource: http://xxx
  name: traveltoxxx
  value_template: "{{ as_timestamp(value_json.connections[0].to.arrival)|timestamp_custom('%A %-d. %b – %X', true) }}"