API request via rules

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
velrat
New user
Posts: 3
Joined: 18 Oct 2023, 11:11

API request via rules

#1 Post by velrat » 18 Oct 2023, 12:01

Hello.

I am using ESP8266 with firmware Build: ESP_Easy_mega_20221105_normal_ESP8266_4M1M Nov 5 2022

A relay is connected to GPIO 12, which turns on a red or green light depending on the received RULES command:

Code: Select all

On gled do
  gpio,12,1
endon

On rled do 
  gpio,12,2
endon
The gled or rled command is sent via HTTP by an external script located on the web hosting.

The script every minute makes an API request https://api.alerts.in.ua/v1/alerts/acti ... _APP_TOKEN> and receives a response of the type
{"alerts":[{"id":8757,"location_title":"Луганська область","location_type":"oblast","started_at":"2022-04-04T16:45:39.000Z","finished_at":null,"updated_at":"2022-04-08T08:04:26.316Z","alert_type":"air_raid","location_oblast":"Луганська область","location_uid":"16","notes":null,"calculated":null,"location_oblast_uid":16},{"id":28288,"location_title":"Автономна Республіка Крим","location_type":"oblast","started_at":"2022-12-10T22:22:00.000Z","finished_at":null,"updated_at":"2022-12-12T12:20:11.900Z","alert_type":"air_raid","location_oblast":"Автономна Республіка Крим","location_uid":"29","notes":"Згідно інформації з Офіційних карт тривог","calculated":null,"location_oblast_uid":29},{"id":51842,"location_title":"м. Нікополь","location_type":"city","started_at":"2023-10-18T09:21:02.000Z","finished_at":null,"updated_at":"2023-10-18T09:21:07.261Z","alert_type":"artillery_shelling","location_oblast":"Дніпропетровська область","location_uid":"5351","notes":null,"calculated":null,"location_oblast_uid":9},{"id":51843,"location_title":"Нікопольська територіальна громада","location_type":"hromada","started_at":"2023-10-18T09:21:02.000Z","finished_at":null,"updated_at":"2023-10-18T09:21:07.269Z","alert_type":"artillery_shelling","location_oblast":"Дніпропетровська область","location_uid":"351","notes":null,"calculated":null,"location_raion":"Нікопольський район","location_oblast_uid":9},{"id":51844,"location_title":"Донецька область","location_type":"oblast","started_at":"2023-10-18T09:36:20.000Z","finished_at":null,"updated_at":"2023-10-18T09:36:23.245Z","alert_type":"air_raid","location_oblast":"Донецька область","location_uid":"28","notes":null,"calculated":null,"location_oblast_uid":28},{"id":51845,"location_title":"Дніпропетровська область","location_type":"oblast","started_at":"2023-10-18T09:36:59.000Z","finished_at":null,"updated_at":"2023-10-18T09:37:03.242Z","alert_type":"air_raid","location_oblast":"Дніпропетровська область","location_uid":"9","notes":null,"calculated":null,"location_oblast_uid":9},{"id":51846,"location_title":"м. Дніпро","location_type":"city","started_at":"2023-10-18T09:37:10.000Z","finished_at":null,"updated_at":"2023-10-18T09:37:13.225Z","alert_type":"air_raid","location_oblast":"Дніпропетровська область","location_uid":"5332","notes":null,"calculated":null,"location_oblast_uid":9},{"id":51847,"location_title":"Дніпровська територіальна громада","location_type":"hromada","started_at":"2023-10-18T09:37:10.000Z","finished_at":null,"updated_at":"2023-10-18T09:37:13.234Z","alert_type":"air_raid","location_oblast":"Дніпропетровська область","location_uid":"332","notes":null,"calculated":null,"location_raion":"Дніпровський район","location_oblast_uid":9},{"id":51848,"location_title":"Полтавська область","location_type":"oblast","started_at":"2023-10-18T09:37:23.000Z","finished_at":null,"updated_at":"2023-10-18T09:37:27.546Z","alert_type":"air_raid","location_oblast":"Полтавська область","location_uid":"19","notes":null,"calculated":null,"location_oblast_uid":19},{"id":51849,"location_title":"Харківська область","location_type":"oblast","started_at":"2023-10-18T09:37:24.000Z","finished_at":null,"updated_at":"2023-10-18T09:37:27.558Z","alert_type":"air_raid","location_oblast":"Харківська область","location_uid":"22","notes":null,"calculated":null,"location_oblast_uid":22},{"id":51850,"location_title":"Запорізька область","location_type":"oblast","started_at":"2023-10-18T09:37:30.000Z","finished_at":null,"updated_at":"2023-10-18T09:37:33.238Z","alert_type":"air_raid","location_oblast":"Запорізька область","location_uid":"12","notes":null,"calculated":null,"location_oblast_uid":12},{"id":51851,"location_title":"м. Кривий Ріг","location_type":"city","started_at":"2023-10-18T09:37:35.000Z","finished_at":null,"updated_at":"2023-10-18T09:37:36.127Z","alert_type":"air_raid","location_oblast":"Дніпропетровська область","location_uid":"5279","notes":null,"calculated":null,"location_oblast_uid":9},{"id":51852,"location_title":"Криворізька територіальна громада","location_type":"hromada","started_at":"2023-10-18T09:37:35.000Z","finished_at":null,"updated_at":"2023-10-18T09:37:36.133Z","alert_type":"air_raid","location_oblast":"Дніпропетровська область","location_uid":"279","notes":null,"calculated":null,"location_raion":"Криворізький район","location_oblast_uid":9},{"id":51853,"location_title":"м. Світловодськ","location_type":"city","started_at":"2023-10-18T09:38:00.000Z","finished_at":null,"updated_at":"2023-10-18T09:38:03.305Z","alert_type":"air_raid","location_oblast":"Кіровоградська область","location_uid":"5786","notes":null,"calculated":null,"location_oblast_uid":15},{"id":51854,"location_title":"Світловодська територіальна громада","location_type":"hromada","started_at":"2023-10-18T09:38:00.000Z","finished_at":null,"updated_at":"2023-10-18T09:38:03.310Z","alert_type":"air_raid","location_oblast":"Кіровоградська область","location_uid":"786","notes":null,"calculated":null,"location_raion":"Олександрійський район","location_oblast_uid":15},{"id":51855,"location_title":"Сумська область","location_type":"oblast","started_at":"2023-10-18T09:39:02.000Z","finished_at":null,"updated_at":"2023-10-18T09:39:03.323Z","alert_type":"air_raid","location_oblast":"Сумська область","location_uid":"20","notes":null,"calculated":null,"location_oblast_uid":20}],"meta":{"last_updated_at":"2023/10/18 09:52:24 +0000","type":"full"},"disclaimer":"If you use python try our official alerts_in_ua PiP package."}
If the answer contains: "location_oblast":"м. Київ" - the rled command is sent, respectively, if it does not contain - gled.

