Loop an automation

Hello,

how can I do to trigger a loop for an automation response? I want to repeat endlessly but remotely from 1 minute until another action occurs. I also want to have the option to interrupt it.

1 Like

Time_pattern trigger the automation every minute with a condition to prevent the actions when required.

Thank you for the information.
Can you give me an example please? I do not know much about automation. In practice, I would like every time a magnetic contact goes in alarm my googlemini will give an alarm notification until I stop it.
The googlemini notification part is already functional. I would just like to integrate the automation part that makes googlemini’s message “loop” until it is interrupted.

Combine a time pattern with another trigger, for example a sensor/input_boolean that goes on whenever you need.

See: https://www.home-assistant.io/docs/automation/trigger/

Sidenote: If you’re beginning with automations, I’d recommend using nodered. It’s, in my opinion, way more intuitive than yaml and you can iterate whithout having to restart HA everytime.

One way to loop is to have two scripts. Script1 does the work then calls Script2. Script 2 simply calls Script1.

Judicious use of conditions, delay and wait_templates can usually provide the control you need.

I’m in the process of experimenting with two scripts passing an integer back and forth which increments so providing a rudimentary For Loop

Hello, excuse me and thank you for your answers but I unfortunately do not understand what to do. I place my code with which I can receive the message from minigoogle but I would like to say it until the sensor returns to the rest condition. can you give me a hand? thank you all.

alias: Cancello lasciato aperto test
trigger:

  • entity_id: binary_sensor.door_window_sensor_158d000273586e
    for: 00:00:10
    from: ‘off’
    platform: state
    to: ‘on’

- platform: time_pattern

minutes: 1

condition: []
action:
- service: tts.google_say
entity_id: media_player.casa_xx
data_template:
message: >-
{{ [
"Cancello aperto da almeno cinque minuti " ,
] |random }}

Hi klogg
I am just wondering if you managed to get anywhere with your two scripts passing an integer between scripts. Any info you can provide would be appreciated.

Cheers

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 }}

This is great. Thanks for taking the time to reply to me dude!

Cheers :slight_smile:

Craig

1 Like