4 weeks ago
I'm writing a script to turn lights off when multiple presence sensors show unoccupied for 30 min. I have the script written just fine - I'm pretty comfortable here. The issue is when I add "for: 30min" to my starters, the "Routine Could Not Be Saved" error appears. Perhaps I'm misusing the "for" command?
I'm able to add "for: 30 min" to one of my presence sensor starters, but as soon as I add a second or 3rd for my additional sensors, I'm no longer able to save the automation. Would that mean "for" affects all of the starters? Or am I right to assume it's only counting the sensor I've "attached it to?"
Below is my script when I add the "for" command for each starter. I did try moving "for" in a different order within each starter too, but that doesn't seem to make a difference.
Is there perhaps a better way to turn off the devices when there's no occupancy for 30 minutes?
# ------------------------------------------------------------------------------------------ #
# Lines starting with “#” are comments and will be ignored by the automation.
# Indentation determines hierarchy within the script.
# Visit g.co/home/script-editor-docs for full documentation.
# ------------------------------------------------------------------------------------------ #
metadata:
name: Basement off when no presence detected # Choose a short name that summarizes important starters and actions, like “Sunset lights”.
description: Turn off basement devices when no presence is detected for 30 minutes # Write a detailed description that includes everything the automation does, like “At sunset, on weekdays, close blinds, turn on lights to 50%, and play the sunset playlist on certain speakers.”
automations:
# “starters” and “actions” are required; “conditions” are optional.
# Use Ctrl + Space to see autocomplete suggestions.
# ---- STARTERS ---- #
# Starters describe events that will start the automation.
# To add more than one starter, duplicate the "- type" section under "starters".
# If you add multiple starter events, any one of them happening will start the automation.
starters:
- type: device.state.OccupancySensing # For devices that can detect occupancy, whether through PIR, ultrasonic, or physical contact sensing.
state: occupancy
# Whether the device senses occupancy. [available operators: is, isNot]
is: UNOCCUPIED
device: Bar presence sensor - Bar
for: 30min
- type: device.state.OccupancySensing # For devices that can detect occupancy, whether through PIR, ultrasonic, or physical contact sensing.
state: occupancy
# Whether the device senses occupancy. [available operators: is, isNot]
is: UNOCCUPIED
device: Basement main presence sensor - Basement
for: 30min
- type: device.state.OccupancySensing # For devices that can detect occupancy, whether through PIR, ultrasonic, or physical contact sensing.
state: occupancy
# Whether the device senses occupancy. [available operators: is, isNot]
is: UNOCCUPIED
device: Basement presence sensor - Basement Living Room
for: 30min
# ---- CONDITIONS ---- #
# Conditions are optional. Delete this section if it’s empty.
# Conditions will prevent an automation from starting if the conditions aren’t met.
# See g.co/home/script-editor-docs for documentation about how to use logical operators like “and”, “or”, and “not”.
condition:
type: and
# "AND" together two or more conditionals
conditions:
- type: device.state.OccupancySensing # For devices that can detect occupancy, whether through PIR, ultrasonic, or physical contact sensing.
state: occupancy
# Whether the device senses occupancy. [available operators: is, isNot]
is: UNOCCUPIED
device: Bar presence sensor - Bar
- type: device.state.OccupancySensing # For devices that can detect occupancy, whether through PIR, ultrasonic, or physical contact sensing.
state: occupancy
# Whether the device senses occupancy. [available operators: is, isNot]
is: UNOCCUPIED
device: Basement main presence sensor - Basement
- type: device.state.OccupancySensing # For devices that can detect occupancy, whether through PIR, ultrasonic, or physical contact sensing.
state: occupancy
# Whether the device senses occupancy. [available operators: is, isNot]
is: UNOCCUPIED
device: Basement presence sensor - Basement Living Room
# ---- ACTIONS ---- #
# Actions will start when a starter event takes place and all conditions are met.
# Actions will start in the order they appear in the script and with any delay specified.
# To add more than one action, duplicate the "- type" section under "actions".
actions:
- type: device.command.OnOff # Turn the device on or off.
# Whether to turn the device on or off.
on: false
devices: Basement Lights - Basement
- type: device.command.OnOff # Turn the device on or off.
# Whether to turn the device on or off.
on: false
devices: Bar Switch - Bar
- type: device.command.OnOff # Turn the device on or off.
# Whether to turn the device on or off.
on: false
devices: Basement Living Room Lights - Basement Living Room
- type: device.command.OnOff # Turn the device on or off.
# Whether to turn the device on or off.
on: false
devices: Laundry room Lights - Laundry room
- type: device.command.OnOff # Turn the device on or off.
# Whether to turn the device on or off.
on: false
devices: Basement Roku - Basement
- type: device.command.OnOff # Turn the device on or off.
# Whether to turn the device on or off.
on: false
devices: Bar Roku - Bar
- type: device.command.OnOff # Turn the device on or off.
# Whether to turn the device on or off.
on: false
devices: Fireplace outlet - Basement Living Room
- type: time.delay
# Adjust duration as needed. Supported units: sec, min, hour
for: 5sec
- type: device.command.OnOff # Turn the device on or off.
# Whether to turn the device on or off.
on: true
devices: Fireplace outlet - Basement Living Room
Answered! Go to the Recommended Answer.
2 weeks ago
Thanks for reaching out. For can only be applied to individual starters or used within a time.delay action.
Could you try removing for from starters and add it to Actions.
actions:
- type: time.delay
for: 30min # Only runs if the conditions remained true for the full 30 mins
- type: device.command.OnOff on: false devices: Basement Lights - Basement
# ... add your other devices here
Let me know if these worked.
3 weeks ago
Well, I turned to Gemini for help. It first recommended exactly how I wrote my script. I didn't give it my current script, just explained what I was trying to accomplish and it spit out what I already wrote. When I explained this fails to save, one of the 3 alternative options it gave me was to write a separate script for each sensor. another alternative as to only rely on 1 sensor for the time duration and I forgot the 2nd alternative, but it wasn't as functional.
Anyway... the "separate script" path should work but it's not as clean. Basically, I added a Starter - sensor 1, then the conditions and actions in the script, followed by Starter - Sensor 2, then the exact same conditions and actions; then the same with the 3rd sensor. Basically, 1 script combining all 3 as if they're separate scripts - if that makes sense.
What I don't like is if I decide to adjust the actions (add or remove smart devices to control), I have to remember to adjust those for each sensor within the script.
I just wrote it, but haven't tested it yet. Logically, it should work. So we'll see.
2 weeks ago
Thanks for reaching out. For can only be applied to individual starters or used within a time.delay action.
Could you try removing for from starters and add it to Actions.
actions:
- type: time.delay
for: 30min # Only runs if the conditions remained true for the full 30 mins
- type: device.command.OnOff on: false devices: Basement Lights - Basement
# ... add your other devices here
Let me know if these worked.