Help with automation delay with conditions

Background:
Given the binary inputs: 1, 2 and A.
1 and 2 are state indicators, which runs a number of cycles through a loop of;
00 = Off
01 = Starting
10 = On
11 = Stopping

Each time 1 or 2 change, it triggers an automation “Testing Active”. It also starts another automation “Testing Complete” with a timed trigger, so if no change is detected for 15 seconds, it triggers to show the cycles have completed. (Basically a crude watchdog timer)

A is a “fail” flag which may “pulse” high during any 1 and 2 state for a few seconds, to indicate the something has failed.

Question:
I need it so, if A becomes TRUE, a separate automation will set a; separate state (E.G. “Failed”) TRUE or perform an action (E.G. persistent_notification), and retain TRUE regardless of future A states.

The only time the state (E.G. “Failed”) or persistent_notification can be set FALSE / reset, is when, the timed trigger for the automation “Testing Complete” is true (I.E. greater than 15 seconds) AND a state change from 1 or 2 is detected.

Perhaps this is not the best way to achieve this and one top-level state can achieve (E.G. Testing State") could create the same outcome in a better way, I am open to change any of it.


I have managed to find a way to do the “testing active” state, by having it on a delay timer, which is reset by any state changes of 1 and 2. I also have created an interpreted state which shows the meaning of 1 and 2.

Later on I hope the automations will also send out notifications or perform other actions.

Example code, just for keeping track of input 1 and 2:

binary_sensor:
  - platform: rpi_gpio
    pull_mode: UP
    invert_logic: true
    ports:
      14: Chamber Output 1  #INPUT 1
      15: Chamber Output 2  #INPUT 2
      23: Test_Fail         #INPUT A

sensor:
  - platform: template
    sensors:
      chamber_1:
        friendly_name: 'Test Cycle State'
        value_template: >-
          {% if is_state("binary_sensor.chamber_output_1", "off")
          and is_state("binary_sensor.chamber_output_2", "off") %}
            OFF
          {% elif is_state("binary_sensor.chamber_output_1", "off")
          and is_state("binary_sensor.chamber_output_2", "on") %}
            Starting
          {% elif is_state("binary_sensor.chamber_output_1", "off")
          and is_state("binary_sensor.chamber_output_2", "off") %}
            Stopping
          {% elif is_state("binary_sensor.chamber_output_1", "off")
          and is_state("binary_sensor.chamber_output_2", "on") %}
            ON

automation:
  - alias: Testing Active
    trigger:
      - platform: state
        entity_id: binary_sensor.chamber_output_1
        to: 'on'
      - platform: state
        entity_id: binary_sensor.chamber_output_2
        to: 'on'
    action:
      service: persistent_notification.create
      data:
        notification_id: "1"
        title: "*****TESTING ACTIVE******"
        message: "Test running" 
        
  - alias: Testing Complete
    trigger:
      - platform: state
        entity_id: sensor.chamber_1
        to: 'Chamber Idle'
        for:
          seconds: 15
    action:
      service: persistent_notification.create
      data:
        notification_id: "1"
        title: "Testing complete"  
        message: 'Last updated: {{now().strftime("%Y/%m/%d - %H:%M:%S")}}'

Any input or ideas would be really appreciated.