Use an external API response in a scene

Feature description
You can make an API call in scenes, but only to trigger actions on remote services.

It would be super powerful to be able to make an API call and use the response as a variable.

Concrete example:

  • at 6 a.m., trigger a call to the weather API
  • send me a message with the day’s forecast

Or if I leave home, warn me if it’s going to rain during the day.

I imagine the possibility of retrieving a returned JSON value, either as in JavaScript {{ reponseapi.data.meteo.pluie }} or in graphical form as already done with existing devices.

Great idea! :slight_smile:

I think we’ll do it in a graphical form. We’ll need to make a « test » call when creating the map. Then read the json response and make a list of all available keys, and populate them in the list of available variables :slight_smile:

I’m coming back to this, still with the aim of improving the scenes, I had fun coding this this morning :slight_smile:

Small video example by querying the BTC price on the Coinbase API, then using the API response in the « send a message » box :slight_smile:

Of course, this is an example, it is possible to retrieve any API response!

https://streamable.com/ezw984

The PR is coming soon

Here’s the PR on GitHub →

https://github.com/GladysAssistant/Gladys/pull/1148

Of course, it is also possible to use the data in the « continue only if » box:

:flushed_face: It’s super clean and exactly what I had in mind when writing this topic!

I wanted Gladys to be able to send me messages with data retrieved from external sources, like the weather when I leave home, it’s awesome! :fire:

What happens if:

  • there is no internet?
  • The response is different than expected during execution (like « API threshold exceeded »)?

Good question, I haven’t handled that case, currently it will do a bit of anything ^^

I think we should stop the scene’s execution.

It will do anything, your message will have holes.

After that, I don’t know what the solution would be: validate that the schema is the same, and if it’s not the same, stop the scene? The issue is that some API responses may have variable responses and that shouldn’t necessarily be a reason to stop the scene’s execution… Good question!

Based solely on the API’s return code.

  • 20-second timeout
  • Code 200 = proceed

Everything else stops execution.

Actually no, I think it’s useful to continue even with other status codes.

Example:

  • You want to check that your site/api is up with Gladys. You create a scene that runs every minute and makes a GET request to your site. You do a « continue only if status != 200 » and you send a message « the site is down, status = {{status}} »

  • You want to control a device and send a message if everything went well

  • etc..

I think that in many cases, the scene should continue even with « error » codes, precisely so that the user can use this information in the scene.

For « internet is down », this seems to be a special case, and there we can indeed stop the scene.

Otherwise, we could in the box put a parameter "stop the scene if the return code is different from: « choose list of codes », but that seems a bit heavy, I don’t know

Hello @lmilcent!
I don’t really understand your example of using an API to fetch the weather when we already have a service that does that.
Why not use this service for the scenes instead of making an API call? (though we need to have the action?)
Otherwise, what you propose @pierre-gilles is clean, but we could indeed let the user choose to stop the scene based on x or y reason but we already have that with continue only if (right?).
After that, in case the response is different, why not add an option to stop it based on the user’s choice?

As far as I know, this is not currently possible.

Yes that’s why I was wondering at the end :smiley:.

In my opinion, to meet this weather need, we just need to develop the missing bricks linked to the meteorological service, today OpenWeather but also tomorrow TotoMeteo :stuck_out_tongue:

Just like the missing bricks in the agenda service (at this point in time, it doesn’t do much except do the same thing as native apps [not as well at the moment because we can’t interact with events])

I am aware that this represents work, I am a dev myself and I don’t have the time (as much time as on V3) to help you ^^.

I’m just sharing ideas, or my thoughts on certain needs :smiley:

Apart from that, I’m keeping an eye on the progress of Gladys (for info, since zibee2mqtt I finally switched to V4 with my V3 still running to compensate for the gap, but that’s another subject :stuck_out_tongue:)

I am closing this topic since the feature has been developed