<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: If else clause in google home script in Home Automation</title>
    <link>https://www.googlenestcommunity.com/t5/Home-Automation/If-else-clause-in-google-home-script/m-p/619513#M46592</link>
    <description>&lt;P&gt;If it’s suddenly stopped working I’d &amp;nbsp;think it’s more the device than the code. Does the device respond to simple manual commands ?&lt;/P&gt;&lt;P&gt;I’ve had 4 or 5 of these if else commands running daily, the biggest problem is getting google to “hear” what I speak, and not go off on some random topic&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 14 May 2024 17:22:40 GMT</pubDate>
    <dc:creator>jh9</dc:creator>
    <dc:date>2024-05-14T17:22:40Z</dc:date>
    <item>
      <title>If else clause in google home script</title>
      <link>https://www.googlenestcommunity.com/t5/Home-Automation/If-else-clause-in-google-home-script/m-p/471350#M21220</link>
      <description>&lt;P&gt;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.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 20 Aug 2023 10:51:03 GMT</pubDate>
      <guid>https://www.googlenestcommunity.com/t5/Home-Automation/If-else-clause-in-google-home-script/m-p/471350#M21220</guid>
      <dc:creator>Zycute711</dc:creator>
      <dc:date>2023-08-20T10:51:03Z</dc:date>
    </item>
    <item>
      <title>Re: If else clause in google home script</title>
      <link>https://www.googlenestcommunity.com/t5/Home-Automation/If-else-clause-in-google-home-script/m-p/471355#M21221</link>
      <description>&lt;P&gt;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?&lt;/P&gt;
&lt;P&gt;Documentation:&lt;BR /&gt;&lt;STRONG&gt;&lt;A href="https://developers.home.google.com/automations/schema/reference/entity/sht_device/sensor_state_state" target="_blank" rel="noopener"&gt;SensorStateState &amp;nbsp;|&amp;nbsp; Automations Script Editor &amp;nbsp;|&amp;nbsp; Google Home Developers&lt;/A&gt;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;&lt;A href="https://developers.home.google.com/automations/schema/reference/entity/sht_device/open_close_state" target="_blank" rel="noopener"&gt;OpenCloseState &amp;nbsp;|&amp;nbsp; Automations Script Editor &amp;nbsp;|&amp;nbsp; Google Home Developers&lt;/A&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;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&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 20 Aug 2023 11:46:17 GMT</pubDate>
      <guid>https://www.googlenestcommunity.com/t5/Home-Automation/If-else-clause-in-google-home-script/m-p/471355#M21221</guid>
      <dc:creator>David_K</dc:creator>
      <dc:date>2023-08-20T11:46:17Z</dc:date>
    </item>
    <item>
      <title>Re: If else clause in google home script</title>
      <link>https://www.googlenestcommunity.com/t5/Home-Automation/If-else-clause-in-google-home-script/m-p/489453#M22817</link>
      <description>&lt;P&gt;You can't have multiple starters&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="ashinzekene_0-1696876871882.png" style="width: 400px;"&gt;&lt;img src="https://www.googlenestcommunity.com/t5/image/serverpage/image-id/29759i16BB7CFF570B1F51/image-size/medium/is-moderation-mode/true?v=v2&amp;amp;px=400" role="button" title="ashinzekene_0-1696876871882.png" alt="ashinzekene_0-1696876871882.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 09 Oct 2023 18:41:19 GMT</pubDate>
      <guid>https://www.googlenestcommunity.com/t5/Home-Automation/If-else-clause-in-google-home-script/m-p/489453#M22817</guid>
      <dc:creator>ashinzekene</dc:creator>
      <dc:date>2023-10-09T18:41:19Z</dc:date>
    </item>
    <item>
      <title>Re: If else clause in google home script</title>
      <link>https://www.googlenestcommunity.com/t5/Home-Automation/If-else-clause-in-google-home-script/m-p/563213#M38759</link>
      <description>&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;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&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 25 Mar 2024 17:21:48 GMT</pubDate>
      <guid>https://www.googlenestcommunity.com/t5/Home-Automation/If-else-clause-in-google-home-script/m-p/563213#M38759</guid>
      <dc:creator>Pat_</dc:creator>
      <dc:date>2024-03-25T17:21:48Z</dc:date>
    </item>
    <item>
      <title>Re: If else clause in google home script</title>
      <link>https://www.googlenestcommunity.com/t5/Home-Automation/If-else-clause-in-google-home-script/m-p/563214#M38760</link>
      <description>&lt;P&gt;Any success yet?&lt;/P&gt;&lt;P&gt;I am trying to use two conditions, too, as in the example below, but no success so far.&lt;/P&gt;&lt;PRE&gt;Starter: "Hey Google - Windows"&lt;BR /&gt;&lt;BR /&gt;Condition 1: Before 1600&lt;BR /&gt;Action: Open all windows&lt;BR /&gt;&lt;BR /&gt;Condition 2: After 1600&lt;BR /&gt;Action: Close all windows&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Personally I think the only problem is to find the correct indents...&lt;/P&gt;</description>
      <pubDate>Sat, 16 Dec 2023 10:14:37 GMT</pubDate>
      <guid>https://www.googlenestcommunity.com/t5/Home-Automation/If-else-clause-in-google-home-script/m-p/563214#M38760</guid>
      <dc:creator>Pat_</dc:creator>
      <dc:date>2023-12-16T10:14:37Z</dc:date>
    </item>
    <item>
      <title>Re: If else clause in google home script</title>
      <link>https://www.googlenestcommunity.com/t5/Home-Automation/If-else-clause-in-google-home-script/m-p/594217#M44418</link>
      <description>&lt;P&gt;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 &lt;span class="lia-unicode-emoji" title=":neutral_face:"&gt;😐&lt;/span&gt; on other occasions it seems to do 2 or 3 actions…&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;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&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;regards&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;jim&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;metadata:&lt;BR /&gt;name: test lights&lt;BR /&gt;description: toggle lights, if off turn on, if on turn off&lt;BR /&gt;automations:&lt;BR /&gt;# if off turn on&lt;BR /&gt;- starters:&lt;BR /&gt;- type: device.state.OnOff&lt;BR /&gt;device: pendant lights - Kitchen&lt;BR /&gt;state: on&lt;BR /&gt;is: false&lt;BR /&gt;actions:&lt;BR /&gt;- type: device.command.OnOff&lt;BR /&gt;devices: pendant lights - Kitchen&lt;BR /&gt;on: true&lt;BR /&gt;# if on turn off&lt;BR /&gt;- starters:&lt;BR /&gt;- type: device.state.OnOff&lt;BR /&gt;device: pendant lights - Kitchen&lt;BR /&gt;state: on&lt;BR /&gt;is: true&lt;BR /&gt;actions:&lt;BR /&gt;- type: device.command.OnOff&lt;BR /&gt;devices: pendant lights - Kitchen&lt;BR /&gt;on: false&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 08 Feb 2024 14:32:08 GMT</pubDate>
      <guid>https://www.googlenestcommunity.com/t5/Home-Automation/If-else-clause-in-google-home-script/m-p/594217#M44418</guid>
      <dc:creator>jh9</dc:creator>
      <dc:date>2024-02-08T14:32:08Z</dc:date>
    </item>
    <item>
      <title>Re: If else clause in google home script</title>
      <link>https://www.googlenestcommunity.com/t5/Home-Automation/If-else-clause-in-google-home-script/m-p/608743#M45761</link>
      <description>&lt;P&gt;Multiple starters work and they can be identical.&amp;nbsp; Conditions can be created to effect if-then-else.&amp;nbsp;&lt;/P&gt;&lt;P&gt;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.&amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;P&gt;Feels like a kludge, but it works.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;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&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 25 Mar 2024 19:21:36 GMT</pubDate>
      <guid>https://www.googlenestcommunity.com/t5/Home-Automation/If-else-clause-in-google-home-script/m-p/608743#M45761</guid>
      <dc:creator>marcg1</dc:creator>
      <dc:date>2024-03-25T19:21:36Z</dc:date>
    </item>
    <item>
      <title>Re: If else clause in google home script</title>
      <link>https://www.googlenestcommunity.com/t5/Home-Automation/If-else-clause-in-google-home-script/m-p/608752#M45762</link>
      <description>&lt;P&gt;Hi Marc,&lt;/P&gt;&lt;P&gt;I tried it - and it works!&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;Indentation is a mess in YAML as one can easily produce validation errors, but your example works like a charm!&lt;/P&gt;&lt;P&gt;Thanks a lot!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 25 Mar 2024 17:16:27 GMT</pubDate>
      <guid>https://www.googlenestcommunity.com/t5/Home-Automation/If-else-clause-in-google-home-script/m-p/608752#M45762</guid>
      <dc:creator>Pat_</dc:creator>
      <dc:date>2024-03-25T17:16:27Z</dc:date>
    </item>
    <item>
      <title>Re: If else clause in google home script</title>
      <link>https://www.googlenestcommunity.com/t5/Home-Automation/If-else-clause-in-google-home-script/m-p/608810#M45765</link>
      <description>&lt;P&gt;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 &lt;span class="lia-unicode-emoji" title=":thumbs_up:"&gt;👍&lt;/span&gt;&lt;span class="lia-unicode-emoji" title=":grinning_face:"&gt;😀&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 25 Mar 2024 22:18:01 GMT</pubDate>
      <guid>https://www.googlenestcommunity.com/t5/Home-Automation/If-else-clause-in-google-home-script/m-p/608810#M45765</guid>
      <dc:creator>jh9</dc:creator>
      <dc:date>2024-03-25T22:18:01Z</dc:date>
    </item>
    <item>
      <title>Re: If else clause in google home script</title>
      <link>https://www.googlenestcommunity.com/t5/Home-Automation/If-else-clause-in-google-home-script/m-p/608812#M45766</link>
      <description>&lt;P&gt;Pat, thanks for testing, marcs approach does indeed work&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 25 Mar 2024 22:19:29 GMT</pubDate>
      <guid>https://www.googlenestcommunity.com/t5/Home-Automation/If-else-clause-in-google-home-script/m-p/608812#M45766</guid>
      <dc:creator>jh9</dc:creator>
      <dc:date>2024-03-25T22:19:29Z</dc:date>
    </item>
    <item>
      <title>Re: If else clause in google home script</title>
      <link>https://www.googlenestcommunity.com/t5/Home-Automation/If-else-clause-in-google-home-script/m-p/619196#M46571</link>
      <description>&lt;P&gt;I tried this and it worked for a bit but no longer does? Did something change&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;metadata:
  name: Motion lighting - MB Presence
  description: When motion is detected, turn on the relivent switch else turn it off