Perhaps someone knows how to make and analyze an API request directly through RULES, and send the appropriate command to the relay? Couldn't find any documentation on how I can make an API request via Rules.

User avatar
Ath
Normal user
Posts: 3520
Joined: 10 Jun 2018, 12:06
Location: NL

Re: API request via rules

#2 Post by Ath » 18 Oct 2023, 12:57

Retrieving results from a HTTP service is currently not implemented.

If the service that sends the rled/gled events to ESPEasy could append a parameter to that event, like event,gled=<content_of_location_oblast_argument> (that content should not contain spaces or commas, best to replace that by underscores) then you could use an internal 'redirect' like:

Code: Select all

on gled* do // The asterisk is needed to have a non-numeric first argument passed through
  asyncevent,location#%eventvalue1%
  gpio,12,1
endon

// Location handlers:
on location#м._Київ do // Handle the specific case for location м. Київ
  // Do what needs to be done for this location
endon
on location#Дніпропетровська_область do // Handle location Дніпропетровська область
  // Something else
endon
And of course, the service could also send this: "event,location#<location_name_without_spaces_or_commas>"


If, as an alternative, you could send the request(s) via an MQTT server, then the MQTT Import plugin could help you translate that value to a numeric value, or a specific event.

NB: Sending large string arguments to an ESP (Especially the ESP8266) may cause issues because of the limited memory available for processing that
/Ton (PayPal.me)

velrat
New user
Posts: 3
Joined: 18 Oct 2023, 11:11

Re: API request via rules

#3 Post by velrat » 18 Oct 2023, 13:15

Thank you very much for your reply.

The changes you mentioned are happening, but it doesn't make sense.

The goal was to achieve independent operation of the ESP8266, without receiving commands from the outside.

Instead of turning on a green or red light, the board should open the circuit of the electromagnetic lock of the bomb shelter, when the air alarm (missile danger) is signaled, or close it after the alarm is over.

The alarm status can be checked by an API request.

I would like to implement an autonomous unit, without additional servers, as it is done now.

Perhaps you could recommend other hardware instead of the ESP8266 that would help solve this problem?

TD-er
Core team member
Posts: 8756
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: API request via rules

#4 Post by TD-er » 18 Oct 2023, 13:34

Is it possible to get a HTTP return code based on what you need?
Like putting a query in your GET url and based on the result you either get a 200 or some other value.
Those will generate an event with the HTTP return value.

velrat
New user
Posts: 3
Joined: 18 Oct 2023, 11:11

Re: API request via rules

#5 Post by velrat » 18 Oct 2023, 13:50

Without additional processing on your own server - no.

When I make an API request, I get a response in the same form as in the first message.

As an alternative, another request - https://api.alerts.in.ua/v1/iot/active_ ... _uid>.json but one of the values is still sent in response:

"A" - air alert is active in the entire area
"P" - partial alarm in districts or communities
"N" - no information about the air alarm

That is, the API response in this case is simple:
"A"

User avatar
Ath
Normal user
Posts: 3520
Joined: 10 Jun 2018, 12:06
Location: NL

Re: API request via rules

#6 Post by Ath » 18 Oct 2023, 22:56

Since the features you desire aren't available from ESPEasy, and also aren't planned for in the short term plans, you could have a look at Micropython, as that is also available for ESP8266 and ESP32. Micropython has a subset of the Python programming language, and is quite versatile in handling internet stuff in general, and Json data in particular.
Available for ESP8266 and ESP32
/Ton (PayPal.me)

Post Reply

Who is online

Users browsing this forum: No registered users and 40 guests