Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Create an entity in Home Assistant with script output json #416

Answered by craigbarratt
tejasitraj asked this question in Q&A
Discussion options

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)
You must be logged in to vote

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

Comment options

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():
 ...
You must be logged in to vote
1 reply
Comment options

wow, you can make a time_trigger AND a service, triggerable by time and manually by a service call?! awesome!

Answer selected by tejasitraj
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /