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… ?

Then:
- 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 http://192.168.1.96:8000/getdoorstatus)
 if [[ -z $STAT  ]]; then
  echo 0
 else
   echo "$STAT"
 fi

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.

#luftpartiklar
  - 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"
#luftpartiklar
  - 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:
{“StartTime”:"/Date(1520884800000+0100)/",“StopTime”:"/Date(1520888400000+0100)/",“Weather”:{“Temperature”:{“Value”:3.44,“Unit”:“°C”},“Humidity”:{“Value”:100,“Unit”:"%"},“SolarInsolation”:{“Value”:-0.25,“Unit”:“W/m2”},“AirPressure”:{“Value”:989.5,“Unit”:“hPa”},“WindSpeed”:{“Value”:1.29,“Unit”:“m/s”},“WindDirection”:{“Value”:309.57,“Unit”:“°”},“RainFall”:{“Unit”:“mm”}},“AirQuality”:{“TotalIndex”:0.91,“NO2”:{“Value”:36.28,“Unit”:“ppb”,“Index”:0.91},“SO2”:{“Value”:4.69,“Unit”:“ppb”,“Index”:0.09},“O3”:{“Value”:6.49,“Unit”:“ppb”,“Index”:0.08},“NOx”:{“Value”:42.11,“Unit”:“ppb”,“Index”:0.14}}}

Any thoughts? :slight_smile: