cancel
Showing results for 
Search instead for 
Did you mean: 

If else clause in google home script

Zycute711
Community Member

Im trying to write a script that checks a window sensor, then does a different action depending on whether the window is open or closed. Is there a way i can write this without having two scripts. Im currently having 1 script for condition A and another for condition B. In usual coding I can use an else clause, but I am not sure how this is done in google home automation script. 

9 REPLIES 9

David_K
Platinum Product Expert
Platinum Product Expert

I don't think the script editor has an if then else clause. You can have multiple starters and actions in the same script, so you could try something like this?

Documentation:
SensorStateState  |  Automations Script Editor  |  Google Home Developers
OpenCloseState  |  Automations Script Editor  |  Google Home Developers

 

 

metadata:
  name: Window sensor
  description: Window sensor
automations:
# If window is open
- starters:
  - type: device.state.OpenClose
    device: Device Name - Room Name
    state: openPercent
    is: 100

  actions:
# Put actions here for when window is open

# If window is closed
- starters:
  - type: device.state.OpenClose
    device: Device Name - Room Name
    state: openPercent
    is: 0

  actions:
# Put actions here for when window is closed

 

 

You can't have multiple starters

ashinzekene_0-1696876871882.png

 

Pat_
Community Member

Yes you can. I have a script with four starters for syncing two bulbs depending on each bulb's state (on/off) and it works just fine.

 

metadata:
  name: Sync Lamps
  description: Scripted automation
automations:
  - starters:
      - type: device.state.OnOff
        state: on
        is: true
        device: Lamp Left - Office

    actions:
      - type: device.command.OnOff
        on: true
        devices: Lamp Right - Office

  - starters:
      - type: device.state.OnOff
        state: on
        is: false
        device: Lamp Left - Office

    actions:
      - type: device.command.OnOff
        on: false
        devices: Lamp Right - Office

  - starters:
      - type: device.state.OnOff
        state: on
        is: true
        device: Lamp Right - Office

    actions:
      - type: device.command.OnOff
        on: true
        devices: Lamp Left - Office

  - starters:
      - type: device.state.OnOff
        state: on
        is: false
        device: Lamp Right - Office

    actions:
      - type: device.command.OnOff
        on: false
        devices: Lamp Left - Office

 

jh9
Community Member

Hi David, I tried that, but got unpredictable results when the automation runs, I think it’s trying to do the two starters sequentially. Basically I’m after a script that turns lights on (if they are off) and off (if they are on). When I run the script it turns the lights on, then off 😐 on other occasions it seems to do 2 or 3 actions…

ps the reason is to minimise the growing number of automations I have, an if then else command would half the number of automations needed

regards

jim

metadata:
name: test lights
description: toggle lights, if off turn on, if on turn off
automations:
# if off turn on
- starters:
- type: device.state.OnOff
device: pendant lights - Kitchen
state: on
is: false
actions:
- type: device.command.OnOff
devices: pendant lights - Kitchen
on: true
# if on turn off
- starters:
- type: device.state.OnOff
device: pendant lights - Kitchen
state: on
is: true
actions:
- type: device.command.OnOff
devices: pendant lights - Kitchen
on: false

Pat_
Community Member

Any success yet?

I am trying to use two conditions, too, as in the example below, but no success so far.

Starter: "Hey Google - Windows"

Condition 1: Before 1600
Action: Open all windows

Condition 2: After 1600
Action: Close all windows

 

Personally I think the only problem is to find the correct indents...

marcg1
Community Member

Multiple starters work and they can be identical.  Conditions can be created to effect if-then-else. 

Here's an example where "OK Google, is the garage door closed?" gives different answers depending on the state of a linked Shelly DW2 door sensor.   

Feels like a kludge, but it works.  

 

automations:
  - starters:
      type: assistant.event.OkGoogle
      eventData: query
      is: Is the garage door closed
   
    condition:
      type: device.state.OpenClose 
      state: openPercent
      is: 0
      device: Garage Door Sensor - Garage

    actions:
      type: assistant.command.Broadcast
      message: Yes, the garage door is closed
      devices: Kitchen display - Kitchen

  - starters:
      type: assistant.event.OkGoogle
      eventData: query
      is: Is the garage door closed

    condition:
      type: device.state.OpenClose 
      state: openPercent
      is: 100
      device: Garage Door Sensor - Garage

    actions:
      type: assistant.command.Broadcast
      message: No, the garage door is open
      devices: Kitchen display - Kitchen

 

 

 

 

Pat_
Community Member

Hi Marc,

I tried it - and it works!

The little trick is that you cannot have the same command - as in your case "Is the garage door closed" - over two separate routines with different commands (one for opening, one for closing) as it will tell you that the command is already blocked by another routine.

But within the same routine it's just fine to have two actions triggered by the same command and now it's working just like intended.

Indentation is a mess in YAML as one can easily produce validation errors, but your example works like a charm!

Thanks a lot!

 

 

jh9
Community Member

Pat, thanks for testing, marcs approach does indeed work 

jh9
Community Member

Marc, many thanks for the explanation and code, I changed it first to tell me the state of the lights, and then changed it to invert the state instead, I now have one command that flips the lights on or off 👍😀