cancel
Showing results for 
Search instead for 
Did you mean: 

Starter => condition1 => action1 => condition2 => action2

Hollen_ar_mor
Community Member

Hello,

I'm trying to write an Automation routine with multiple conditions (like in my post title).

It seems to be possible : GHome Automation give that example : 

 
What's a bit funny is that if you paste the code in Automation Script Editor, it doesn't work at all !!
 

 

metadata:
  name: Synchronize two lights
  description: If one light is turned on, turn the other on, and if one light is turned off, turn the other off.
automations:
  starters:
    type: device.state.OnOff
    device: Alexie Hue 1 - Alexie
    state: on
    is: true

  condition:
      type: device.state.OnOff
      device: Alexie Hue 2 - Alexie
      state: on
      is: false

  actions:
      type: device.command.OnOff
      devices: Alexie Hue 2 - Alexie
      on: true

    starters:
      type: device.state.OnOff
      device: Alexie Hue 1 - Alexie
      state: on
      is: false

    condition:
      type: device.state.OnOff
      device: Alexie Hue 2 - Alexie
      state: on
      is: true

    actions:
      type: device.command.OnOff
      devices: Alexie Hue 2 - Alexie
      on: false

      starters:
        type: device.state.OnOff
        device: Alexie Hue 2 - Alexie
        state: on
        is: true

  condition:
    type: device.state.OnOff
    device: Alexie Hue 1 - Alexie
    state: on
    is: false

  actions:
    type: device.command.OnOff
    device: Alexie Hue 1 - Alexie
    on: true

  starters:
    type: device.state.OnOff
    device: Alexie Hue 2 - Alexie
    state: on
    is: false

  condition:
    type: device.state.OnOff
    device: Alexie Hue 1 - Alexie
    state: on
    is: true

  actions:
    type: device.command.OnOff
    devices: Alexie Hue 1 - Alexie
    on: false

 

 

There's an error at the 2nd "starter"... 

 

It's possible to have several conditions in the same routine ?

 

Otherwise, I can write as much routines as I have conditions. But it's less clean...  😑

1 Recommended Answer

I think I may have sorted this out (testing will happen tonight)...

The key points:

  1. In the documentation they elude to the "Automations" block containing "rules" and each rule contains starters and actions, with conditions be optional.
    1. See here: https://developers.home.google.com/automations/schema/automations 
  2. How do we define multiple rules? Well it seems like it is somewhat buried but it looks like you define different rules by giving a "name" and putting your starters, conditions, and actions underneath that.
    1. See second code example here: https://developers.home.google.com/automations/schema/reference/standard/automation 
  3. Having these multiple rules lets you set different starters and conditions (at least in theory) for the actions you want to take.

I've edited my automation to what I believe will work (as I said, testing will happen tonight). The validator returned no errors when I ran it. As always, with YAML, indentation is crucial, make sure it's right (I removed most of the auto-generated comments so something may have messed up in editing). Feel free to peruse the following code block. I'll have removed any sensitive information but it's fairly straightforward.

I hope this info helps you on your way!

metadata:
  name: Bedtime Wind-Down 
  description: This automation will automatically turn off our nightstand lights after a set amount of time and the TV off after a set amount of time.

