cancel
Showing results for 
Search instead for 
Did you mean: 

Is there a way for nested conditions?

zludany
Community Member

Why is the automation script below having an error thrown: redundant struct

 

metadata:

  name: fitness custom

description: Scripted automation

automations:

  starters:

    - type: assistant.event.OkGoogle

      eventData: query

      is: "training time"

    - type: device.state.TemperatureControl

      state: temperatureAmbient

      lessThan: 18C

      device: Thermal sensor Wellness - Wellness room

  actions:

    - type: device.command.ActivateScene # Activate or deactivate a scene.

      activate: true

      devices: Wintertime Wellness ON

  starters:

    - type: assistant.event.OkGoogle

      eventData: query

      is: "training time"

    - type: device.state.TemperatureControl

      state: temperatureAmbient

      greaterThan: 22C

      device: Thermal sensor Wellness - Wellness room

  actions:

    - type: device.command.ActivateScene # Activate or deactivate a scene.

      activate: true

      devices: Summertime Wellness ON

1 Recommended Answer

salbando
Community Member

In general, all starters are always "or", but they are also a type of conditional because they are basically saying, "If starter, then 'the rest of the script tries to execute'." An "if" inside of another "if" is logically equivalent to an "and". Therefore, by making your ambient temperature a conditional, you will effectively achieve an "and". So your script would look something like this:

metadata:
  name: fitness custom
  description: Scripted automation

automations:
  - starters:
      type: assistant.event.OkGoogle
      eventData: query
      is: training time
    condition:
      type: device.state.TemperatureControl
      state: temperatureAmbient
      lessThan: 18C
      device: Thermal sensor Wellness - Wellness room
    actions:
      type: device.command.ActivateScene # Activate or deactivate a scene.
      activate: true
      devices: Wintertime Wellness ON
      
  - starters:
      type: assistant.event.OkGoogle
      eventData: query
      is: training time
    condition:
      type: device.state.TemperatureControl
      state: temperatureAmbient
      greaterThan: 22C
      device: Thermal sensor Wellness - Wellness room
    actions:
      type: device.command.ActivateScene # Activate or deactivate a scene.
      activate: true
      devices: Summertime Wellness ON

You will notice that the hyphen that you had on "type" isn't necessary (it doesn't hurt either) because there is only one "type" per block now. Also, you don't need quotes in your OkGoogle query because you aren't using any other delimiter like apostrophe.

 

 

View Recommended Answer in original post

8 REPLIES 8

salbando
Community Member

In order to have more than one starter you need a hyphen in front of each occurrence of the word "starter" with the appropriate indenting.

zludany
Community Member

Ok, where to put the hyphen??

I haven't seen hyphens in front of starters neither in code examples, nor by the pilot.

salbando
Community Member

Here is an example:

 

metadata:
  name: Dog or cat notification
  description: Testing out the dog and cat sensing
automations:
  - starters:
      type: device.event.AnimalCatDetection
      device: Front Yard camera - Front Yard
    actions:
      type: home.command.Notification
      title: Cat
      body: I tawt I taw a puddy tat
      members:
        - John G - #
        - Krista G - #
  - starters:
      type: device.event.AnimalDogDetection
      device: Front Yard camera - Front Yard
    actions:
      type: home.command.Notification
      title: Dog
      body: Oh, dog!
      members:
        - John G - #
        - Krista G - #

 

I should also remind you that in your example in the first starter block if either is true then that action will be performed. In other words, if you say "training time" OR the temperature is less than 18C then you action will be performed. It doesn't seem to me that this is your intent.

zludany
Community Member

I can't see the much needed hyphens in your code by the multiple starters.

My identation was also seemed correct.

The error message was about repeating columns error in struct.

Where can i make the logical and formula definition?

salbando
Community Member

The hyphen is how YAML stores multiple values for each key. If your familiar with programming it is how YAML stores array values. Without it, the script will see it as redundant or repeated values for the same single variable.

zludany
Community Member

Ok, thanks, but how to put AND clause between the starters?

salbando
Community Member

In general, all starters are always "or", but they are also a type of conditional because they are basically saying, "If starter, then 'the rest of the script tries to execute'." An "if" inside of another "if" is logically equivalent to an "and". Therefore, by making your ambient temperature a conditional, you will effectively achieve an "and". So your script would look something like this:

metadata:
  name: fitness custom
  description: Scripted automation

automations:
  - starters:
      type: assistant.event.OkGoogle
      eventData: query
      is: training time
    condition:
      type: device.state.TemperatureControl
      state: temperatureAmbient
      lessThan: 18C
      device: Thermal sensor Wellness - Wellness room
    actions:
      type: device.command.ActivateScene # Activate or deactivate a scene.
      activate: true
      devices: Wintertime Wellness ON
      
  - starters:
      type: assistant.event.OkGoogle
      eventData: query
      is: training time
    condition:
      type: device.state.TemperatureControl
      state: temperatureAmbient
      greaterThan: 22C
      device: Thermal sensor Wellness - Wellness room
    actions:
      type: device.command.ActivateScene # Activate or deactivate a scene.
      activate: true
      devices: Summertime Wellness ON

You will notice that the hyphen that you had on "type" isn't necessary (it doesn't hurt either) because there is only one "type" per block now. Also, you don't need quotes in your OkGoogle query because you aren't using any other delimiter like apostrophe.

 

 

zludany
Community Member

Thanks man.

I did it in parallel on my way, however it was the hard way figuring out on your own.