Automation with various conditions

Hello Everyone,

I need some help to finish an automation. I have a motion sensor that turns on my bathroom light. I had it to set a low light between 1-6 am and full-strength any other time. It then has a delay of 3 minutes and switches off if no motion is detected. This works fine most of the time but I decided that I wanted it to stay on and ignore the motion sensor if it’s switched on manually. Therefore the state of the light.master_bathroom light.master_bathroom has to be “off”. I’m constantly getting the error “[choose] is an invalid option for [automation]” although I have used it before in automations. Any help will be appreciated.

This is what I have so far:

- 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
          to: "off"    
        sequence:
        - service: light.turn_on
          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
          to: "off"                                      
      sequence:  
      - service: light.turn_on
        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

You are getting the error message because the choose statement is malformed. The second sequence block is insufficiently indented. Indent the entire block by two more spaces.

I did as you said but I’m still getting the error. Do I not need some “or”/“and” condition for the 2 conditions after the choose?

Because it still contains indentation errors. Indent minutes by two more spaces.

Although not an error, the syntax to call a service is inconsistent (a mix of old and modern). This is the old-fashioned way of defining a service call:

      - service: light.turn_on
        entity_id: light.master_bathroom    
        data:
          brightness: 255
          color_temp: 225

Consider updating it to the modern way:

      - service: light.turn_on
        target:
          entity_id: light.master_bathroom    
        data:
          brightness: 255
          color_temp: 225

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).