automations:
  # ---- TV STARTERS ---- #
  - name: TV Sleep Timer
    starters:
      - type: device.state.OnOff
        state: on
        is: true
        device: <MY TV>

    # ---- CONDITIONS ---- #
    condition:
      type: and
      conditions:
        - type: home.state.HomePresence
          state: homePresenceMode
          is: HOME
        - type: device.state.ArmDisarm
          state: currentArmLevel
          is: HOME
          device: <MY ALARM SYSTEM>
        - type: time.between
          before: 2:30 AM
          after: 9:30 PM
          weekdays:
            - MON
            - TUE
            - WED
            - THU
            - FRI
            - SAT
            - SUN

    # ---- ACTIONS ---- #
    actions:
      - type: time.delay
        for: 1hour30min
      - type: device.command.OnOff
        on: false
        devices: <MY TV>

  # ---- <PERSON> LAMP AUTOMATION ---- #
  # ---- Starter 1 ---- #
  - name: <PERSON> Lamp Starter 1
    starters:
      - type: device.state.Brightness
        state: brightness
        is: 50
        device: <PERSON LAMP>

    # ---- CONDITIONS ---- #
    condition:
      type: and
      conditions:
        - type: device.state.OnOff
          state: on
          is: true
          device: <PERSON LAMP>
        - type: home.state.HomePresence
          state: homePresenceMode
          is: HOME
        - type: device.state.ArmDisarm
          state: currentArmLevel
          is: HOME
          device: <MY ALARM SYSTEM>
        - type: time.between
          before: 2:30 AM
          after: 9:30 PM
          weekdays:
            - MON
            - TUE
            - WED
            - THU
            - FRI
            - SAT
            - SUN

    # ---- ACTIONS ---- #
    actions:
      - type: time.delay
        for: 5 min
      - type: device.command.BrightnessAbsolute
        brightness: 25
        devices: <PERSON LAMP>

 

View Recommended Answer in original post

14 REPLIES 14

linkian19
Community Member

You can have multiple conditions in an automation. I think their example scripts aren't fully correct for how the new editor works.

Regardless, here's an example of multiple conditions using the "AND" type (they also support "OR" and "NOT"):

 condition:
    type: and
    # "AND" together two or more conditionals
    conditions:
      - type: device.state.ArmDisarm # This function supports arming and disarming as used in, for example, security systems.
        state: currentArmLevel
        is: HOME
        device: <MY SECURITY DEVICE>
      - 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: 2:30 AM
        # 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: 9:30 PM # HH:MM XM (12 hours format). Adjust time as needed
        # Optional. Days of the week to apply condition on.
        weekdays:
          - MON
          - TUE
          - WED
          - THU
          - FRI
          - SAT
          - SUN
      - type: home.state.HomePresence
        state: homePresenceMode
        # [available operators: is, isNot]
        is: HOME

 You might want to make sure the formatting on that is correct if you copy/paste, this forum doesn't let me format the code block to YAML (which is what the script editor is in).

This block of conditions checks 3 things:

  1. If I armed my security system to HOME.
  2. If it's between the times of 9:30 PM and 2:30 AM on any day.
  3. And if our Home Presence is seen as HOME.

All of these conditions have to be TRUE in order for the automation to then continue.

 

Hope that helps!

Thanks for your answer linkian19


@linkian19 wrote:

this forum doesn't let me format the code block to YAML (which is what the script editor is in).

 


Yes !  That's pretty curious for a forum talking, among other things, of Automation written in YAML... 🙄

 

Your script is talking about several conditions for the same action.

 

What I want to do is different actions depending on different conditions, like :

 

condition1
   action1

condition2
   action2

...

 

 

I know there's a workaround making as much routines there's different conditions. But it's not satisfying...

Oh! I'm sorry I misunderstood your question.

I'm looking for similar functionality, actually haha. As far as I know we can only set multiple conditions for whatever starters we have and then it will do whatever actions if conditions are met.

I would LOVE to be able to check the current state of the device before performing an action, especially if I have a time delay on an automation.

Hopefully Google will add more support for it (or make it clearer on how to achieve something like that).

In the example they gave (link in my 1st post), "starter" is repeated each time.

I'm going to try this, but it's heavy...

I think I may have sorted this out (testing will happen tonight)...

The key points:

  1. In the documentation they elude to the "Automations" block containing "rules" and each rule contains starters and actions, with conditions be optional.
    1. See here: https://developers.home.google.com/automations/schema/automations 
  2. How do we define multiple rules? Well it seems like it is somewhat buried but it looks like you define different rules by giving a "name" and putting your starters, conditions, and actions underneath that.
    1. See second code example here: https://developers.home.google.com/automations/schema/reference/standard/automation 
  3. Having these multiple rules lets you set different starters and conditions (at least in theory) for the actions you want to take.