automations:
  # When motion turn on switch
  - starters:
      - type: device.state.MotionDetection
        device: Motion - Master Bedroom
        state: motionDetectionEventInProgress
        is: true
    condition:
      type: device.state.OnOff # The basic on and off functionality for any device that has binary on and off, including plugs and switches as well as many future devices.
      device: (MB) Presence - Z Z Automation
      state: on
      is: false
    actions:
      - type: device.command.OnOff
        devices:
          - (MB) Presence - Z Z Automation
        on: true
  # When inactive turn off switch
  - starters:
      - type: device.state.MotionDetection # For devices that can detect motion.
        device: Motion - Master Bedroom
        state: motionDetectionEventInProgress
        is: false
    condition:
      type: device.state.OnOff # The basic on and off functionality for any device that has binary on and off, including plugs and switches as well as many future devices.
      device: (MB) Presence - Z Z Automation
      state: on
      is: true
    actions:
      - type: device.command.OnOff
        devices:
          - (MB) Presence - Z Z Automation
        on: false&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 12 May 2024 20:15:30 GMT</pubDate>
      <guid>https://www.googlenestcommunity.com/t5/Home-Automation/If-else-clause-in-google-home-script/m-p/619196#M46571</guid>
      <dc:creator>BorrisTheCat</dc:creator>
      <dc:date>2024-05-12T20:15:30Z</dc:date>
    </item>
    <item>
      <title>Re: If else clause in google home script</title>
      <link>https://www.googlenestcommunity.com/t5/Home-Automation/If-else-clause-in-google-home-script/m-p/619513#M46592</link>
      <description>&lt;P&gt;If it’s suddenly stopped working I’d &amp;nbsp;think it’s more the device than the code. Does the device respond to simple manual commands ?&lt;/P&gt;&lt;P&gt;I’ve had 4 or 5 of these if else commands running daily, the biggest problem is getting google to “hear” what I speak, and not go off on some random topic&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 14 May 2024 17:22:40 GMT</pubDate>
      <guid>https://www.googlenestcommunity.com/t5/Home-Automation/If-else-clause-in-google-home-script/m-p/619513#M46592</guid>
      <dc:creator>jh9</dc:creator>
      <dc:date>2024-05-14T17:22:40Z</dc:date>
    </item>
    <item>
      <title>Re: If else clause in google home script</title>
      <link>https://www.googlenestcommunity.com/t5/Home-Automation/If-else-clause-in-google-home-script/m-p/670274#M50964</link>
      <description>&lt;P&gt;I must be doing something wrong. I'm getting 3 validation errors saaying "The current struct has duplicate fields [starters]" (and the same for "[condition]" and "[actions]"&lt;/P&gt;&lt;P&gt;I'm trying to get a simple script to answer a question "Is the gate open?"&amp;nbsp; that tests the value of a simple controllable switch. I got the first half working ok which says "yes the gate is open" when the switch is on, but when I copy and paste that part and make the minor modifications to try to make it say "no it's closed" I get the errors above.&lt;/P&gt;&lt;P&gt;Hope you folks can help &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 17 Jan 2025 18:39:50 GMT</pubDate>
      <guid>https://www.googlenestcommunity.com/t5/Home-Automation/If-else-clause-in-google-home-script/m-p/670274#M50964</guid>
      <dc:creator>Stevie_B</dc:creator>
      <dc:date>2025-01-17T18:39:50Z</dc:date>
    </item>
    <item>
      <title>Re: If else clause in google home script</title>
      <link>https://www.googlenestcommunity.com/t5/Home-Automation/If-else-clause-in-google-home-script/m-p/670277#M50965</link>
      <description>&lt;P&gt;You might check the correct indentation (tab key) and any missing "-" signs at the beginning. YAML is very sensitive in both cases.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here's a simple script I use to sync two lamps. When I switch on/off either one of them, the other will be switched on/off, too.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;metadata:
  name: Sync Lamps
  description: Scripted automation
