Value Template Sensor with Attributes generated from array

Hello,

I’ve been spending some time trying to extract data from the FlightRadar24 sensor, filter it, and put it out as a different sensor with attributes. The goal is to use the filtered data in a script to draw a screen on a Tidbyt.

sensor.flightradar24_current_in_area contains an array of multiple aircraft, each array index has multiple elements and I can easily use a value template to extract what I want using if statements in a for loop that reads the array.

Pulling this sensor in my Tidbyt script gives me an array that I am not good at processing, so I want to massage the data in a way that my script can read. I need there to be a sensor with a single set of elements that is updated using a value template applied to the flightradar sensor.

For example, the value template below does what I want. It filters aircraft using some paramters I am interested in, and only returns certain elements of a single index:

{% set ns = namespace(matched = 0) %}
{% set data = state_attr('sensor.flightradar24_current_in_area', 'flights') %} 
{% for flight in data | sort (attribute='altitude') %} 
{% if flight.altitude > 1000 and flight.airline_short and not flight.airport_origin_code_iata == 'MYF' and not flight.airport_destination_code_iata == 'MYF' and not ns.matched == 1 %}
  airline_iata: {{flight.airline_iata}}
  airport_origin_code_iata: {{flight.airport_origin_code_iata}}
  airport_destination_code_iata: "{{flight.airport_destination_code_iata}}
  aircraft_code: {{flight.aircraft_code}}
  flight_number: {{flight.flight_number}} 
  altitude: {{flight.altitude}}
  {%set ns.matched = 1 %}
  {% endif %}
  {% endfor %}

The output in the Developer Tools Template tab gives me a string output:

airline_iata: UA
  airport_origin_code_iata: ORD
  airport_destination_code_iata: "SAN
  aircraft_code: B739
  flight_number: UA1793 
  altitude: 3000

I want to put that data into a new sensor, say, sensor.flights_for_tidbyt but have each element as a separate attribute so I can pull the data from HA in my Tidbyt script.

Maybe I’m trying to approach this the wrong way. The goal is to be able to make a single call to an entity that contains the most up to date filtered data.

I’ve found mentions of code and integrations that handle variables, but none of those have been able to work with setting the variables/attributes from a for loop in a value template. It’s also very possible I’ve been doing this all wrong, but I can’t get the data to look right.

The closest I got was creating a Value Template helper that outputs the string above, but when read by the Tidbyt script it comes through in an unexpected format since they are not attributes. This is when I manually added the quotes to each element, but the string translated it into escaped characters

{"entity_id": "sensor.flights_for_tidbyt", "state": "\"attributes\": {\"airline_iata\": \"AS\", 
\"airport_origin_code_iata\": \"SFO\", \"airport_destination_code_iata\": \"SAN\", \"aircraft_code\": 
\"E75L\", \"flight_number\": \"AS3301\", \"altitude\": \"2050\"}", "attributes": {"icon": "mdi:airplane", 
"friendly_name": "Flights for Tidbyt"}, "last_changed": "2024-02-18T18:33:30.730365+00:00", 
"last_updated": "2024-02-18T18:33:30.730365+00:00", "context": {"id": 
"01HPYQX9V60VR41G3A08DW44RQ", "parent_id": None, "user_id": None}}

I would like it to look something like this:

{"entity_id": "sensor.flights_for_tidbyt", "state": {True}", "attributes": {"airline_iata": "AS", 
"airport_origin_code_iata": "SFO", "airport_destination_code_iata": "SAN", "aircraft_code": "E75L", 
"flight_number": "AS3301", "altitude": "2050", "icon": "mdi:airplane", "friendly_name": "Flights for Tidbyt"},
 "last_changed": "2024-02-18T18:33:30.730365+00:00", "last_updated": 
"2024-02-18T18:33:30.730365+00:00", "context": {"id": "01HPYQX9V60VR41G3A08DW44RQ", 
"parent_id": None, "user_id": None}}

edit: I ended up figuring out a way to do the filtering and sorting on the Tidbyt Starlark code by using some Python code, but I still wonder if there’s an easy way to add attributes to a sensor.

It’s explained in the Template Sensor documentation:

You’ll need to configure it in YAML as the UI doesn’t yet support attributes. I assume that what you’'ve been using from your “Value Template helper” language?

Wild guess at the first attribute, might need some tweaking:

template:
  - sensor:
      - name: Flight data
        state: "{{ now().isoformat() }}"
        attributes: >
          airline_iata: "{{ (state_attr('sensor.flightradar24_current_in_area','flights')|sort(attribute='altitude')|selectattr('altitude','>',1000)|selectattr('airline_short','==',True)|rejectattr('airport_origin_code_iata','==','MYF')|rejectattr('airport_destination_code_iata','==','MYF')|list|first)['airline_iata'] }}"
          etc