Help with cancelling a running automation

Hi, I am fairly new to home assistant and trying to setup an automation which I am unable to figure out.
My use case is that I have a desktop switch which I want to turn on (if current state is off) when I scan an nfc tag. If I scan the same tag again when the state is on, I want to start a timer for 15 mins and then turn off. This is because I have an oled monitor which runs pixel refresher after I turn the screen off, so I want to wait for this.

The above works fine, but I want to include a case when I trigger the off timer and then decide I need to cancel it. Is there a way to incorporate in my script. Below is the automation at the moment. I would like to add a case where the timer is running to turn off and if I scan tag, I want it to cancel the timer.

alias: Desktop on off
description: ""
trigger:
  - platform: tag
    tag_id: 5488ad84-21ab-3ab4-9223-1c07face1062
condition: []
action:
  - if:
      - condition: state
        entity_id: switch.desktop_switch
        state: "off"
    then:
      - service: switch.turn_on
        data: {}
        target:
          entity_id: switch.desktop_switch
    else:
      - delay:
          hours: 0
          minutes: 15
          seconds: 0
          milliseconds: 0
      - service: switch.turn_off
        data: {}
        target:
          entity_id: switch.desktop_switch
      - service: notify.mobile_app_s21
        data:
          title: Desktop Off
          message: Desktop turned off
mode: single

You could:

  • Create a helper timer instead of using a delay in the auomation
  • Add a trigger for a timer.finished event
  • Replace your if statement with a choose

The finished automation might look something like this:

alias: Desktop on off
description: ""
trigger:
  - platform: tag
    tag_id: 5488ad84-21ab-3ab4-9223-1c07face1062
    id: tag
  - platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.your_nfc_timer
    id: timer    
condition: []
action:
  - choose:
    - conditions:
      - condition: trigger
        id: timer
      sequence:
        - service: switch.turn_off
          data: {}
          target:
            entity_id: switch.desktop_switch
        - service: notify.mobile_app_s21
          data:
            title: Desktop Off
            message: Desktop turned off  
   - conditions:
      - condition: state
        entity_id: switch.desktop_switch
        state: "off"
      sequence:
        - service: switch.turn_on
          data: {}
          target:
            entity_id: switch.desktop_switch
    - conditions:
      - condition: state
        entity_id: timer.your_nfc_timer
        state: idle
      sequence:
        - service: timer.start
          data:
            duration: 00:15:00
          target:
            entity_id: timer.your_nfc_timer            
    - conditions:
      - condition: state
        entity_id: timer.your_nfc_timer
        state: active
      sequence:
        - service: timer.cancel
          target:
            entity_id: timer.your_nfc_timer            
mode: single

Those conditions, in order, do the following:

  1. If the timer just finished, turn off the switch
  2. If the switch is off, turn it on
  3. If the timer isn’t running (and if we get here, we know the switch is on), start it
  4. If the timer is running, cancel it
1 Like

You can also have a button that disables the automation, waits a couple of seconds and re-enables it again - this will cancel all running actions in that automation.

1 Like

Thanks very much for the detailed response. Can you please clarify if “choose” only executes one of the condition and finishes the automation?

It will run the actions tied to the condition (or choice) plus anything that follows that is outside of the choice. So you could have some universal automatons that run before or after the choice selection regardless of what triggered it.

So in the code provided, the sequence only under that one condition would execute.

1 Like

Thanks very much for clarifying.

Let me also clarify, it will execute ALL conditions that are met, so it’s only one and done if you configure it to be so.

A Choose statement will only execute the first sequence that has matching conditions, even if later conditions would have matched. In this way, it’s as if the first option were an if/then, and every subsequent options were and elseif/then. At least, this is the way it’s described in the documentation.

1 Like

I was referring to multiple choice actions.