Automation suddenly stopped working partially

I am running HA core 2021.6.3. One of my automation has suddenly stopped working partially. It was working fine till few days ago. The only change I have done is upgrade core to 2021.6.x versions as they came out, but I am not sure which one broke the automation. The automation is simple. When an Insteon scene turns ON, three non-insteon lights are turned ON, when the scene turns OFF, the same three lights are turned OFF.

Problem is that when Insteon scene turns ON, the lights turn ON, but when the scene turns OFF, the lights do not turn OFF. Here is what I have observed so far:

a) When I turn the scene ON/OFF, the automation’s last triggered time changes. So, it does get called when the scene turns ON/OFF.
b) Insteon scenes are treated as switches. So, the switch.basement is an Insteon scene. In Developer Tools, when I check the state of switch.basement, it toggles ON/OFF as I turn the scene ON/OFF.
c) After the automation fails to turn OFF the lights, if I click on Run Actions, the lights turn OFF. This means that the state OFF is being detected by the automation.

I have deleted and re-created the automation but no luck. Any ideas/help will be appreciated.

Here is the automation code:

- id: '1614879408315'
  alias: Basement All Lights ON OFF
  description: ''
  trigger:
  - platform: state
    entity_id: switch.basement
  condition: []
  action:
  - choose:
    - conditions:
      - condition: state
        entity_id: switch.basement
        state: 'on'
      sequence:
      - type: turn_on
        device_id: 82c29fde89af456b885c6929e0990a77
        entity_id: switch.01613010dc4f226f27ec
        domain: switch
      - type: turn_on
        device_id: aae465ab234188329320529e1778d31b
        entity_id: switch.85667640c4dd57254e92
        domain: switch
      - type: turn_on
        device_id: e989f5090cb211eb966e81737437dd02
        entity_id: switch.basement_tubelight_2
        domain: switch
    - conditions:
      - condition: state
        entity_id: switch.basement
        state: 'off'
      sequence:
      - type: turn_off
        device_id: 82c29fde89af456b885c6929e0990a77
        entity_id: switch.01613010dc4f226f27ec
        domain: switch
      - type: turn_off
        device_id: aae465ab234188329320529e1778d31b
        entity_id: switch.85667640c4dd57254e92
        domain: switch
      - type: turn_off
        device_id: e989f5090cb211eb966e81737437dd02
        entity_id: switch.basement_tubelight_2
        domain: switch
    default: []
  mode: single

look ok so
Did you look in the new trace and trace it

Yes and everything seems to be working, except that the actions in the option 2 when state is OFF do not run. Here is Changed Variables section from trace. The first trigger is ON, the second trigger is OFF.

trigger:
  platform: state
  entity_id: switch.basement
  from_state:
    entity_id: switch.basement
    state: 'on'
    attributes:
      group_all_on: 'off'
      friendly_name: Basement Ceiling Lights
      icon: mdi:google-circles-communities
    last_changed: '2021-06-09T14:11:19.240054+00:00'
    last_updated: '2021-06-09T14:11:19.240054+00:00'
    context:
      id: 31eeaf512b14893ea3eb9dcc36a087e6
      parent_id: null
      user_id: null
  to_state:
    entity_id: switch.basement
    state: 'on'
    attributes:
      group_all_on: 'on'
      friendly_name: Basement Ceiling Lights
      icon: mdi:google-circles-communities
    last_changed: '2021-06-09T14:11:19.240054+00:00'
    last_updated: '2021-06-09T14:11:19.257877+00:00'
    context:
      id: 24fb0e8cc1a4f54cf933096b9f787303
      parent_id: null
      user_id: null
  for: null
  attribute: null
  description: state of switch.basement
  id: '0'



trigger:
  platform: state
  entity_id: switch.basement
  from_state:
    entity_id: switch.basement
    state: 'on'
    attributes:
      group_all_on: 'off'
      friendly_name: Basement Ceiling Lights
      icon: mdi:google-circles-communities
    last_changed: '2021-06-09T14:11:19.240054+00:00'
    last_updated: '2021-06-09T14:14:54.052334+00:00'
    context:
      id: afa8442971e8990f85d460e2e399bf9a
      parent_id: null
      user_id: null
  to_state:
    entity_id: switch.basement
    state: 'off'
    attributes:
      group_all_on: 'off'
      friendly_name: Basement Ceiling Lights
      icon: mdi:google-circles-communities
    last_changed: '2021-06-09T14:14:54.069815+00:00'
    last_updated: '2021-06-09T14:14:54.069815+00:00'
    context:
      id: e632f4923d7d4debe1aafd628c762318
      parent_id: null
      user_id: null
  for: null
  attribute: null
  description: state of switch.basement
  id: '0'

Digging some more into trace, I found this discrepancy. When automation turns the lights ON, the trace timeline looks like:

When automation turns the lights OFF, the trace timeline looks like:

So, it looks like the automation does not enter the action sequence when state changes to OFF. Am I reading it right?

Thanks for your help and time.

This should get the job done with less code:

- id: '1614879408315'
  alias: Basement All Lights ON OFF
  description: ''
  trigger:
  - platform: state
    entity_id: switch.basement
  condition: []
  action:
  - service: 'switch.turn_{{ trigger.to_state.state }}'
    target:
      entity_id:
      - switch.01613010dc4f226f27ec
      - switch.85667640c4dd57254e92
      - switch.basement_tubelight_2
  mode: single

Thanks…!!! I tried this code but same issue. Lights turn ON but do not turn OFF.

Then the problem isn’t the automation. You have just tried two very different automations and both produce the same result. You need to investigate the integration responsible for controlling the lights.

Thanks. Will check the integration and update this thread with findings.