Delaying turn off automation/script

I am currently running a automation to turn off the fan after 4 minutes when the switch goes off. I have a switch on a NodeMCU then a fan connected to a wifi switch. Using an automation that fires a script after 4 minute delay.

Automation:

- id: '1618445631637'
  alias: Toilet switch fan off
  description: ''
  trigger:
  - platform: mqtt
    topic: stat/bathroom_95FD09/POWER2
    payload: 'OFF'
  condition: []
  action:
  - delay:
      hours: 0
      minutes: 4
      seconds: 0
      milliseconds: 0
  - service: script.toilet_fan_switch_off
  mode: single

Reference Script:

toilet_fan_switch_off:
  alias: Toilet Fan Switch off
  sequence:
  - type: turn_off
    device_id: 718b56eb2840d46c243f54451929b154
    entity_id: switch.powertech_plug_2
    domain: switch
  mode: single
  icon: mdi:fan-off

The problem is that if someone turns the fan switch back on during that 4 minute period, the automation turns it off regardless of the switch now being on.

I know there must be a way that I have some type of condition or checking method to stop it from turning off if the switch is now back on, I am just not sure how to go about it. Can anyone help?

The toilet fan is an entity named switch.powertech_plug_2.

Where is the entity representing the other switch (the one based on the NodeMCU)? In other words, why is it not modelled as an MQTT Switch (or even an MQTT Binary Sensor) instead of reading its status via an MQTT Trigger?

If you model it as an MQTT Switch, you can easily solve the problem you are facing by using a State Trigger with a for: '00:04:00' option (instead of delay).

1 Like

Thanks for the reply @123 Taras. I probably haven’t got it set up as best that I could, I think I originally had some problem with doing it via switch so I fell back to just using the MQTT method. I have the following in my config to allow the switch already:

  - platform: mqtt
    name: "Toilet Fan Switch"
    state_topic: stat/bathroom_95FD09/POWER2
    payload_on: "ON"

That has resulted in these two:

binary_sensor.toilet_fan_switch
switch.tasmota2

I will try to work out how you have mentioned to try doing it and see if I can get it going

@123 what about this one:

- id: '1618445631637'
  alias: Toilet switch fan off
  description: ''
  trigger:
  - platform: state
    entity_id: switch.tasmota2
    to: 'off'
    for: 0:04:00
  condition: []
  action:
  - service: script.toilet_fan_switch_off
  mode: single

That’s effectively what I suggested but I am a bit confused by what you said in your previous post. You showed a configuration for an MQTT Binary Sensor and then indicated it resulted in two entities:

I don’t see how that’s possible. You must have created an MQTT Switch but didn’t post its configuration?

Maybe the second one, ‘switch.tasmota2’ might be from mqtt auto discovery? I am not sure.

In that case you remove the MQTT Binary Sensor configuration. The auto-discovered switch is all that’s needed here.

So does the suggested State Trigger, with the for: '00:04:00' option, resolve the problem you were experiencing?