Help with multiple and or conditions

Hi
I am trying to get the following code to work with my hallway motion sensor and to turn off the light:

  1. If there has been no motion for 30 seconds with the following conditions:
    2a) The light is already on
    2b) The sensor.day_night is either ‘day’ or ‘night’
    2c) If sensor.day_night is ‘day’ then the sun.sun has to be ‘above_horizon’

I have done the following:

sensor.day_night

  - platform: template
    sensors:
      day_night:
        friendly_name: 'Time of Day'
        value_template: >-
          {%- if now().hour >= 18 and now().hour < 23 %}
          Evening
          {% elif now().hour >= 23 and now().hour < 6 %}
          Night
          {% else %}
          Day
          {%- endif %}

hallway_motion_off.yaml

alias: 'Hallway Motion Off'
trigger:
  - platform: state
    entity_id: sensor.hallway_motion_sensor
    to: 'OK'
    for:
      seconds: 30
condition:
  condition: and
  conditions:
    - condition: state
      entity_id: light.hallway_1
      state: 'on'
    - condition: state
      entity_id: sun.sun
      state: 'above_horizon'
    - condition: or
      conditions:
      - condition: state
        entity_id: sensor.day_night
        state: 'Day'
      - condition: state
        entity_id: sensor.day_night
        state: 'Night'
action:
  service: homeassistant.turn_off
  entity_id: light.hallway_1
  data:
    transition: 5

But of course that doesn’t turn the light off over night when sensor.day_night is ‘night’ as the sun isn’t ‘above_horizon’.

Can you have a AND condition within an OR statement like this:

alias: 'Hallway Motion Off'
trigger:
  - platform: state
    entity_id: sensor.hallway_motion_sensor
    to: 'OK'
    for:
      seconds: 30
condition:
  condition: and
  conditions:
    - condition: state
      entity_id: light.hallway_1
      state: 'on'
    - condition: or
      conditions:
      - condition: state
        entity_id: sensor.day_night
        state: 'Night'
      - condition: and
        conditions:
        - condition: state
          entity_id: sun.sun
          state: 'above_horizon'
        - condition: state
          entity_id: sensor.day_night
          state: 'Day'
action:
  service: homeassistant.turn_off
  entity_id: light.hallway_1
  data:
    transition: 5

Thanks

1 Like

I haven’t looked at the logic closely, but you CAN nest conditions.

1 Like

I have tried the above and below both result in HA failures:

alias: 'Hallway Motion Off'
trigger:
  - platform: state
    entity_id: sensor.hallway_motion_sensor
    to: 'OK'
    for:
      seconds: 30
condition:
  condition: and
  conditions:
    - condition: state
      entity_id: light.hallway_1
      state: 'on'
    - condition:
      - condition: or
        conditions:
        - condition: state
          entity_id: sensor.day_night
          state: 'Night'
        - condition:
            condition: and
            conditions:
              - condition: state
                entity_id: sun.sun
                state: 'above_horizon'
              - condition: state
                entity_id: sensor.day_night
                state: 'Day'
action:
  service: homeassistant.turn_off
  entity_id: light.hallway_1
  data:
    transition: 5

Does anyone have any suggestions what I am doing wrong please?

Try the following. Also, double check the case of the states…typically, they are lower case.

condition:
    - condition: state
      entity_id: light.hallway_1
      state: 'on'
    - condition: or
      conditions:
        - condition: state
          entity_id: sensor.day_night
          state: 'Night'
        - condition:
            - condition: state
              entity_id: sun.sun
              state: 'above_horizon'
            - condition: state
              entity_id: sensor.day_night
              state: 'Day'

Post errors, if any.

Try:

alias: 'Hallway Motion Off'
trigger:
  - platform: state
    entity_id: sensor.hallway_motion_sensor
    to: 'OK'
    for:
      seconds: 30
condition:
  condition: and
  conditions:
    - condition: state
      entity_id: light.hallway_1
      state: 'on'
    - condition:
      - condition: or
        conditions:
        - condition: state
          entity_id: sensor.day_night
          state: 'Night'
        - condition: template
          value_template: '{{is_state("sun.sun","above_horizon") and is_state("sensor.day_night","Day")}}'
action:
  service: homeassistant.turn_off
  entity_id: light.hallway_1
  data:
    transition: 5

Thank you guys. Getting the following in the log though :frowning:

2017-12-12 15:31:23 ERROR (MainThread) [homeassistant.config] Invalid config for [automation]: not a valid value for dictionary value @ data['condition'][1]['conditions'][1]['condition']. Got None
required key not provided @ data['condition'][1]['conditions'][1]['entity_id']. Got None. (See ?, line ?). Please check the docs at https://home-assistant.io/components/automation/

Please post the full automation and the corresponding logs.

OK this is driving me mad. I have used the following script by using the suggestions above and other posts on the forum:

alias: 'Hallway Motion Off'
trigger:
  - platform: state
    entity_id: sensor.hallway_motion_sensor
    to: 'OK'
    for:
      seconds: 30
condition:
  condition: and
  conditions:
    - condition: state
      entity_id: light.hallway_1
      state: 'on'
    - condition:
      - condition: or
        conditions:
        - condition: state
          entity_id: sensor.day_night
          state: 'Night'
        - condition: template
          value_template: '{{ is_state("sun.sun", "above_horizon") }}' and '{{ is_state("sensor.day_night", "Day") }}'
