Turn off Switch 2 when Switch 1 is turned off but only if Switch 1 has been on for over N minutes

Hi wonder if any one has done the below and can assist me. I am trying to create an automation that when a switch is turned off after 30 seconds it will turn off other switches, but only if the switch has been on for more than a minute. The intention here is not to turn off the other switches if someone by mistake turned the switch in question on and off quickly. I was thinking of using a helper that is time based that i can set when the switch is turned on and when turned off i can check the time from now to the helper to get the difference, but if there is a cleaner way i would love to know.

Thanks

I think you are on the right path really for your description.

Possibly use a timer helper that is activated when that happens and another automation to do stuff after checking some things when it completes.

Hi, you can set it up like this (remember to change the entities for your own):

description: ""
mode: single
trigger:
  - platform: state
    entity_id:
      - switch.lavadora
    to: "off"
    for:
      hours: 0
      minutes: 0
      seconds: 0
condition:
  - condition: state
    entity_id: switch.lavadora
    state: "on"
    for:
      hours: 0
      minutes: 0
      seconds: 30
action:
  - delay:
      hours: 0
      minutes: 1
      seconds: 0
      milliseconds: 0
  - service: switch.turn_off
    metadata: {}
    data: {}
    target:
      entity_id: switch.lavavajillas

this will trigger? it’s “off” but condition is that it’s on? if this works, that’s interesting and new info for me!

trigger:
  - platform: state
    entity_id:
      - switch.lavadora
    to: "off"
    for:
      hours: 0
      minutes: 0
      seconds: 0
condition:
  - condition: state
    entity_id: switch.lavadora
    state: "on"
    for:
      hours: 0
      minutes: 0
      seconds: 30

You can verify if the automation you’re setting is following the steps you intended to program by clicking on the three dots on the top right corner - Execute (Sorry, UI in spanish):

And after, you click on the button next to the three dots and you can check the steps the automation is making:

image

In this case, first (star) is trigger (switch off), then verify condition (switch on for 1 min) then two possible paths, if true, go on with the delay, if false, stop

I hope I managed to explain myself

i understand the intent. but the issue is that at the point of trigger, the switch is “off”.
then when you check the condition, you’re checking that it’s on and has been on for 30 seconds. by that time, the state is “off” so it fails the condition.

or at least it did for me. i may be doing something wrong.

here’s a trace of a test i did:

