Script stops working after a few days

I have an automation to turn off a virtual switch for when I don’t want alerts to occur.

alias: Pause Vehicle Detector
sequence:
  - service: input_boolean.turn_off
    data: {}
    target:
      entity_id: input_boolean.vehicle_detector
  - delay:
      hours: 0
      minutes: 5
      seconds: 0
      milliseconds: 0
  - service: input_boolean.turn_on
    data: {}
    target:
      entity_id: input_boolean.vehicle_detector
mode: restart
icon: mdi:car

Initially, the script runs fine, but after a few days it stops. If I edit and save the script it starts working again. The logs when it fails show no steps are executed and when I attempt to run the script manually once it has failed, I get:

Failed to call service script/pause_vehicle_detector. 'NoneType' object has no attribute 'async_turn_on'

The script is triggered by a few different automations, such as garage door open or someone arrives home. The automations/triggers run fine, and show they invoked the script. There are entries in the logbook when the script works but none when it fails. Nothing is in the homeassistant log.

Any ideas how to fix this?

Wondering why your mode is restart instead of single. Is it triggering so many times HA finally bails on it?

What is the purpose of restart?

The purpose of the restart is to extend the period to five minutes after the last trigger.

That isn’t what restart does; It starts a new run after first stopping previous run. The script will

  1. turn off
  2. delay for 5 min
  3. turn on

if interrupted with new trigger will cancel and start over. Most likely on the 5 min delay.

Is this the behavior you wish?

Yes, similar to a light staying on 5 minutes after motion was last detected.

OK, I solved or worked around my problem. Apparently it’s a bug caused by having a script and automation with the same name. I have an automation trigger the script and also can trigger the script via the API. When I open my door, it turns on the person detector, and while it’s off it keeps extending the off period by five minutes when the cameras see a person. So I can go outside for 30 minutes and the notifications will be off, but once I’m back inside 5 minutes I get notifications if someone appears on my property. Same with the vehicles.

1 Like

I’m facing this hurdle too, but the names of my Automation & Script are not the same. Can you say more about the bug you identified, or any other details?

My use is “when a fan on a smart outlet gets turned on or off, begin an endlessly repeating script that will toggle the fan on/off at intervals”. I desire having some air circulation, such that the fan is on for 7 minutes and off for 16. I’m using the Automation to kick off the script. And the Script is set to turn on and off the fan with delays, then end with calling itself. But the Automation is hitting this same error you were seeing. 'NoneType' object has no attribute 'async_turn_on'

1 Like

FWIW, I worked out my issue with an Automation that calls a Script. Baseline, the fan is off. But when the fan is manually turned on, it enters a cycle: 6min on, 4min off.

Automation

alias: Livingroom Fan
description: ''
trigger:
  - platform: device
    type: turned_on
    entity_id: switch.outlet_living_room_fan
    domain: switch
condition: []
action:
  - service: script.1647924545227
    data: {}
mode: restart

Script

alias: Living Room Fan Cycle
sequence:
  - type: turn_on
    entity_id: switch.outlet_living_room_fan
    domain: switch
  - delay:
      hours: 0
      minutes: 6
      seconds: 0
      milliseconds: 0
  - type: turn_off
    entity_id: switch.outlet_living_room_fan
    domain: switch
  - delay:
      hours: 0
      minutes: 4
      seconds: 0
      milliseconds: 0
  - type: turn_on
    entity_id: switch.outlet_living_room_fan
    domain: switch
mode: single
icon: mdi:fan

Still buggy, seems to have regressed in the 2024.2 release