automations:
  - starters:
      - type: device.state.OnOff
        state: on
        is: true
        device: Lamp 1

    actions:
      - type: device.command.OnOff
        on: true
        devices: Lamp 2

  - starters:
      - type: device.state.OnOff
        state: on
        is: false
        device: Lamp 1

    actions:
      - type: device.command.OnOff
        on: false
        devices: Lamp 2

  - starters:
      - type: device.state.OnOff
        state: on
        is: true
        device: Lamp 2

    actions:
      - type: device.command.OnOff
        on: true
        devices: Lamp 1

  - starters:
      - type: device.state.OnOff
        state: on
        is: false
        device: Lamp 2

    actions:
      - type: device.command.OnOff
        on: false
        devices: Lamp 1&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 17 Jan 2025 09:42:39 GMT</pubDate>
      <guid>https://www.googlenestcommunity.com/t5/Home-Automation/If-else-clause-in-google-home-script/m-p/670277#M50965</guid>
      <dc:creator>Pat_</dc:creator>
      <dc:date>2025-01-17T09:42:39Z</dc:date>
    </item>
    <item>
      <title>Re: If else clause in google home script</title>
      <link>https://www.googlenestcommunity.com/t5/Home-Automation/If-else-clause-in-google-home-script/m-p/670302#M50969</link>
      <description>&lt;P&gt;You are indeed correct. I tidied it up (deleted all the automatic comments first!) and found the indentation errors. Working now. Thanks&lt;/P&gt;</description>
      <pubDate>Fri, 17 Jan 2025 12:33:03 GMT</pubDate>
      <guid>https://www.googlenestcommunity.com/t5/Home-Automation/If-else-clause-in-google-home-script/m-p/670302#M50969</guid>
      <dc:creator>Stevie_B</dc:creator>
      <dc:date>2025-01-17T12:33:03Z</dc:date>
    </item>
  </channel>
</rss>

