Loop an automation

Yes I did. In my garden irrigation package I collect historical temperature and rainfall data over the last five days with the intention of adjusting watering time appropriately.

Taking temperature…

I have five Input Numbers all named input_number.temp_minus0, temp_minus1temp_minus4 to store the data and very day I cycle the data through the Input Numbers, dropping off the oldest.

This is how I do it. I don’t know if it is really necessary in my case but it was done partly as a learning exercise! And also if you decide to inspect the code, it is experimental in terms of the use I trying to achieve. The logic processing seems to work, I am just having some trouble choosing what data to use!

#=================
# === Automations
#=================
automation:

  #================================================
  #=== Collect new and cycle historic weather data
  #================================================
  - alias: Irrigation Weather Data - Collect new and cycle historic weather data
    initial_state: 'on'
    trigger:
      - platform: time
        at: '01:00:00'

    action:
      # These scripts pass the count of entities but in this case as I 
      # have numbered them beginning with zero pass the count minus 1

      # Cycle the temperature figures
      - service: script.garden_temperature_data
        data_template:
          loop_count: >
            {% set ns = namespace(count = 0) %}
            {% for item in states if item.entity_id.startswith('input_number.temp_minus') %}
              {% set ns.count = loop.index %}
            {% endfor %}
            {{ (ns.count - 1) }}
#=============
# === Scripts
#=============
script:

  #====================================================
  #=== Collect new and cycle historic TEMPERATURE data
  #====================================================
  garden_temperature_data:
    sequence:

      # Cycle the temperature figures.
      # Passed {{ loop_count}} which is the number relating to the highest entity
      # e.g. input_number.temp_minus4

      # Setting the temperature for today:
      # DarkSky dark_sky_forecast_temperature_high_0 is used as the high temperature.

      - service: input_number.set_value
        data_template:
          entity_id: >
            input_number.temp_minus{{ loop_count }}
          value: >
            {% if loop_count | int == 0 %}
              {{ states('sensor.dark_sky_forecast_daytime_high_temperature_0') | float }}
            {% else %}
              {{ states('input_number.temp_minus' ~ (loop_count | int - 1) | string) }}
            {% endif %}

      # Stop when count is 0
      - condition: template
        value_template: >
          {{ loop_count | int > 0 }}

      # Stop the looping script...
      - service: homeassistant.turn_off
        entity_id: script.loop_garden_temperature_data

      # ...before looping
      - service: script.loop_garden_temperature_data
        data_template:
          loop_count: >
            {{ loop_count }}


  #============================================
  #=== Loop the script garden_temperature_data
  #============================================
  loop_garden_temperature_data:
    sequence:

      # Stop the calling script...
      - service: homeassistant.turn_off
        entity_id: script.garden_temperature_data

      # ...then restart it
      - service: script.garden_temperature_data
        data_template:
          loop_count: >
            {{ loop_count | int - 1 }}