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 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.
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.
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.