cancel
Showing results for 
Search instead for 
Did you mean: 

Nesting conditions (cond1 and cond2) or (cond3 and cond4)

JacobWaterweg
Community Member

I want an action being executed at 21:50 when I am not at home and at 22:30 when I am at home. I have the following starter and condition. There are no errors but the script is never executed. 

In pseudo code this is what I mean:

   (cond1 and cond2)

or

  (cond3 and cond4)

 

Any ideas what can be wrong?

starters:
- type: time.schedule
at: 21:50
- type: time.schedule
at: 22:30

condition:
type: or
conditions:
- type: and
conditions:
- type: time.between
before: 22:00
- type: home.state.HomePresence
state: homePresenceMode
is: AWAY
- type: and
conditions:
- type: time.between
after: 22:00
- type: home.state.HomePresence
state: homePresenceMode
is: HOME

4 REPLIES 4

salbando
Community Member

The following example should do what you are asking. Since starters are always "Or" then this will turn on a light at 21:50 when it is before 22:00 AND you are away OR it will turn off the light at 22:30 when it is after 22:00 AND you are home.

metadata:
  name: Nested conditionals
  description: Example of nested conditionals
automations:
  - starters:
      type: time.schedule
      at: 21:50
    condition:
      type: and
      conditions:
        - type: time.between
          before: 22:00
        - type: home.state.HomePresence
          state: homePresenceMode
          is: AWAY
    actions:
      type: device.command.OnOff
      on: true
      devices: Family room Lights - Family room
  - starters:
      type: time.schedule
      at: 22:30
    condition:
      type: and
      conditions:
        - type: time.between
          after: 22:00
        - type: home.state.HomePresence
          state: homePresenceMode
          is: HOME
    actions:
      type: device.command.OnOff
      on: false
      devices: Family room Lights - Family room

 

This is the solution that I am using right now.

But the actions is a long list of actions which I had to duplicate. And as a former programmer I dislike duplicate code.

salbando
Community Member

Actually, I got curious about your version and tried the following script and it works fine. In this script, if (S1 and S2) or (S3 and S4) are on then it turns all switches off. I think what is going on with your script is that you need to consider starters as part of the conditional. Essentially, a script executes "If" the starter happens, "Then" the script runs. So essentially, what you have created was a more complex nested conditional than you thought. You have If (starter) Or (starter), then If (condition1 and condition2) or (condition3 and condition4), then action. I used an online truth table generator to evaluate it and out of the 64 possible scenarios,  7 times the overall implication evaluates to false. It is fine for your conclusion to be false that just means your action won't execute, but the overall logic can't evaluate to false. Six of those are the error. One of the 7 is when the conclusion is false when the conditions are met and this should result in a false conditional. In fact, it is the only time a conditional should evaluate to false. This can be confusing. A false hypothesis does not ever yield a false conditional, but a true hypothesis cannot lead to a false conclusion. If you were to enter the logic of the version I gave you originally, you would see it only evaluates once to false (out of the 64 truth values) as it should. I hope this makes sense. Anyway here is my script that shows your idea of nesting conditionals works fine on its own.

 

metadata:
  name: Another nested conditional test
  description: Single starter with nested conditionals
automations:
  starters:
    - type: assistant.event.OkGoogle
      eventData: query
      is: Do conditional test
  condition:
    type: or
    conditions:
      - type: and
        conditions:
          - type: device.state.OnOff
            state: on
            is: true
            device: S1 - Playground
          - type: device.state.OnOff
            state: on
            is: true
            device: S2 - Playground
      - type: and
        conditions:
          - type: device.state.OnOff
            state: on
            is: true
            device: S3 - Playground
          - type: device.state.OnOff
            state: on
            is: true
            device: S4 - Playground
  actions:
    - type: device.command.OnOff
      on: false
      devices:
        - S1 - Playground
        - S2 - Playground
        - S3 - Playground
        - S4 - Playground

 

 

Hollen_ar_mor
Community Member

For multi conditions script, I use "name". i.e. :

name: 21h50

    starter: .....

          -type: .....

 

name: 22h30

   starter: ...

 

Take care of the tabs !