Automation Issue.... help

Hi all.
I have this two automations to turn on a suction fan when i turn on the bathroom light and keep it on for x minutes after the switch-off of the lamp.

- id: '1657758456346'
  alias: Accensione Ventola Bagno con Luce
  trigger:
  - entity_id: switch.light
    platform: state
    from: "off"
    to: "on"
  condition: []
  action:
  - service: switch.turn_on
    entity_id: switch.fan

- id: '1612488456346'
  alias: Spegnimento ritardato Ventola Bagno
  trigger:
  - entity_id: switch.light
    platform: state
    from: "on"
    to: "off"
  condition: []
  action:
  - delay: 00:05:00
  - service: switch.turn_off
    entity_id: switch.fan

If i turn on the light, the fan start and when i turn off the light the fan stay on for 5 minutes. OK

The problem occurs when, after turning off the light, i switch it on and then off a second time: the fan stops instead of being switched on.

Sorry for my bad english :slight_smile:

I believe there is a more elegant solution than this, but you could try this in your automation with id 1657758456346:

Replace

service: switch.turn_on
entity_id: switch.fan

With

service: automation.turn_off
entity_id: automation.1612488456346
service: automation.turn_on
entity_id: automation.1612488456346
service: switch.turn_on
entity_id: switch.fan

you need to make a script, cancel the script and restart it.

script:

script:
  delay_fan_off:
    sequence:
      - delay: 00:05:00
      - service: switch.turn_off
        entity_id: switch.fan

automation revamped.

- id: '1612488456346'
  alias: Spegnimento ritardato Ventola Bagno
  trigger:
  - entity_id: switch.light
    platform: state
    from: "on"
    to: "off"
  condition: []
  action:
  - service: script.turn_off
    entity_id: script.delay_fan_off
  - service: script.delay_fan_off

This will ‘reset’ the 5 minutes and the fan will remain on for 5 more minutes.

Also in the automation turning on the light you would need to stop the script. And then restart it when turning the light from on to off.

Alternatively you could try this:

- id: '1612488456346'
  alias: Spegnimento ritardato Ventola Bagno
  trigger:
  - entity_id: switch.light
    platform: state
    from: "on"
    to: "off"
    for:
      minutes: 5
  condition: []
  action:
  - service: switch.turn_off
    entity_id: switch.fan

In that case it would reset automatically if you switch on the light again before the 5 minutes end. It will only turn off the fan if the light has been off for 5 consecutive minutes.

now if i turn on the light, the script start immediatly, turning off my fan even if the light is on

Yes you shouldnt start the script when turning on the lamp. If you use the script from petro you need to stop it in the automation turning the light on. And start it in the automation turning the light off.
However, give my automation a try with the trigger state for 5 minutes. This works like a charm for me in several different automations.

i tried with 1 minute delay but the fan still on

Hm,… try this format:

- id: '1612488456346'
  alias: Spegnimento ritardato Ventola Bagno
  trigger:
    platform: state
    entity_id: switch.light
    to: 'off'
    for:
      minutes: 5
  action:
  - service: switch.turn_off
    entity_id: switch.fan

i solved in this way:

- id: '1657758456346'
  alias: Accensione Ventola Bagno con Luce
  trigger:
  - entity_id: switch.luce_bagno_taverna
    platform: state
    from: "off"
    to: "on"
  condition: []
  action:
  - service: switch.turn_on
    entity_id: switch.ventola_bagno
  - service: script.turn_off
    entity_id: script.delay_fan_off

- id: '1612488456346'
  alias: Spegnimento ritardato Ventola Bagno
  trigger:
  - entity_id: switch.luce_bagno_taverna
    platform: state
    from: "on"
    to: "off"
  condition: []
  action:
  - service: script.turn_off
    entity_id: script.delay_fan_off
  - service: script.delay_fan_off

thanks all :slight_smile:

sorry, was away. Yep, that should work.