Stop script using 'script.turn_off' not working for me

Hi, I’m really struggling to stop a script once started. I’ve read many posts and tried many options but cannot seem to get a script to stop once started

I’ve read:

and

and

This is where I’ve got to so far:

I’ve set up an automation with 4 triggers. Each “cancel” 3 scrips using script.turn_off then continue to do what I require. I’ve tried canceling from the automation, the script and both but whatever I do when an new option is chosen in the middle of one currently running it won’t work.

Automation: Triggers

  1. Maiden: Off
  2. Maiden: 1h
  3. Maiden: 2h
  4. Maiden: 3h

Script:
A: Cancel what is not being used
B: Turn on switch
C: Wait x hours
D: Turn off switch
E: Send finished notification

One of my 4 scripts

alias: Maiden 1 Hour
sequence:
  - service: script.turn_off
    data: {}
    target:
      entity_id: script.maiden_off
  - service: script.turn_off
    data: {}
    target:
      entity_id: script.maiden_2_hours
  - service: script.turn_off
    data: {}
    target:
      entity_id: script.maiden_3_hours
  - type: turn_on
    device_id: 1d88db3bac3d1745d486c
    entity_id: switch.kitchen_maiden
    domain: switch
  - delay:
      hours: 1
      minutes: 0
      seconds: 0
      milliseconds: 0
  - type: turn_off
    device_id: 1d88db3bac3d1745d486c
    entity_id: switch.kitchen_maiden
    domain: switch
  - device_id: dfdb79ffda1445eb93f45
    domain: mobile_app
    type: notify
    message: 1h script ran
mode: single

My automation trigger:

alias: Maid Trigger
description: ""
trigger:
  - platform: state
    entity_id:
      - input_select.maiden
    id: "Off"
    to: "Off"
  - platform: state
    entity_id:
      - input_select.maiden
    id: 1 Hour
    to: 1 Hour
  - platform: state
    entity_id:
      - input_select.maiden
    id: 2 Hours
    to: 2 Hours
  - platform: state
    entity_id:
      - input_select.maiden
    id: 3 Hours
    to: 3 Hours
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: 1 Hour
        sequence:
          - service: script.turn_off
            entity_id: script.maiden_off
          - service: script.turn_off
            entity_id: script.maiden_1hour
            enabled: false
          - service: script.turn_off
            entity_id: script.maiden_2_hours
          - service: script.turn_off
            entity_id: script.maiden_3_hours
          - device_id: f7c09145a6e3fda1445eb93f45
            domain: mobile_app
            type: notify
            title: Maiden
            message: 1 hour started
          - device_id: 999c8931f9f5e06498ac20d6
            domain: mobile_app
            type: notify
            title: Maiden
            message: 1 hour started
            enabled: false
          - service: script.maiden_1hour
            data: {}
      - conditions:
          - condition: trigger
            id: 2 Hours
        sequence:
          - service: script.turn_off
            entity_id: script.maiden_off
          - service: script.turn_off
            entity_id: script.maiden_1hour
            enabled: true
          - service: script.turn_off
            entity_id: script.maiden_2_hours
            enabled: false
          - service: script.turn_off
            entity_id: script.maiden_3_hours
          - device_id: 7c09145a6e3fda1445eb93f45
            domain: mobile_app
            type: notify
            title: Maiden
            message: 2 hours started
          - device_id: de999c8931f9f5e06498ac20d6
            domain: mobile_app
            type: notify
            title: Maiden
            message: 2 hours started
            enabled: false
          - service: script.maiden_2_hours
            data: {}
      - conditions:
          - condition: trigger
            id: 3 Hours
        sequence:
          - service: script.turn_off
            entity_id: script.maiden_off
          - service: script.turn_off
            entity_id: script.maiden_1hour
            enabled: true
          - service: script.turn_off
            entity_id: script.maiden_2_hours
          - service: script.turn_off
            entity_id: script.maiden_3_hours
            enabled: false
          - device_id: f7c09145a6e3fda1445eb93f45
            domain: mobile_app
            type: notify
            title: Maiden
            message: 3 hours started
          - device_id: de999c8931f9f5e06498ac20d6
            domain: mobile_app
            type: notify
            title: Maiden
            message: 3 hours started
            enabled: false
          - service: script.maiden_3_hours
            data: {}
      - conditions:
          - condition: trigger
            id: "Off"
        sequence:
          - service: script.turn_off
            entity_id: script.maiden_off
            enabled: false
          - service: script.maiden_1hour
            data: {}
          - service: script.turn_off
            entity_id: script.maiden_2_hours
          - service: script.turn_off
            entity_id: script.maiden_3_hours
          - device_id: f7c09145a6e3fda1445eb93f45
            domain: mobile_app
            type: notify
            title: Maiden
            message: "Off"
          - device_id: de999c8931f9f5e06498ac20d6
            domain: mobile_app
            type: notify
            title: Maiden
            message: "Off"
            enabled: false
          - service: script.maiden_off
            data: {}