{
  "trace": {
    "last_step": "condition/0/entity_id/0",
    "run_id": "ce0aba1bda2cd5bf63d3c42f9b3947c5",
    "state": "stopped",
    "script_execution": "failed_conditions",
    "timestamp": {
      "start": "2024-05-28T20:42:27.568911+00:00",
      "finish": "2024-05-28T20:42:27.569653+00:00"
    },
    "domain": "automation",
    "item_id": "1716928914180",
    "trigger": "state of input_boolean.testbool2",
    "trace": {
      "trigger/0": [
        {
          "path": "trigger/0",
          "timestamp": "2024-05-28T20:42:27.569095+00:00",
          "changed_variables": {
            "this": {
              "entity_id": "automation.test_off_and_on_condition",
              "state": "on",
              "attributes": {
                "id": "1716928914180",
                "last_triggered": null,
                "mode": "single",
                "current": 0,
                "friendly_name": "test off and on condition"
              },
              "last_changed": "2024-05-28T20:42:06.575761+00:00",
              "last_reported": "2024-05-28T20:42:06.575761+00:00",
              "last_updated": "2024-05-28T20:42:06.575761+00:00",
              "context": {
                "id": "01HZ0F0MVFCRBYBFJMDH765CDY",
                "parent_id": null,
                "user_id": null
              }
            },
            "trigger": {
              "id": "0",
              "idx": "0",
              "alias": null,
              "platform": "state",
              "entity_id": "input_boolean.testbool2",
              "from_state": {
                "entity_id": "input_boolean.testbool2",
                "state": "on",
                "attributes": {
                  "editable": true,
                  "friendly_name": "testbool2"
                },
                "last_changed": "2024-05-28T20:42:20.875509+00:00",
                "last_reported": "2024-05-28T20:42:20.875509+00:00",
                "last_updated": "2024-05-28T20:42:20.875509+00:00",
                "context": {
                  "id": "01HZ0F12TAJPDZK02JPSMHEYN6",
                  "parent_id": null,
                  "user_id": "62b8b965a9334cd39e6cd95084161a4b"
                }
              },
              "to_state": {
                "entity_id": "input_boolean.testbool2",
                "state": "off",
                "attributes": {
                  "editable": true,
                  "friendly_name": "testbool2"
                },
                "last_changed": "2024-05-28T20:42:27.561675+00:00",
                "last_reported": "2024-05-28T20:42:27.561675+00:00",
                "last_updated": "2024-05-28T20:42:27.561675+00:00",
                "context": {
                  "id": "01HZ0F19B88BHPTP6KTK268PQ8",
                  "parent_id": null,
                  "user_id": "62b8b965a9334cd39e6cd95084161a4b"
                }
              },
              "for": {
                "__type": "<class 'datetime.timedelta'>",
                "total_seconds": 0
              },
              "attribute": null,
              "description": "state of input_boolean.testbool2"
            }
          }
        }
      ],
      "condition/0": [
        {
          "path": "condition/0",
          "timestamp": "2024-05-28T20:42:27.569301+00:00",
          "result": {
            "result": false
          }
        }
      ],
      "condition/0/entity_id/0": [
        {
          "path": "condition/0/entity_id/0",
          "timestamp": "2024-05-28T20:42:27.569442+00:00",
          "result": {
            "result": false,
            "state": "off",
            "wanted_state": "on"
          }
        }
      ]
    },
    "config": {
      "id": "1716928914180",
      "alias": "test off and on condition",
      "description": "",
      "trigger": [
        {
          "platform": "state",
          "entity_id": [
            "input_boolean.testbool2"
          ],
          "to": "off",
          "for": {
            "hours": 0,
            "minutes": 0,
            "seconds": 0
          }
        }
      ],
      "condition": [
        {
          "condition": "state",
          "entity_id": "input_boolean.testbool2",
          "state": "on",
          "for": {
            "hours": 0,
            "minutes": 0,
            "seconds": 10
          }
        }
      ],
      "action": [
        {
          "service": "light.toggle",
          "metadata": {},
          "data": {},
          "target": {
            "entity_id": "light.study_lights_2"
          }
        }
      ],
      "mode": "single"
    },
    "blueprint_inputs": null,
    "context": {
      "id": "01HZ0F19BGBFCDCX0JV7PW2FJM",
      "parent_id": "01HZ0F19B88BHPTP6KTK268PQ8",
      "user_id": null
    }
  },
  "logbookEntries": []
}

it aborted with “failed_conditions”… but again, maybe i’m doing something wrong…

if @arkixana 's method works, that’s nice and tight. in my test it’s not passing the condition. if it doesn’t work for you then here’s a different approach.

i’d do it w/o a timer.

create a helper switch (input_boolean.armed_switch) and use these two automations:

trigger:
  - platform: state
    entity_id:
      - switch.switch_main
    to: "on"
    for:
      minutes: 1
    id: "on"
  - platform: state
    entity_id:
      - switch.switch_main
    to: "off"
    id: "off"
condition: []
action:
  - service: input_boolean.turn_{{trigger.id}}
    target:
      entity_id: input_boolean.armed_switch

the first arms the armed_switch only if it’s been on for 1 minute.

trigger:
  - platform: state
    entity_id:
      - input_boolean.armed_switch
    to: "off"
    for:
      seconds: 30
condition: []
action:
  - service: switch.turn_off
    target:
      entity_id: switch.other_switches

the armed_switch turns off all your other switches only after it’s been turned off for 30 seconds.

The last_changed property of the from_state will hold a datetime object reflecting when the previous state change occurred (in this case when the door closed). Add your offset to that time, and compare it to the current time.

trigger:
  - alias: "when a switch has been turned 'off' for 30 seconds"
    platform: state
    entity_id: switch.example
    from: 'on'
    to: 'off'
    for: "00:00:30"
condition:
  - alias: "only if the switch had previously been 'on' for more than a minute"
    condition: template
    value_template: >
      {{ now() - trigger.from_state.last_changed >= timedelta(minutes=1, seconds=30) }}
action:

The additional 30 seconds in the timedelta() compensate for the duration required by the trigger.

@armedad I usually use this kind of logic with an automation I have to manage states for my washing machine and other appliances, but as you mention, I usually check another entity (helper) and not the same one that triggers (power vs an input select), so probably you’re correct and in this case won’t work as by the time the condition enters, its already off.

Thank you for pointing it out!

@Didgeridrew Perfect this is exactly what i was looking for. I was going down the helper way but this is much cleaner many thanks