Automation 1 or 2 generates an infinite loop

Hello. After struggling a bit with the code, I stopped again with something that I don’t know how to do.

Automation 1 runs when someone changes the source from the audio equipment
Automation 2 runs when someone changes the input_select source from within the application.

The problem is that when I change the source of the equipment, automation 2 is triggered, which in turn executes automation 1 again creating a loop that ends up hanging the equipment
Can you make when one automation runs, the other is disabled?

Thanks
automation 1

  - alias: bdv_n9200w_input_select
    description: "Set input select to value of media title whenever media content id changes"
    trigger:
      - platform: state
        entity_id: media_player.bdv_n9200w
        attribute: media_content_id
    condition: []
    action:
      - service: input_select.select_option
        data:   
          option: "{{ state_attr('media_player.bdv_n9200w','media_content_id')[7:] }}"
        target:
          entity_id: input_select.home 

automation 2

  - alias: bdv_n9200w_change_source
    description: "Cambia media con Broadlink cuando se cambia input_select.home en  home assistant"
    trigger:
      - platform: state
        entity_id: input_select.home
    condition: []
    variables:
      repeats: >
          {% set sources = ["Bluetooth","Audio","DB DVD","USB","FM Present","HDMI 1","HDMI 2","TV","SCR M"] %}
          {% set old_pos = sources.index(trigger.from_state.state) | int(0) %}
          {% set new_pos = sources.index(trigger.to_state.state) | int(0) %}
          {% if new_pos > old_pos %}
            {{ new_pos - old_pos }}
          {% else %}
            {{ (sources|length - old_pos) + new_pos }}
          {% endif %}
    action:
      repeat:
        count: "{{repeats}}"
        sequence:
        - service: script.bdv_n9200w_source

script

  bdv_n9200w_source:
    sequence:
    - service: remote.send_command
      target:
        entity_id: remote.broadlink_m3_remote_living
      data:
        command: 'b64:JgDuAFESKRIVExQTKBMUEygTKBMUExUSFRIVEygSFRMoEygAApxREygSFRMTFCgTFBMnFCgTExQUExQTFBQnExQUJxQnAAKdUBQnExQUExQnFBMUJxQnFBQTFBMUExQUJxQTFCcUJwACpk8UJxQUExQTKBMUEygTKBMUExQUExQTFCcUFBMoEygAAqVQEygTFBMUFCcUExQnFCcUExQUExQTFBMoExQUJxQnAAKlUBQnFBMUExQnFBQTKBMoExQTFBMUFBMUJxQTFCcUJwACpk8UKBMUExQTKBMUFCcUJxMUFBMUFBMUEygTFBMoEygADQUAAAAAAAAAAAAA'
    - delay: '00:00:01'  

Can’t you disable the second automation in the first automations actions and vice versa?

I don’t know how to do this. The problem is that if someone changes the source of the equipment, one automation is activated and the other is also activated and thus it can continue all day. I can’t think of how you can do that. That if one automation started, the other can deactivate it until it is finished.
I hope you understand because I don’t speak English. Thanks

There is a service call called service: automation.turn_off and turn_on.

If one of that actions cause the loop then do:

service: automation.turn_off

The action you want to do

service: automation.turn_on

This is the kind of interaction I was talking about yesterday when I said it might be necessary to add some conditions to the automations. One option is to use a template condition to compare the current time to the time the script or automation was last triggered.

En Espanol

Esto es lo que advertí ayer cuando dije que quizas seria necessario añadir “condiciones” a la automatización para evitar interacciones. Una opción es comparar el atributo “last_triggered” de la automatización y el script con la hora actual. La condición "template’ requiere que la automatización o script no se haya activado en los últimos 3 segundos. Es posible que necesitaras probar algunos diferentes períodos de tiempo para que todo funcione correctamente.


 - alias: bdv_n9200w_input_select
    description: "Set input select to value of media title whenever media content id changes"
    trigger:
      - platform: state
        entity_id: media_player.bdv_n9200w
        attribute: media_content_id
    condition:
      - "{{ now() - state_attr('script.bdv_n9200w_source', 'last_triggered')  >= timedelta(seconds=3) }}"
    action:
      - service: input_select.select_option
        data:   
          option: "{{ state_attr('media_player.bdv_n9200w','media_content_id')[7:] }}"
        target:
          entity_id: input_select.home 

- alias: bdv_n9200w_change_source
    description: "Cambia media con Broadlink cuando se cambia input_select.home en  home assistant"
    trigger:
      - platform: state
        entity_id: input_select.home
    condition:
      - "{{ now() - state_attr('automation.bdv_n9200w_input_select', 'last_triggered')  >= timedelta(seconds=3) }}"
    variables:
      repeats: >
          {% set sources = ["Bluetooth","Audio","DB DVD","USB","FM Present","HDMI 1","HDMI 2","TV","SCR M"] %}
          {% set old_pos = sources.index(trigger.from_state.state) | int(0) %}
          {% set new_pos = sources.index(trigger.to_state.state) | int(0) %}
          {% if new_pos > old_pos %}
            {{ new_pos - old_pos }}
          {% else %}
            {{ (sources|length - old_pos) + new_pos }}
          {% endif %}
    action:
      repeat:
        count: "{{repeats}}"
        sequence:
        - service: script.bdv_n9200w_source

The amount of new things he was unaware of is impressive. I am amazed at how powerful this system is and how much we can do to create these automations.
Thank you very much again. I’m going to try it but it’s clear in concept