02-14-2024 10:23 AM
I'm trying to write a script to turn on a light when motion occurs between two different time ranges. Here's what the AI suggested, the code validates and what seems like it should work.. but the condition check always fails.
metadata:
name: Motion in living room
description: Turn on living room lights if motion between 6am-8am or 6pm-10pm
automations:
- starters:
- type: device.state.MotionDetection
state: motionDetectionEventInProgress
is: true
device: Living room - Motion
condition:
type: or
conditions:
- type: time.between
before: 8:00 AM
after: 6:00 AM
- type: time.between
before: 10:00 PM
after: 6:00 PM
actions:
- type: device.command.OnOff
devices:
- Living Room Dimmer - Living Room
on: true
Am I doing something wrong or can you not use the or condition to check against two different time.between conditions?
I did end up doing a work around by duplicating the starters and having different conditions but seems redundant. I also added a check to see if the lights are off.
metadata:
name: Motion in living room
description: Turn on living room lights when lights are off and motion is detected between 6am-8am or 6pm-10pm
automations:
- starters:
- type: device.state.MotionDetection
state: motionDetectionEventInProgress
is: true
device: Living room - Motion
condition:
type: and
conditions:
- type: time.between
before: 8:00 AM
after: 6:00 AM
- type: device.state.OnOff
is: false
device: Living Room Dimmer - Living Room
actions:
- type: device.command.OnOff
devices:
- Living Room Dimmer - Living Room
on: true
- starters:
- type: device.state.MotionDetection
state: motionDetectionEventInProgress
is: true
device: Living room - Motion
condition:
type: and
conditions:
- type: time.between
before: 10:00 PM
after: 6:00 PM
- type: device.state.OnOff
is: false
device: Living Room Dimmer - Living Room
actions:
- type: device.command.OnOff
devices:
- Living Room Dimmer - Living Room
on: true
02-16-2024 02:50 PM
I have investigated this problem and I agree with you. It seems that there is a bug when trying to "Or" two "time.between". Your solution is a good work-around, but you don't need the extra conditional. It will work if you just use the two starters (since this will behave like an "Or") It certainly doesn't hurt to check if the light is off as an additional criteria, but it isn't necessary. So your script could be a little simpler like this:
metadata:
name: Motion in living room
description: Turn on living room lights when lights are off and motion is detected between 6am-8am or 6pm-10pm
automations:
- starters:
- type: device.state.MotionDetection
state: motionDetectionEventInProgress
is: true
device: Living room - Motion
conditions:
- type: time.between
before: 8:00 AM
after: 6:00 AM
actions:
- type: device.command.OnOff
devices:
- Living Room Dimmer - Living Room
on: true
- starters:
- type: device.state.MotionDetection
state: motionDetectionEventInProgress
is: true
device: Living room - Motion
conditions:
- type: time.between
before: 10:00 PM
after: 6:00 PM
actions:
- type: device.command.OnOff
devices:
- Living Room Dimmer - Living Room
on: true
One of us, or both, should probably report this bug. (I don't work for Google).
11-13-2024 09:58 AM
Did you report this bug?
I'm also hitting it even now. For me it makes almost any scripted automations including time impossible, because I usually want to have different time ranges on weekends.