action:
  service: homeassistant.turn_off
  entity_id: light.hallway_1
  data:
    transition: 5

But even though HASS.io check says its valid I get the following error in the log:

2017-12-14 22:04:27 ERROR (SyncWorker_0) [homeassistant.util.yaml] while parsing a block mapping
  in "/config/includes/automation/motion/hallway_motion_off.yaml", line 20, column 11
expected <block end>, but found '<scalar>'
  in "/config/includes/automation/motion/hallway_motion_off.yaml", line 21, column 72
2017-12-14 22:04:27 ERROR (MainThread) [homeassistant.bootstrap] Error loading /config/configuration.yaml: while parsing a block mapping
  in "/config/includes/automation/motion/hallway_motion_off.yaml", line 20, column 11
expected <block end>, but found '<scalar>'
  in "/config/includes/automation/motion/hallway_motion_off.yaml", line 21, column 72

Can anyone spot what I have done wrong? It’s seemingly moaning about the -condition: template.

Thanks

I would change the condition template to:

{{ is_state("sun.sun", "above_horizon") and is_state("sensor.day_night", "Day") }}

Use the “Template” in the developer tools to test your templates. Your template was returning “True and True” instead of merging them into one “True”

I tried this

alias: 'Hallway Motion Off'
trigger:
  - platform: state
    entity_id: sensor.hallway_motion_sensor
    to: 'OK'
    for:
      seconds: 30
condition:
  condition: and
  conditions:
    - condition: state
      entity_id: light.hallway_1
      state: 'on'
    - condition:
      - condition: or
        conditions:
        - condition: state
          entity_id: sensor.day_night
          state: 'Night'
        - condition: template
          value_template: {{ is_state("sun.sun", "above_horizon") and is_state("sensor.day_night", "Day") }}
action:
  service: homeassistant.turn_off
  entity_id: light.hallway_1
  data:
    transition: 5

and get the following error :frowning:

2017-12-15 08:58:52 ERROR (SyncWorker_0) [homeassistant.util.yaml] while parsing a flow mapping
  in "/config/includes/automation/motion/hallway_motion_off.yaml", line 21, column 28
expected ',' or '}', but got '<scalar>'
  in "/config/includes/automation/motion/hallway_motion_off.yaml", line 21, column 65
2017-12-15 08:58:52 ERROR (MainThread) [homeassistant.bootstrap] Error loading /config/configuration.yaml: while parsing a flow mapping
  in "/config/includes/automation/motion/hallway_motion_off.yaml", line 21, column 28
expected ',' or '}', but got '<scalar>'
  in "/config/includes/automation/motion/hallway_motion_off.yaml", line 21, column 65

If I surround the template with ’ ’ I get loads of errors in the log.

OK I have changed the script somewhat and used service_template to include the turn_off as well as a turn_on depending on the time of day:

alias: 'Hallway Motion Off'
trigger:
  - platform: state
    entity_id: sensor.hallway_motion_sensor
    to: 'OK'
    for:
      seconds: 30
condition:
  - condition: state
    entity_id: light.hallway_1
    state: 'on'
action:
  - service_template: >
      {% if is_state('sun.sun', 'above_horizon') and is_state('sensor.day_night', 'Day') %}
        homeassistant.turn_off
      {% elif is_state('sensor.day_night', 'Night') %}
        homeassistant.turn_off
      {% else %}
        homeassistant.turn_on
      {% endif %}
    entity_id: light.hallway_1
      transistion: 5
      brightness: 100

But now get the error:

2017-12-15 15:40:13 ERROR (SyncWorker_0) [homeassistant.util.yaml] mapping values are not allowed here
in “/config/includes/automation/motion/hallway_motion_off.yaml”, line 22, column 18
2017-12-15 15:40:13 ERROR (MainThread) [homeassistant.bootstrap] Error loading /config/configuration.yaml: mapping values are not allowed here
in “/config/includes/automation/motion/hallway_motion_off.yaml”, line 22, column 18

So assume I cannot use the transistion or brightness field. How do I apply these in this context? Thanks

First, there is a typo. Second, you are missing data. It should be

  action:
    - service_template: >
        {% if is_state('sun.sun', 'above_horizon') and is_state('sensor.day_night', 'Day') %}
          homeassistant.turn_off
        {% elif is_state('sensor.day_night', 'Night') %}
          homeassistant.turn_off
        {% else %}
          homeassistant.turn_on
        {% endif %}
     data:
       entity_id: light.hallway_1
       brightness: 100
       transition: 5
2 Likes

Excellent thanks! This now works…

I want this if statement in a script. But someone told me when you turnoff a light you can have a data information like Kelvin, brightness…

You can only turn on a light with colour / brightness data. Turning off a light… well, the light is off, so no data required / accepted. If you want the light to have a certain set of parameters set in it ready for when it is next turned on you would need to send a light.turn_on first with those settings, then follow it up with a simple light.turn_off.

1 Like

Or if you really just want to set the color temperature while turning off the light; you can use the turn on command, setting brightness to 0 and color temp to the desired value. Worth a try.

1 Like