Automation with various conditions

This is my Automation now, still the same error.

- alias: Turn on/off Main Bathroom Light using sensor4
  trigger:
    - platform: mqtt
      topic: "zigbee2mqtt/IKEA Sensor 4"
      payload: true
      value_template: "{{ value_json.occupancy }}"
  action:
    - choose:
      - conditions:
        - condition: time
          after: '01:00:00'
          before: '06:00:00'
        - condition: state
          entity_id: light.master_bathroom
          state: "off"    
        sequence:
        - service: light.turn_on
          target:
            entity_id: light.master_bathroom    
          data:
            brightness: 35
            color_temp: 554    
        - delay:
            minutes: 3
        - service: light.turn_off
          target:
            entity_id: light.master_bathroom        
      - conditions:
        - condition: state
          entity_id: light.master_bathroom
          state: "off"                                      
        sequence:  
        - service: light.turn_on
          target:
            entity_id: light.master_bathroom    
          data:
            brightness: 255
            color_temp: 225        
        - delay:
            minutes: 3
        - service: light.turn_off
          target:
            entity_id: light.master_bathroom      
      default: []              
  mode: restart

Can you spot any more errors?

I copy-pasted it into my system and it passed “Check Configuration” (i.e. it contains no syntax errors).

If you are still receiving an error, then you are doing something else wrong.

I don’t know whether it matters but I worked a bit more on the indentations, also no syntax errors with the following. Maybe worth a try?

- alias: Turn on/off Main Bathroom Light using sensor4
  trigger:
    - platform: mqtt
      topic: "zigbee2mqtt/IKEA Sensor 4"
      payload: true
      value_template: '{{ value_json.occupancy }}'
  action:
    - choose:
        - conditions:
            - condition: time
              after: '01:00:00'
              before: '06:00:00'
            - condition: state
              entity_id: light.master_bathroom
              state: 'off'
              sequence:
                - service: light.turn_on
                  target:
                    entity_id: light.master_bathroom
                  data:
                    brightness: 35
                    color_temp: 554
                - delay:
                    minutes: 3
                - service: light.turn_off
                  target:
                    entity_id: light.master_bathroom
        - conditions:
            - condition: state
              entity_id: light.master_bathroom
              state: 'off'
              sequence:
                - service: light.turn_on
                  target:
                    entity_id: light.master_bathroom
                  data:
                    brightness: 255
                    color_temp: 225
                - delay:
                    minutes: 3
                - service: light.turn_off
                  target:
                    entity_id: light.master_bathroom
  default: []
  mode: restart

Edited:

topic: zigbee2mqtt/IKEA Sensor 4

to:

topic: "zigbee2mqtt/IKEA Sensor 4"

Thanks, will give it a shot

I assume you are using a text editor to make the suggested changes. What steps do you perform immediately after modifying the automation and saving the changes?

For example, do you run Check Configuration and, if it passes, execute Reload Automations?

Yes, I use Visual Studio Code on a share to Hassio. I make the changes, save it and run check configuration. I get the error on automation then.

In the last example you posted, the indentation for default is incorrect.

I think so too. But the syntax validation checker passed :crazy_face:

I pasted the example, containing the incorrectly indented default, into the Automation Editor and, after clicking the Save button, it immediately flagged the error.

I changed it back to 4 spaces (default). No errors on Check configuration but still get a notification with the following error:

  • Invalid config for [automation]: [to] is an invalid option for [automation]. Check: automation->action->0->choose->0->conditions->1->to. (See /config/configuration.yaml, line 18).
  • Invalid config for [automation]: [sequence] is an invalid option for [automation]. Check: automation->action->0->choose->0->conditions->1->sequence. (See /config/configuration.yaml, line 18).

And if like this?

