Automation step to turn off a device triggers a turn on before truning off

Hi,
I have a simple setup with an Insteon Keypad 6 buttons and an Insteon dual On/Off Outlet. I use the Insteon Integration with a Hub V2. Button A controls the top outlet and button B controls the bottom outlet. The Main button controls both outlets. I manually linked the buttons with the outlets. When I use the physical buttons, everything works as it should.

HA Insteon Integration replicates the status of the buttons and outlets when physically controlled, but to control them via a dashboard, I found that I needed to replicate the logic in automation.

For example, I have this automation when a button is turned off:

alias: Lights Off
description: ""
triggers:
  - type: turned_off
    device_id: Button A
    entity_id: Button A
    domain: switch
    trigger: device
conditions:
  - condition: state
    entity_id: input_boolean.lights_off
    state:
      - "off"
actions:
  - action: input_boolean.turn_on
    metadata: {}
    target:
      entity_id: input_boolean.lights_off
    data: {}
  - type: turn_off
    device_id: Top Outlet
    entity_id: Top Outlet
    domain: switch
  - type: turn_off
    device_id: Button A
    entity_id: Button A
    domain: switch
  - action: input_boolean.turn_off
    metadata: {}
    target:
      entity_id: input_boolean.lights_off
    data: {}
mode: single

Basically, when Button A is turned off (either physically or via dashboard), the corresponding outlet is turned off and Button A is also turned off (to synchronize the physical button light with it’s HA status). The input_boolean is used to make sure the automation is not fired again when turning off Button A.

Sorry for the long introduction. Now the problem.
I notice that sometimes the automation triggers a turn on while a turn off is being requested. Here is the trace:

Triggered by the state of switch.keypad_with_dimmer_58_91_51_button_a at January 21, 2026 at 15:17:29

Test If Lights Off is Off

Input boolean 'Turn on'

(input_boolean.lights_off) turned on

Turn off On/Off Outlet 58.C5.7C Top

Turn off Keypad with Dimmer 58.91.51 Button A

1 second later

(switch.keypad_with_dimmer_58_91_51_button_a) turned on

1 second later

(switch.keypad_with_dimmer_58_91_51_button_a) turned off

Input boolean 'Turn off'

(input_boolean.lights_off) turned off

Finished at January 21, 2026 at 15:17:32 (runtime: 3.10 seconds)

If I check the related activities, I get:

Keypad with Dimmer 58.91.51 Button A turned on triggered by automation Lights Off triggered by state of Keypad with Dimmer 58.91.51 Button A

15:17:31 - 1 hour ago

Keypad with Dimmer 58.91.51 Button A turned off triggered by automation Lights Off triggered by state of Keypad with Dimmer 58.91.51 Button A

15:17:32 - 1 hour ago

The automation seems to have triggered a turn on of the device even if it wasn’t listed in the actions.

Why?