Only turn off switch if

I am trying to incorporate in automation to turn off a switch in my “actions” of another automation with its own trigger and conditions already set.

Trigger: turn off a plug
Conditions: certain time of the day and others
Actions: turn on switch, turn off a couple other switches/plugs, delay, turn off another plug, and then I want to turn off the first switch turned on in the “actions” but only if it was turned on by the automation or has been on for less than x seconds…

I’ve tried a ton of different things with no luck so far…
Thanks in advance!

There is an If/Then/Else action you can use. Without seeing some of what you’ve tried, it’s hard to give you anymore pointers than that.

If I understand what you’re asking (and I very likely don’t), set a variable to the current state of the switch as the first action of your automation. Then at the end, use that variable (and whatever time condition you need in an “OR” statement) as a condition to turn it off

Here’s my automation…

alias: "Cora's Room: stars off / minions off"
trigger:
  - platform: device
    type: turned_off
    device_id: 05e588fc2e459c12fb5df85554b68e6d
    entity_id: switch.coras_stars
    domain: switch
condition: []
action:
  - alias: Turn on Emily's Craft Room if it's off...
    if:
      - condition: state
        entity_id: switch.emilys_craft_room
        state: "off"
    then:
      - type: turn_on
        device_id: a302cfca658bc1004f49c433506f68fb
        entity_id: 8f08c1d4b774f4d28658820c906bb624
        domain: switch
  - type: turn_off
    device_id: e6bcd91a6bf0f8243772157a0ef190ab
    entity_id: 352764d51ba45a14e0c5b049c8445d40
    domain: switch
  - delay:
      hours: 0
      minutes: 0
      seconds: 5
      milliseconds: 0
  - type: turn_off
    device_id: ba55da481b13deeea5ab8620df3dc01c
    entity_id: switch.minions
    domain: switch
  - type: turn_off
    device_id: 3f5451da20b18a7409933328616b0ae4
    entity_id: 03631e0e30a01dcc65831cdf538744ab
    domain: switch
  - delay:
      hours: 0
      minutes: 0
      seconds: 25
      milliseconds: 0
  - type: turn_off
    device_id: a302cfca658bc1004f49c433506f68fb
    entity_id: 8f08c1d4b774f4d28658820c906bb624
    domain: switch
mode: single