mode: single

Am I doing something stupid? If so I apologies, I’ve only been using HA for a few weeks.

Any guidance would be appreciated, many thanks.

It’s been a while, so you may have already figured things out, but here are my thoughts.

You can get by with just one script. You can use templates to get the selected delay and then pass it to the script as a variable. But I would just put the script in the automation. I think it would also work if you set the automation mode to restart. That way there is nothing to cancel when the selection is changed (and you have the automation as the whole script).

You could use a timer instead of the delay in the script. Your current trigger sets and starts the timer. Then add a trigger for the timer finish. Then in actions have a chooser for the two triggers in the same way as you do now. The advantage of a timer is that you can have it shown in the dashboard. This may need some tweaking, I didn’t think it too hard.

Hi, many thanks for your reply, yes I did indeed sort it and I’ve been meaning to post my solution for some time. I used a timer as you mentioned.

  1. I created an input select helper for choosing the duration:

  1. I created a timer helper which I can set and reset using an automation:

  1. Created an automation which triggers when a certain input is selected. This in turn sets a timer to the desired time and once counted down it turns off. I also included a few notifications.
alias: Maiden Options
description: ""
trigger:
  - platform: state
    entity_id:
      - input_select.maiden
    id: "Off"
    to: "Off"
  - platform: state
    entity_id:
      - input_select.maiden
    id: 1 Hour
    to: 1 Hour
  - platform: state
    entity_id:
      - input_select.maiden
    id: 2 Hours
    to: 2 Hours
  - platform: state
    entity_id:
      - input_select.maiden
    id: 3 Hours
    to: 3 Hours
  - platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.timer_maiden
    id: Timer Finish
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - "Off"
        sequence:
          - service: timer.finish
            data: {}
            target:
              entity_id: timer.timer_maiden
          - type: turn_off
            device_id: x
            entity_id: x
            domain: switch
          - device_id: x
            domain: mobile_app
            type: notify
            message: "Off"
            title: "Maiden:"
          - device_id: x
            domain: mobile_app
            type: notify
            title: "Maiden:"
            message: "Off"
          - delay:
              hours: 0
              minutes: 0
              seconds: 10
              milliseconds: 0
            enabled: false
          - type: turn_off
            device_id: x
            entity_id: x
            domain: switch
            enabled: false
      - conditions:
          - condition: trigger
            id: 1 Hour
        sequence:
          - service: timer.start
            data:
              duration: "3600"
            target:
              entity_id: timer.timer_maiden
          - type: turn_on
            device_id: x
            entity_id: x
            domain: switch
          - device_id: x
            domain: mobile_app
            type: notify
            message: 1 hour started
            title: "Maiden:"
          - device_id: x
            domain: mobile_app
            type: notify
            message: 1 hour started
            title: "Maiden:"
            enabled: true
      - conditions:
          - condition: trigger
            id: 2 Hours
        sequence:
          - service: timer.start
            data:
              duration: "7200"
            target:
              entity_id: timer.timer_maiden
          - type: turn_on
            device_id: x
            entity_id: x
            domain: switch
          - device_id: x
            domain: mobile_app
            type: notify
            message: 2 hours started
            title: "Maiden:"
          - device_id: x
            domain: mobile_app
            type: notify
            message: 2 hours started
            title: "Maiden:"
            enabled: true
      - conditions:
          - condition: trigger
            id: 3 Hours
        sequence:
          - service: timer.start
            data:
              duration: "10800"
            target:
              entity_id: timer.timer_maiden
          - type: turn_on
            device_id: x
            entity_id: x
            domain: switch
          - device_id: x
            domain: mobile_app
            type: notify
            message: 3 hours started
            title: "Maiden:"
          - device_id: x
            domain: mobile_app
            type: notify
            message: 3 hours started
            title: "Maiden:"
            enabled: true
      - conditions:
          - condition: trigger
            id:
              - Timer Finish
        sequence:
          - service: input_select.select_option
            data:
              option: "Off"
            target:
              entity_id: input_select.maiden
          - type: turn_off
            device_id: x
            entity_id: x
            domain: switch
          - device_id: x
            domain: mobile_app
            type: notify
            message: FINISHED
            title: "Maiden:"
          - device_id: x
            domain: mobile_app
            type: notify
            message: FINISHED
            title: "Maiden:"
mode: single

Added this to a dashboard and hey presto, all good! The wife is now happy the maiden is not burning electricity for hours until she remembers to turn it off.

Screenshot 2023-10-10 at 09.51.35