- alias: Turn on/off Main Bathroom Light using sensor4
  trigger:
    - platform: mqtt
      topic: "zigbee2mqtt/IKEA Sensor 4"
      payload: true
      value_template: "{{ value_json.occupancy }}"
  action:
    - choose:
      - conditions:
        - condition: time
          after: '01:00:00'
          before: '06:00:00'
        - condition: state
          entity_id: light.master_bathroom
          state: "off"    
          sequence:
          - service: light.turn_on
            target:
              entity_id: light.master_bathroom    
            data:
              brightness: 35
              color_temp: 554    
          - delay:
              minutes: 3
          - service: light.turn_off
            target:
              entity_id: light.master_bathroom        
      - conditions:
        - condition: state
          entity_id: light.master_bathroom
          state: "off"                                      
          sequence:  
          - service: light.turn_on
            target:
              entity_id: light.master_bathroom    
            data:
              brightness: 255
              color_temp: 225        
          - delay:
              minutes: 3
          - service: light.turn_off
            target:
              entity_id: light.master_bathroom      
      default: []              
  mode: restart

Where in the example I posted is there a to option?

Either your actual version of the automation is different than what I suggested or the error message is referring to some other automation you have.

I have checked my entire automation.yaml file and there is no condition that has a “to”, only state triggers. I’m unsure where this comes from. I have rebooted but it still comes up.

This information:

automation->action->0->choose->0->conditions->1->to.

tells us that the offending option is located in an automation’s action in the first conditions section of the first choose statement (and it’s the second condition within conditions).

That’s clearly not in the example I posted so it’s somewhere else.

Use Visual Studio Code to find all choose statements in all of your automation files (I assume you have more than one otherwise your manually created automations are combined with those composed using the Automation Editor).

I already done that and found no “to” anywhere after any condition. I only create automation manually so no Automation editor stuff in my yaml file. The first example I posted had a “to” in the condition but I changed that to state later on. on my first reply.

Post the automation you are currently using (I no longer know if you are using the one I suggested, tamsy’s version, or a hybrid of the two).

- alias: Turn on/off Main Bathroom Light using sensor4
  trigger:
    - platform: mqtt
      topic: "zigbee2mqtt/IKEA Sensor 4"
      payload: true
      value_template: "{{ value_json.occupancy }}"
  action:
    - choose:
      - conditions:
        - condition: time
          after: '01:00:00'
          before: '06:00:00'
        - condition: state
          entity_id: light.master_bathroom
          state: "off"
          sequence:
          - service: light.turn_on
            target:
              entity_id: light.master_bathroom
            data:
              brightness: 35
              color_temp: 554
          - delay:
              minutes: 3
          - service: light.turn_off
            target:
              entity_id: light.master_bathroom
      - conditions:
        - condition: state
          entity_id: light.master_bathroom
          state: "off"
          sequence:
          - service: light.turn_on
            target:
              entity_id: light.master_bathroom
            data:
              brightness: 255
              color_temp: 225
          - delay:
              minutes: 3
          - service: light.turn_off
            target:
              entity_id: light.master_bathroom
      default: []
  mode: restart

That version isn’t mine and contains an error from tamsy’s example. Both sequence blocks are over-indented.

For your convenience, here it is:

- alias: Turn on/off Main Bathroom Light using sensor4
  trigger:
    - platform: mqtt
      topic: "zigbee2mqtt/IKEA Sensor 4"
      payload: true
      value_template: "{{ value_json.occupancy }}"
  action:
    - choose:
      - conditions:
        - condition: time
          after: '01:00:00'
          before: '06:00:00'
        - condition: state
          entity_id: light.master_bathroom
          state: "off"
        sequence:
        - service: light.turn_on
          target:
            entity_id: light.master_bathroom
          data:
            brightness: 35
            color_temp: 554
        - delay:
            minutes: 3
        - service: light.turn_off
          target:
            entity_id: light.master_bathroom
      - conditions:
        - condition: state
          entity_id: light.master_bathroom
          state: "off"
        sequence:
        - service: light.turn_on
          target:
            entity_id: light.master_bathroom
          data:
            brightness: 255
            color_temp: 225
        - delay:
            minutes: 3
        - service: light.turn_off
          target:
            entity_id: light.master_bathroom
      default: []
  mode: restart

Thank you. I moved the sequence back 2 spaces and now the error is done.

Thank a million for the help. Where can I read more about the indentation rules for yaml? If you have any good resources I would appreciate it very much,

1 Like

Glad to hear it finally resolved the problem.

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic is resolved. This helps other users find answers to similar questions. For more information, refer to guideline 21 in the FAQ.

Start here: YAML

Then review the syntax for choose.

1 Like