rule-enginethingsboard

Thingsboard REST api call - variable response


Following this tutorial - weather using rest api calls - I’m trying to extend the example to gather the 1 hour accumulated rain data. The Openweather api docs state:

If you do not see some of the parameters in your API response it means that these weather phenomena are just not happened for the time of measurement for the city or location chosen. Only really measured or calculated data is displayed in API response.

No accumulated rain volume means there is no rain section in the response. How do I deal with this in the rule engine? Essentially a conditional rule (if rain.1h is present, use rain.1h, else set rain.1h = 0)

Thanks


Solution

  • Rule Chain script is based on JavaScript. Response from thirdparty service will be in msg.

    To check if rain.1h is in response:

    if (msg.rain && msg.rain.1h) {
    ...
    }