I've edited my automation to what I believe will work (as I said, testing will happen tonight). The validator returned no errors when I ran it. As always, with YAML, indentation is crucial, make sure it's right (I removed most of the auto-generated comments so something may have messed up in editing). Feel free to peruse the following code block. I'll have removed any sensitive information but it's fairly straightforward.

I hope this info helps you on your way!

metadata:
  name: Bedtime Wind-Down 
  description: This automation will automatically turn off our nightstand lights after a set amount of time and the TV off after a set amount of time.

automations:
  # ---- TV STARTERS ---- #
  - name: TV Sleep Timer
    starters:
      - type: device.state.OnOff
        state: on
        is: true
        device: <MY TV>

    # ---- CONDITIONS ---- #
    condition:
      type: and
      conditions:
        - type: home.state.HomePresence
          state: homePresenceMode
          is: HOME
        - type: device.state.ArmDisarm
          state: currentArmLevel
          is: HOME
          device: <MY ALARM SYSTEM>
        - type: time.between
          before: 2:30 AM
          after: 9:30 PM
          weekdays:
            - MON
            - TUE
            - WED
            - THU
            - FRI
            - SAT
            - SUN

    # ---- ACTIONS ---- #
    actions:
      - type: time.delay
        for: 1hour30min
      - type: device.command.OnOff
        on: false
        devices: <MY TV>

  # ---- <PERSON> LAMP AUTOMATION ---- #
  # ---- Starter 1 ---- #
  - name: <PERSON> Lamp Starter 1
    starters:
      - type: device.state.Brightness
        state: brightness
        is: 50
        device: <PERSON LAMP>

    # ---- CONDITIONS ---- #
    condition:
      type: and
      conditions:
        - type: device.state.OnOff
          state: on
          is: true
          device: <PERSON LAMP>
        - type: home.state.HomePresence
          state: homePresenceMode
          is: HOME
        - type: device.state.ArmDisarm
          state: currentArmLevel
          is: HOME
          device: <MY ALARM SYSTEM>
        - type: time.between
          before: 2:30 AM
          after: 9:30 PM
          weekdays:
            - MON
            - TUE
            - WED
            - THU
            - FRI
            - SAT
            - SUN

    # ---- ACTIONS ---- #
    actions:
      - type: time.delay
        for: 5 min
      - type: device.command.BrightnessAbsolute
        brightness: 25
        devices: <PERSON LAMP>

 

Yes !  

I juste wrote a test script with "- name:", and the script editor gave no error !

I have to do the same in my personnal routine...

Indentation is the hell ! 👺

And I must copy/paste the same starter for each name...

So just as a further update...

This did not seem to function exactly how I expected it to.

What I expected:

  • Each "name" block would start when it's starter is triggered.
  • Actions underneath each block would run independently of other blocks.

That didn't seem to be the case.

What I observed (or at the very least, this is what it seemed like it was doing):

  • Whichever starter was triggered first would run its actions
  • Other starters and actions would not run until that block was done

 

