How to parse and display json array from command_line result?

Hello,
I created a python script that return the next flight departures and arrivals for the upcoming x hours.
I would like to display this output in HA now instead of running the script manually each time. In the end I would like to have a list with 1 line per flight in the UI.

I am a bit loss on how to integrate this into HA though.

The different options look like:

  • HA Python scripts integration: this does not support python imports so I can’t use it
  • command_line integration: this may work but I don’t see the script output anywhere, nor do I know if it is possible to interpret a json array
  • pyscript or appdaemon: those 2 looks like they are designed to interact directly with HA components. Not sure if it is what I need.

What is the best way to proceed from here ?

Thanks for your suggestions :wink:

Here is its current output (I can adapt this if required) :

[
  [
    {
      "date": "06/02/2025",
      "arrival_time": "16:24",
      "from": "Munich",
      "airline": "Lufthansa",
      "flight_number": "LH2398",
      "status": "Landed 16:18",
      "note": "",
      "direction": "A",
      "aircraft": "Bombardier CRJ-900",
      "time_left": "2min"
    },
    {
      "date": "06/02/2025",
      "arrival_time": "16:25",
      "from": "Rabat Sale",
      "airline": "Netjets",
      "flight_number": "NJE801",
      "status": "",
      "note": "Cargo",
      "direction": "A",
      "aircraft": "Bombardier Global Express (C)",
      "time_left": "3min"
    },
    {
      "date": "06/02/2025",
      "arrival_time": "16:35",
      "from": "Istanbul Sabiha Gökcen",
      "airline": "Pegasus Airlines",
      "flight_number": "PC942",
      "status": "Boarding closed",
      "note": "",
      "direction": "D",
      "aircraft": "Airbus A321neo",
      "time_left": "13min"
    }
  ]
]

Would the Jinja from_json filter be useful? I believe it will give you a Jinja object like a dictionary or a list which you can then further format before using within home assistant.

This will probably work to interpret the json output.

I am however still blocked before that as I don’t manage to retrieve my script output.
I develop the script in another VM and wanted to implement it within HA VM.

However, I am pondering if running this script in the same VM as HA is a good idea, or if it is not best to run it into another one and feed a MQTT sensor directly. That might be easier.

Currently my config looks like this:

sensor:
  - platform: command_line
    name: Upcoming Flights
    command: "python3 /config/python_scripts/update_flights.py"
    scan_interval: 900 # Run every 15 minutes
    value_template: "{{ value_json }}"  # Adjust based on the response

Not sure about your objectives, so it’s hard for me to say, but I don’t see a lot of downside to running a simple Python script in the same VM instance. The reduction in complexity might be worth any trade-off.

One more thing. If what you’re saying is that you’re not getting the output of your update_flights.py script at all: Are you sure that your script is actually in /config/python_scripts? That is, you have a directory called config at the root of your filesystem?

Are you sure that your homeassistant user (usually ‘homeassistant’) has permissions to access the script?

Are you seeing anything in the logs?

UPDATED: I originally wrote “permissions to execute the script” but I think in this case what’s needed is permissions to read, since you’re passing the script location as an argument to the python executable.

My objective is to get the next departures and arrivals each 15min and display them nicely into HA. That way I can plan my walk :grin:
My current terminal output looks like this:

I did installed the script in the config/python_scripts folder. Not sure if it is that important given I am not using the HA python integration (as it does not support python imports).

I then installed the advanced terminal to check if the script is running properly directly in the terminal, but I noticed some issue with urllib3 / name resolution.
Strangely, I can ping / curl the same url without any issue from the terminal but not within the script or python interpreter.
I am getting the following error:
urllib3.exceptions.NameResolutionError

The same script is working fine on the other VM.

Where is the “config/python_scripts” folder, though? Your YAML has a / at the start of the path, which makes it an absolute path. What happens when you run the command

$ ls -lad /config/python_scripts

If the config/python scripts is really in your homeassistant user directory, try removing the leading /

The config/python_scripts is indeed in my /homeassistant folder.
My initial command_line would not have run indeed.

However, I have issues when running the script directly from the terminal too:
I added the ls -lad command at the end.

It is pointing towards a DNS issue, but it only happens in my HA VM.
Here is the script in question: update_flights.py

My other approach would be to simply use MQTT (with MQTT discovery) to feed a custom sensor. I have already done it for custom lights integration and it works fine while leaving the HA VM untouched.

Oh, yikes. I’m sure there are plenty of people who could help you troubleshoot the seemingly DNS-related Python vomit, but I am unfortunately not a good candidate for that.

I think MQTT could work for this (and you’d still be working with JSON); the only thing I’d look into, and it’s mostly a gut instinct not rooted in any particular expertise with MQTT, is how to structure the payloads so that they aren’t too large.

I switched to the MQTT publishing.
It all works well, but I am limited to 256 chars which is only enough for 3-4 flights.
Is there a way to bypass this ?

I am currently using a “sensor” with my values written into “state” attribute.
Is there another component that allow bigger payload ?

An entity state can never exceed 255 characters. An entity attribute however can be much longer, and is not restricted to strings but can be any basic data type (bool, number, list, dict, etc.).

Thanks for your answer !
I noticed this as well going once again through the docs and switch to an attribute which is working now as intended.
I can finally enjoy my flights in HA :smiley:

I am just missing some styling ^^ :