cancel
Showing results for 
Search instead for 
Did you mean: 

Multiple conditions in script

BrianSullivan
Community Member
Just starting to play with Google Home scripted automations and am having trouble with the correct syntax/indenting for an automation that has multiple conditions.
 
Anybody have a valid example that they can post (or a link) to an automation with multiple conditions?
 
6 REPLIES 6

BrianSullivan
Community Member

OK a bit of fooling around and I think I have this one solved.

SpruceHome
Community Member

Morning @BrianSullivan can you post the example YAML code you got working.  I'm trying to set my closet light bulb brightness and color based on time of day when the door is opened.  I'm struggling a little with the best placement in the Google Script Editor to add the brightness and color.   I'm hoping seeing what you got working will help me figure out my issue.

Here is my current working code:

Working Code as of 21 Oct 2023

 

# ------------------------------------------------------------------------------------------ #

# 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: MB Closet Light ON # Choose a short name that summarizes important starters and actions, like “Sunset lights”.

  description: Turn MB Closet Light ON When Door Opens. Uses Aoetec door sensor to turn on Feit recessed lights.

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.OpenClose

      state: openPercent

      # [available operators: is, isNot, lessThan, lessThanOrEqualTo, greaterThan, greaterThanOrEqualTo]

      is: 100

      device: Master BR Closet Sensor - Master Bedroom

 

  # ---- 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: time.between

    # Optional. Accepts either clock time (10:00:00 AM, with seconds optional, or in a 24 hour format), or 'sunrise' or 'sunset', with an optional offset ('sunrise+10m', for instance)

    before: 21:00 # HH:MM (24 hours format). Adjust time as needed

    # Optional. Accepts either clock time (10:00:00 AM, with seconds optional, or in a 24 hour format), or 'sunrise' or 'sunset', with an optional offset ('sunrise+10m', for instance)

    after: 07:00 # HH:MM (24 hours format). Adjust time as needed

    # Optional. Days of the week to apply condition on.

 

  # ---- 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.

      devices: Master Bedroom Lights - Master Bedroom

      on: true

    - type: time.delay

      for: 3min

 

    # Set MB Closet Lights to 100 Percent Brightness

    - type: device.command.BrightnessAbsolute # Adjust device absolute brightness.

      brightness: 100

      devices: Master Bedroom Lights - Master Bedroom

 

    # Set MB Closet Lights Color to Bright White

    - type: device.command.ColorAbsolute

      color:

        name: white

      devices: Master Bedroom Lights - Master Bedroom

 

    # Turns Group Master Bedroom Lights off after 3min

    - type: device.command.OnOff # Turn the device off.

      devices: Master Bedroom Lights - Master Bedroom

      on: false

 

Example:

If closet door is opened from 7:00am to 9:00 pm Set Light color to white and 100 percent brightness.

If closet door is opened from 9:00pm to 7:00 am Set Light color to Red and 50 percent brightness. ( Cats light to open closet doors in the middle of the night and setting the light to red will hopefully prevent me from waking up.  Also if I get up early and need something out of closet I can open door and not wake up my partner GF)

BrianSullivan
Community Member

Am posting link to working code. Hopefully tabs et al are preserved.

 

https://docs.google.com/document/d/1ja_-4PWK5yC6q9gVnEa4lXFLsAJkKkpToONou7H1zXI/edit?usp=drivesdk

OpethNJ
Community Member

Looks like you solved this but the by the book way of doing multiple conditions is leveraging AND

 

 

condition:
    type: and
    # "AND" together two or more conditionals
    conditions:
      - type: device.state.OpenClose
        state: openPercent
        # [available operators: is, isNot, lessThan, lessThanOrEqualTo, greaterThan, greaterThanOrEqualTo]
        is: 100
        device: AQCS1 Freezer - Kitchen
      - type: home.state.HomePresence
        state: homePresenceMode
        # [available operators: is, isNot]
        is: HOME

madCase
Community Member

Hi,

i made in automation with several conditions. No error when i press validation but when the automation runs it ignores the conditions...

metadata:
name: Luftbefeuchter an, wenn unter 55 & und jemand im Wohnzimmer ist NEU
description: Turn on the humidifier in the living room when the humidity drops too low
automations:
- starters:
- type: device.state.HumiditySetting
device: Das Wohnzimmer - EG Wohnzimmer
state: humidityAmbientPercent
lessThan: 55
condition:
type: or
# "OR" together two or more conditionals
conditions:
- type: device.state.OnOff
state: on
# [available operators: is, isNot]
is: true
device: Auf Holzkommode Neu - EG Wohnzimmer
- type: device.state.OnOff
state: on
# [available operators: is, isNot]
is: true
device: Lampe Sofa - EG Wohnzimmer
- type: device.state.OnOff
state: on
# [available operators: is, isNot]
is: true
device: Pilzlampe - EG Wohnzimmer

actions:
- type: device.command.OnOff
on: true
devices: Luftbefeuchter Wohnzimmer - EG Wohnzimmer
- type: assistant.command.Broadcast
message: Der Luftentfeuchter wurde automatisch eingeschaltet, da die Luftfeuchtikeit unter 55 % gefallen und du dich im Wohnzimmer aufhälst
devices: EG Wohnzimmer Display - EG Wohnzimmer

 

Any advise?

baartu95
Community Member

Same as @madCase, the execution does not starts because no conditions matched:

condition:
type: or
conditions:
- type: home.state.HomePresence
state: homePresenceMode
is: AWAY
- type: time.between
before: sunset
after: sunrise