So, to further explain my use case for my automation and what I want to happen:

  • Routine 1:
    1. Begins when I arm my security system.
    2. Checks for:
      1. between certain time
      2. days of the week
      3. home presence
      4. security system arm level.
    3. If conditions are satisfied performs the following actions:
      1. Turns on my bedside lamps, waits 3 seconds, and sets brightness to 50%
      2. Waits 3 seconds, turns on our bedroom TV
      3. Waits 3 seconds, turns off all other defined lights.
  • Routine 2:
    • This is where I want the meat of the automation to be and where I've defined more starters and actions, ideally would function in the following ways.
      1. TV Automation should start when the TV is turned on from Routine 1.
        1. Checks for various conditions.
        2. Waits for 1 hour 30 minutes.
        3. Turns the TV off (effectively an automatic sleep timer)
      2. AT THE SAME TIME I would like my Lamp Automations to start when their brightness is set to 50% from Routine 1.
        • Over the course of 20 minutes I want the following to happen in 5 minute intervals:
          1. Change lamp brightness to 25%, 10%, 1%, and then off.
          2. Check various conditions including if the lamp is already turned off (don't want to turn it back on if I manually turn it off).
        • Essentially I just want to dim the lights while we're getting settled into bed and then have them turn off.

What seemed to have happened is that when my TV turned on it began it's 1 hour 30 min wait to turn off and nothing else ran. Now I'm assuming that it wasn't because of a starter issue with any of the brightness of the lamps (my lamp starters are triggered by when the brightness is set to the defined number). If I stopped the routine and then manually changed brightness, it seemed like those would actions would perform as expected BUT only for that particular block (as in, if I updated both lamps brightness manually, the first to trigger would be the only one to run).

 

I need to perform some more testing to see if I can verify the behavior, but with thinking a little deeper about YAML, it makes sense that it doesn't quite function exactly how we expect an actual programming language to perform (like python) so IF statements or loops aren't really a thing.

 

If I can confirm the behavior I think I can get around it by making a few more routines (one for each lamp and the TV) and having multiple "name" blocks in those as needed. It's tedious but, I think, doable.

Hollen_ar_mor
Community Member

I think we're not trying to do the same thing.
Thanks to your advice, I managed to make a routine that uses the same triggers, but does not do the same actions depending on different conditions.

Your scene seems a little complicated to me. But if I understood correctly, you want your lights to dim when they are on at 50% and your TV is on.

So why not do:

starter: light = 50%
condition: TV = ON

?

(But you should dim the lights to 50% and turn on the TV at the same time)

I'm glad you got your use case figured out!

I'm basically just trying have my TV turn off after a set amount of time while also having my bedside lights dim and turn off over 20 minutes (unless it was already turned off).

 

I did find that many starters hav a "for" parameter that lets you set a time limit for how long the starter needs to be active before it continues (tv on for 5 minutes for example). I did some testing and it seemed to work the way I'm looking for.

I wrote a comment earlier that got flagged as spam for some reason, I appealed it so we'll see if that gets restored. It had an example and more information that I found out. If you're curious about it all I can write another comment later haha.

 

Happy automating!

My case is exactly the same as yours, I want to use the same starter, but have multiple condition/action blocks share that starter, so that 1 block may run, or the other block, or even both depending on the conditions.. But they are all triggered by the same starter. And obviously, I don't want to have the same problem as the other poster where the distance blocks seem to be blocking/sequential/single threaded.

 

Did you ever make any progress on this?

Unfortunately I wasn't really to come up with a solution (at least at the time) to this issue. The best I had come up with was creating several routines (which is ugly) and while that seemed to work better, it didn't seem to work how I expected, still.

I haven't taken another look at this since then because it was getting too frustrating, haha. I've been meaning to revisit some of this stuff again to see if Google extended support for more actions or scenarios, but haven't thought about it until now.

Hollen_ar_mor
Community Member

Hello,

If you have several conditions and the same action, you can use "or" in your condition.

If the several action depend of a condition, I'd try :

- starter: X

     condition: A

     action: 1

- starter: X

     condition: B

     action: 2

 

Always the same starter, with a "-".

 

 

No, mine was the same as your original problem. Different conditions for different actions, but the same starter

It seemed from this thread that both you and the other respondent had issues with your second suggestion:

- starter: X

     condition: A

     action: 1

- starter: X

     condition: B

     action: 2

The issue seemed to be that it wasn't running both starters simultaneously. Instead it was running them sequentially. So the first had to complete before the second was attempted. Is that correct? With a delay in the first, that caused a problem for Linkian19.

Were you successful with your use case of 2 x starters?

Dabbi
Community Member

Been sitting here all morning trying to get a similar setup to work. So frustrating..it seems so close to validating. Then will have one line wheres it's like I don't understand why this is here... Gah