Script's switch returns to off

Hello friends,
I have a script in my hassio that:

  1. turns on a fan
  2. wait some time (30…120 minutes, depends by a slider)
  3. then turn off the fan.

What I expect is: when I start the script via the related switch on the Lovelace interface, the switch goes to ON position and remains ON until the script is terminated and the fan turn off.

The probelm is when I turn on the switch, it returns to off after some seconds, but the script is not yet terminated and the fan is active. If I update the Home Assistant webpage (pressing F5 for example), the switch returns to on position as expected.

It’s a strange behaviour.

Could you help me?

This is the source of the script:

ventilazione_forzata:
  alias: "Ventilazione a tempo"
  sequence:
    - data:
        entity_id: switch.ventola
      service: switch.turn_on
    - delay:
        minutes: "{{ states('input_number.slider_fan') | int }}"
    - data:
        entity_id: switch.ventola
      service: switch.turn_off

My hassio is latest version (107.x), and it is installed into a Raspberry.
I have it since version 0.94, and this issue has always been present.
The fan is connected to a Sonoff with Tasmota firmware, but I think this is not useful, because the problem seems related to the script itself.

If you let the script run to completion does the fan turn off. If it does then there is nothing wrong with the script. You could also test this by activating the script from the services screen stand-alone. one would really need to understand what UI control you are using to active the script.

Yep, I agree with micque, the script looks fine.
So the question is how are you triggering it.
I generally use something similar in an automation which is triggered when the switch is turned on.

I think this might be a bug. I’ve seen it reported before. Can’t find that message though or an open issue.

Thank you to all;

micque:
yes, if I let the script run, it complete its sequence turning off the fan. The script itself is working good. The problem is on the switch behaviour.

micque and Mutt:
The script is triggered by a switch on the Lovelace interface. No other way to start this script.
switch
This is the code on Lovelace:

entities:
  - entity: switch.ventola
    secondary_info: last-changed
  - entity: script.ventilazione_forzata
    icon: 'mdi:camera-timer'
    secondary_info: last-changed
  - entity: input_number.slider_fan
    icon: 'mdi:clock'
show_header_toggle: false
theme: default
title: Ventilazione
type: entities

tom_l: thank you to inform me that this might be a bug. If it’s true, it exists since a long time, and I didn’t find any thread or forum speaking about it.

You can file an issue here if there’s not already one.
Until then, a workaround would be a template switch for the script…

switch:
  - platform: template
      delayed_switch:
        value_template: "{{ is_state('script.delayed_script', 'on') }}"
        turn_on:
          service: homeassistant.turn_on
          data:
            entity_id: script.delayed_script
        turn_off:
          service: homeassistant.turn_off
          data:
            entity_id: script.delayed_script
1 Like

i have seen this behaviour to it has been like that quite a wile

And it seems like nearly two years later the bug is still there?

Actually the switch has been replaced with the word “Execute” and “Abort” (“Esegui” and “Annulla” in italian language). When I press the button, the word change accordingly and have the expected behaviour.
So, in my experience, the bug is solved.
immagine