For some silly reason, i used http requests and wrote a simple endpoint to save events to a database.
I dont know why i did that, and in hindsight it seems silly.
If i were to re-tackle this (and i might one day) i would build some node red flows that do something along the lines of;
watch for opensky events
use a template node to get the attributes of the event
if its an in event,
write the time_in, and the callsign to the database
if its an out event,
update the record for the callsign, and write the time_out
For right now i have some scripts for event in, and event out, and those scripts call my http endpoint.
i then use an sql sensor to get the data into HA.
Here is what I am doing now:
scripts:
add_opensky_in:
sequence:
- service: rest_command.opensky_list
data_template:
payload: "{\n \"callsign\": \"{{ callsign }}\" \n}\n"
add_opensky_out:
sequence:
- service: rest_command.opensky_list_out
data_template:
payload: "{\n \"callsign\": \"{{ callsign }}\" \n}\n"
automation
- alias: Flight API
trigger:
platform: event
event_type: opensky_entry
action:
- data_template:
callsign: '{{ trigger.event.data.callsign }}'
service: script.add_opensky_in
id: 59db686497064a55be06b354abf8faa6
- alias: Flight API Out
trigger:
platform: event
event_type: opensky_exit
action:
- data_template:
callsign: '{{ trigger.event.data.callsign }}'
service: script.add_opensky_out
id: 6194bf1566ba4f06b4a00f1be3d003e2
the rest commands:
opensky_list:
url: http://xxxxx?status=in
method: post
timeout: 20
payload: '{{ payload }}'
opensky_list_out:
url: http://xxxxx?status=out
method: post
timeout: 20
payload: '{{ payload }}'
SQL sensor:
queries:
- name: "Current Flights"
query: "SELECT IFNULL(GROUP_CONCAT(callsign SEPARATOR \" \"), \"None\") AS current_flights FROM events WHERE when_out IS NULL"
column: current_flights
Lovelace card:
type: markdown
content: |-
Flights: {% for flight in state_attr("sensor.opensky_list", "flights") %}
* {{flight.callsign}} - {{flight.when}}
{%- endfor %}
title: Opensky Flights
The result:
As i mentioned, i would replace the automation, script, and rest commands with a node red flow, and directly save the events to mysql.