Using a list in my automation

I created a variable using the custom variable integration and I’m using it as a list. This list includes a date, flight_number, and city for each item, and I use it to keep track of all the flights I want to track in the future.

I created an automation that runs every midnight to check if there are any flights on the list that need to be tracked and, if so, it will add it to the list of flights actively tracked by the flightradar24 integration.

Everything is working fine, as long as I have a single flight to add to the tracker that day. If I have more than one I don’t know how to access it. Here’s the code that I’m using in the automation:

alias: Add / Remove Flight to Tracker
description: ""
triggers:
  - trigger: time
    at: "00:01:00"
conditions: []
actions:
  - repeat:
      count: >-
        {{ states("var.flight_list") | from_json | selectattr("date", "equalto",
        now().strftime("%Y-%m-%d")) | list | length }}
      sequence:
        - variables:
            flight: >-
              {{ states("var.flight_list") | from_json | selectattr("date",
              "equalto", now().strftime("%Y-%m-%d")) | list | first }}
        - action: notify.mobile_app_dx3h98p60dxp
          metadata: {}
          data:
            message: "Flight Number: {{ flight.flight_number }}"
            title: Flight Tracked
mode: single

I understand that my issues is that I’m using “first” when I set the variable for the iteration, but I don’t know what to change this to to get the current or next item on the list.

Can anyone help me with this automation?

Use a Repeat for Each instead:

alias: Add / Remove Flight to Tracker
description: ""
triggers:
  - trigger: time
    at: "00:01:00"
conditions: []
actions:
  - repeat:
      for_each: >-
        {{ states("var.flight_list") | from_json | selectattr("date", "equalto",
        now().strftime("%Y-%m-%d")) | list }}
      sequence:
        - action: notify.mobile_app_dx3h98p60dxp
          metadata: {}
          data:
            message: "Flight Number: {{ repeat.item.flight_number }}"
            title: Flight Tracked
mode: single

One thing to be aware of, a state can’t hold more than 255 characters. So if the you store the date, flight_number, and city you’re going to run out of space quickly. Attributes can store significantly more and they are not limited to strings so they can be arrays or dictionaries.

1 Like

That worked perfectly, thank you very much!

1 Like