Automation trick help

Hi, I have following automation that monitor event and trigger every minute to set, value of an input text.
The input text is used to modify caracteristics of a button (style, icon, etc.).
Every thing it’s working great, but I noticed that in every check, done every minute, state of “input_text.stato_scanning_bt_proxy_1” is reset to “spento” for a while, then set to “acceso” almost immediately (just because is “acceso”).
That’s causing that for a while my button style, icon and so on changing and come back to “real status” should be.
Any idea to fix it?

AUTOMATION:

> alias: BT Scanning Status - esp32-bluetooth-proxy-1
> description: ""
> trigger:
>   - platform: event
>     event_type: esphome.ble_scanning_state
>   - platform: time_pattern
>     minutes: /1
> action:
>   - data_template:
>       entity_id: input_text.stato_scanning_bt_proxy_1
>       value: >
>         {% if trigger.event is defined and trigger.event.data.status == 'acceso'
>         %}
>           acceso
>         {% else %}
>           spento
>         {% endif %}
>     action: input_text.set_value

BUTTON:

type: custom:button-card
show_entity_picture: true
entity: input_text.stato_scanning_bt_proxy_1
icon: mdi:bluetooth
show_state: false
show_label: true
show_name: true
show_icon: true
tap_action:
  confirmation:
    text: Confermi il cambio di stato ?
  action: call-service
  service: |
    [[[
      if (states['input_text.stato_scanning_bt_proxy_1'].state === 'acceso')
        return 'button.press';
      else
        return 'button.press';
    ]]]
  service_data:
    entity_id: |
      [[[
        if (states['input_text.stato_scanning_bt_proxy_1'].state === 'spento')
          return 'button.esp32_bluetooth_proxy_1_start_scan_esp32_bt_proxy_1';
        else
          return 'button.esp32_bluetooth_proxy_1_stop_scan_esp32_bt_proxy_1';
      ]]]
size: 40%
name: Stato Tracking BT
label: |
  [[[
    if (states['input_text.stato_scanning_bt_proxy_1'].state === "acceso")
      return "ACCESO";
    else if (states['input_text.stato_scanning_bt_proxy_1'].state === "spento")
      return "SPENTO";
  ]]]
styles:
  card:
    - padding: 5px 15px
    - background: linear-gradient(rgba(255,255,255,0.1) 25%, rgba(0,0,20,0.3)50%)
    - border-radius: 25px
    - border: |
        [[[
          if (states['light.esp32_bluetooth_proxy_1_led_gpio2'].state === "on") return '3px solid blue';
          else return '3px solid red';
        ]]]
    - color: ivory
  name:
    - text-transform: null
    - color: white
    - font-weight: bold
    - font-size: 80%
    - padding: 15px 0px 0px 0px
  label:
    - color: white
    - justify-self: middle
    - font-weight: bold
    - padding: 5px 0px 0px 0px
state:
  - value: spento
    icon: mdi:bluetooth-off
    color: red
  - value: acceso
    icon: mdi:bluetooth
    color: blue

Why is that necessary?

Isn’t the Event Trigger sufficient?

It is because every time_pattern is executed state of input_text.stato_scanning_bt_proxy_1 is set to “spento”.
This cause button to goes to state “spento” for a moment then comes back to “acceso” one.

How frequently does the Event Trigger recur? You should be able to determine that from the timing of the automation’s traces.

My impression is in recurs at almost the same frequency as the Time Pattern Trigger (i.e. about every 60 seconds).

Unfortunately no, it is not like that. The frequency of the event is much higher. It happens every second.

That easily explains this behavior:

reset to “spento” for a while, then set to “acceso” almost immediately

Found following solution:

AUTOMATION

alias: BT Scanning Status - esp32-bluetooth-proxy-1
description: ""
trigger:
  - platform: event
    event_type: esphome.ble_scanning_state
  - platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.bt_scanning_timer
condition: []
action:
  - choose:
      - conditions:
          - condition: template
            value_template: >-
              {{ trigger.event.event_type == 'esphome.ble_scanning_state' and
              trigger.event.data.status == 'acceso' }}
        sequence:
          - data:
              entity_id: input_text.stato_scanning_bt_proxy_1
              value: acceso
            action: input_text.set_value
          - data:
              entity_id: timer.bt_scanning_timer
            action: timer.start
      - conditions:
          - condition: template
            value_template: "{{ trigger.event.event_type == 'timer.finished' }}"
        sequence:
          - data:
              entity_id: input_text.stato_scanning_bt_proxy_1
              value: spento
            action: input_text.set_value
    default: []

TIMER

timer:
  bt_scanning_timer:
    duration: "00:00:05"