-
Notifications
You must be signed in to change notification settings - Fork 58
Create an entity in Home Assistant with script output json #416
-
Hi All!
I'm super new to python in general, and especially pyscript.
I have written the code below in python to output a json, which shows the aircraft currently tracked by my ADS-B receiver. Now my question is, how do I add this code into pyscript and what adjustments do I need to make, so it creates an entity in Home Assistant with the output? And how do I set the code to run every 5 minutes?
import requests
import json
import time
import datetime
from flightorigindest import *
url = "http://192.168.1.4:8078/data/aircraft.json"
page = requests.get(url)
parsedpage = json.loads(page.text)
data = []
outputjson = {"now":time.mktime(datetime.datetime.now().timetuple()),"aircraft":[]}
for aircraft in parsedpage["aircraft"]:
row_data = {"Registration": "","Type": "","Flight": "","Altitude": "","Airspeed": "","Origin": "","Destination": ""}
try:
row_data["Registration"] = (aircraft["r"])
except KeyError:
row_data["Registration"] = ("-")
try:
row_data["Type"]=(aircraft["t"])
except KeyError:
row_data["Type"]=("-")
try:
row_data["Flight"]=(aircraft["flight"])
except KeyError:
row_data["Flight"]=("-")
try:
row_data["Altitude"]=(aircraft["alt_baro"])
except KeyError:
row_data["Altitude"]=("-")
try:
row_data["Airspeed"]=(aircraft["tas"])
except KeyError:
row_data["Airspeed"]=("-")
try:
row_data["Origin"]=(find_origin_destination(aircraft["r"])[0])
except:
row_data["Origin"]=("-")
try:
row_data["Destination"]=(find_origin_destination(aircraft["r"])[1])
except:
row_data["Destination"]=("-")
data.append(row_data)
outputjson["aircraft"]=(data)
print(str(len(data)) + " Aircraft Seen")
print(outputjson)
Beta Was this translation helpful? Give feedback.
All reactions
It seems you've made progress with the code you posted in #419. Using the code from that post, you have made getAircraft()
a service call. You can also use pyscript
to call getAircraft()
every 5 minutes from midnight using a @time_trigger
decorator, eg:
@time_trigger("period(midnight, 5m)") @service def getAircraft(): ...
Replies: 1 comment 1 reply
-
It seems you've made progress with the code you posted in #419. Using the code from that post, you have made getAircraft()
a service call. You can also use pyscript
to call getAircraft()
every 5 minutes from midnight using a @time_trigger
decorator, eg:
@time_trigger("period(midnight, 5m)") @service def getAircraft(): ...
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 1
-
wow, you can make a time_trigger AND a service, triggerable by time and manually by a service call?! awesome!
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 1