Rest Sensor gets Unkown

Hi all, I want to get value from below API which tells me est time for bus to arrive. I’ve learnt how to get value with get method, but now I am stuck with Post, and having trouble find a right solution.

https://gist.transportdata.tw/gist_web/BusInfo/BusLiveSearch?city=NewTaipei&op=全部業者&type=route&name=F237&direction=1&isUID=false

Here is my f237.yaml config

sensor:
  - platform: rest
    resource: 'https://gist.transportdata.tw/gist_web/BusInfo/BusLiveSearch'
    method: POST
    headers:
        User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:107.0) Gecko/20100101 Firefox/107.0
    payload: '{"city":"NewTaipei","op":"全部業者","type":"route","name":"F237","direction":"1","isUID":false}'
    name: test
    value_template: "{{ value_json['success'] }}"

In my automation, I’ve just tried excuse this and push notification to my phone.

service: notify.mobile_app_chien
data:
  title: BUS
  message: "{{ states('sensor.f237') }}"

First i was able to get False value for few times, then I tried to add more headers data with below under headers:

Content-Type
	application/json

Then I kept getting Unavailable

However, I am able to use postman and get result with Post method:
https://gist.transportdata.tw/gist_web/BusInfo/BusLiveSearch?city=NewTaipei&op=全部業者&type=route&name=F237&direction=1&isUID=false

I’ve also checked command-line sensor, but it seems require python installed with SDK card install method (Raspberry Pi - Home Assistant)

Thanks in advance

If that URL works, why do you try to convert the parameters into a payload? The following configuration gives me a result without payload or headers:

sensor:
  - platform: rest
    resource: 'https://gist.transportdata.tw/gist_web/BusInfo/BusLiveSearch?city=NewTaipei&op=%E5%85%A8%E9%83%A8%E6%A5%AD%E8%80%85&type=route&name=F237&direction=1&isUID=false'
    method: POST
    name: test
    value_template: "{{ value_json['success'] }}"

However, the result is pretty complex and I have no idea what data exactly you want to extract.

Hi thx for reply.

I am able to get result by using postman, but with url ( https://gist.transportdata.tw/gist_web/BusInfo/BusLiveSearch?city=NewTaipei&op=全部業者&type=route&name=F237&direction=1&isUID=false) directly in to browser won’t get me result (will show unsupported)

but with postman, i am able to get as below, and the ‘EstimateTime’ is what i want to get

Which i use value_template: “{{ value_json[‘detail’][0][‘EstimateTime’] }}” but got me Unknown

i’ve tried your sensor code as well, but when i push to my phone with

service: notify.mobile_app_chien
data:
  title: BUS
  message: "{{ states('sensor.f237') }}"

Will get me ‘unavailable’. You got complex result, is it same as the screenshot above?

Thx

Additionally, i tried with another URL (https://tdx.transportdata.tw/api/basic/v2/Bus/EstimatedTimeOfArrival/City/NewTaipei/946副?%24top=30&%24format=JSON)

With the new url, i used as below:
busest.yaml

sensor:
  - platform: rest
    resource: https://tdx.transportdata.tw/api/basic/v2/Bus/EstimatedTimeOfArrival/City/NewTaipei/946%E5%89%AF?%24top=30&%24format=JSON
    name: bus
    value_template: "{{ value_json[0]['EstimateTime'] }}"

automation:

service: notify.mobile_app_chien
data:
  title: BUS
  message: "{{ states('sensor.bus') }}"

But i still so confused why it is giving me ‘unknown’, i have also tried putting common on the url, but no luck

sometimes i get Unknown, sometimes i get Unavailable. What are the difference?

Using that URL I get an actual JSON response in my web browser, but in HA I get the following message in the log.

2022-12-02 19:42:42.225 DEBUG (MainThread) [homeassistant.components.rest.sensor] Data fetched from resource: no Authorization header found

Maybe you can sign up for their API and get an authorisation token or something like that. However, a quick test revealed that the server looks for a User-Agent header, so this appears to work:

sensor:
  - platform: rest
    resource: https://tdx.transportdata.tw/api/basic/v2/Bus/EstimatedTimeOfArrival/City/NewTaipei/946%E5%89%AF?%24top=30&%24format=JSON
    name: bus
    value_template: "{{ value_json[0].EstimateTime }}"
    headers:
      User-Agent: Mozilla/5.0 (iPad; CPU OS 13_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/87.0.4280.77 Mobile/15E148 Safari/604.1

The following configuration also gives me a response:

sensor:
  - platform: rest
    resource: 'https://gist.transportdata.tw/gist_web/BusInfo/BusLiveSearch?city=NewTaipei&op=%E5%85%A8%E9%83%A8%E6%A5%AD%E8%80%85&type=route&name=F237&direction=1&isUID=false'
    method: POST
    name: bus
    value_template: "{{ value_json['detail'][0]['EstimateTime'] }}"

1 Like

Thx for the help, i am able to get result as well now. I think most of the reason is i did not restart HA and not properly use header…

After checking on the webpage, indeed it requires token for more API calls in a day. 1 per day is good enough for me.

This is example i got from the API doc:

curl --request GET \
     --url TDX_API_URI \
     --header 'authorization: Bearer ACCESS_TOKEN'

That means i will add authorization in to header

headers:
      User-Agent: Mozilla/5.0 (iPad; CPU OS 13_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/87.0.4280.77 Mobile/15E148 Safari/604.1
      authorization: 'TOKEN'

Thx again for the help!

1 Like