If api not available return 0

Can anyone help me to return 0 when api is not available.
I want to get rid of all errors generated.

  - platform: rest
    name: GBG_AirPressure
    resource: http://data.goteborg.se/AirQualityService/v1.0/LatestMeasurement/API_KEY?format=json
    method: GET
    value_template: '{{ value_json.Weather.AirPressure["Value"] }}'
    unit_of_measurement: "hPa"

Hmmm …

either you run a proxy that can deal with this situation or you could download the URL’s content periodically using cron and wget/curl to a file and use the File Sensor component on it. The File Sensor will return the value from the last successful download, not 0

Sounds like a great solution to use the last successful value.
I am pretty new to this and have not used cron and wget/curl before, is that hard to learn?

Absolutely not.

  • wget should already be installed, if not use your distribution’s package manager to get it.
  • wget command should be something like wget -O /tmp/airquality.json 'http://data.goteborg.se/AirQualityService/v1.0/LatestMeasurement/API_KEY?format=json' (test on console first)
  • cron is configured using command crontab -e, a new line is appended like
    */5 * * * * /usr/bin/wget -q -O /tmp/airquality.json 'http://data.goteborg.se/AirQualityService/v1.0/LatestMeasurement/API_KEY?format=json' (fetches every 5 minutes, -q is for “quiet”)

As a result, you should find a file /tmp/airquality.json for further parsing by the file sensor.

I forgot to mention that i am using hassio,

So is it in the sensor.yaml i should put this wget… ?

- platform: file
file_path: /tmp/airquality.json
value_template: ‘{{ value_json.AirQuality.O3[“Value”] }}’
unit_of_measurement: “ppb”

Here’s how I handle that:

Instead of using a REST sensor, I use a command line sensor.
Then, I put my REST call in a bash script, and handle the missing API error in the script like this:

 STAT=$(curl -s --connect-timeout 5 -X GET
 if [[ -z $STAT  ]]; then
  echo 0
   echo "$STAT"

Getting more and more difficult for me as beginner

I understand. Let’s get back to the errors, is the remote API unstable or are your requests blocked maybe due to too frequent calls?

It is unstable, it consists of several values in the api.

At the moment there is two sensors not returning any data.

  - platform: rest
    name: GBG_PM10
    resource: http://data.goteborg.se/AirQualityService/v1.0/LatestMeasurement/api-key?format=json
    method: GET
    value_template: '{{ value_json.AirQuality.PM10["Value"] }}'
    unit_of_measurement: "µg/m3"
  - platform: rest
    name: GBG_PM2_5
    resource: http://data.goteborg.se/AirQualityService/v1.0/LatestMeasurement/api-key?format=json
    method: GET
    value_template: '{{ value_json.AirQuality.PM2_5["Value"] }}'
    unit_of_measurement: "µg/m3"

Here is what the API returns right now:

Any thoughts? :slight_smile: