Simple "AND" statement

When motion sensor one and two don’t show motion for two minutes I want it to turn off the light, this is what i have now but don’t understand why its not working?

- alias: Turn off Garage light
  trigger: 
     platform: state
   condition:
     condition: and
     conditions:
       - condition: state
         entity_id: binary_sensor.inside_motion_1
         state: off
         for: 00:02:00
       - condition: state
         entity_id: binary_sensor.inside_motion_2
         state: off
         for: 00:02:00
  action:
    service: homeassistant.turn_off
    entity_id: switch.g_inlight

Configuring functions is much easier in node red. In case you are interested I can tell you how to do it in Node Red.

How does node red work?

Try to use: ‘off’ for your conditions.

You need some sort of trigger

Error loading /config/configuration.yaml: while parsing a block mapping
in “/config/automations.yaml”, line 466, column 3
expected , but found ‘’
in “/config/automations.yaml”, line 469, column 4

do yall see some where the spacing is off?

- alias: Turn off Garage light
  trigger: 
     platform: state
   condition:
     condition: and
     conditions:
       - condition: off
         entity_id: binary_sensor.inside_motion_1
         state: off
         for: 00:02:00
       - condition: off
         entity_id: binary_sensor.inside_motion_2
         state: off
         for: 00:02:00
  action:
    service: homeassistant.turn_off
    entity_id: switch.g_inlight

that’s right.

You’ve defined nothing to trigger the automation. How were you planning to execute it? If you use platform: state then you also need to supply an entity_id.

The second version you posted is invalid. There’s no such thing as condition: off.

Highly subjective statement.

I find NodeRed extremely unintuitive, confusing, and unnecessarily complex. I prefer to use the built in YAML automation engine or AppDaemon for more complex tasks.

Maybe you don’t find writing python or yaml as easy as others, but NodeRed certainly isn’t for everyone either.

As 123 pointed out, you didn’t properly define a condition.

Automations have to be triggered, meet a set of conditions, then do an action.
Sometimes that condition can be blank and on every trigger it does the action, but it needs a trigger to do the action no matter what.

- id: turn_off_garage_light
  alias: Turn off Garage light
  trigger:
    - platform: state
      entity_id: binary_sensor.inside_motion_1
      to: 'off'
      for: '00:02:00'
    - platform: state
      entity_id: binary_sensor.inside_motion_2
      to: 'off'
      for: '00:02:00'
  condition:  
    condition: and
    conditions:
      - condition: state
        entity_id: binary_sensor.inside_motion_1
        state: 'off'
      - condition: state
        entity_id: binary_sensor.inside_motion_2
        state: 'off'
  action:
    - service: homeassistant.turn_off
      entity_id: switch.g_inlight

The above code, adapted from what you had before, will trigger when either sensor if off for two minutes, only continue if both are off, then do your action.

The shortfall of this is that if one sensor is off for 2 minutes and other other is off for 10 seconds, the action will still occur. If that acceptable? Or you you truly need both off for 2 minutes before turning off the light?

1 Like

Phew. So it’s not just me then? I am yet to find anything I want to do that I can’t do in yaml.

2 Likes

Definitely not just you.

1 Like

Sorry for late response: So with node red you have have to make you have to make state nodes for each of your motion sensors. They will turn on and off automatically when the state changes. Link to states to a set function for example: set.flow(“senosr1”, “on”). Now you can call those function within any process just by copy pasting nodes. For instance if you want your sensor 2 to open the garage door as well you don’t need to write the whole thing from scratch all you do you drop the get function between the two nodes. Node red is easier to implement because you can reuse a function just by copy/paste and drag drop. Hope it helps.

yeah both would need to be at least 2 mins, because I may be in another part of the garage and the lights may go out. Something like (if sensor 1 & sensor 2 = “off” for 2 mins then turn off lights)

I was googling a solution again and ended back here…

does this look right?

The “or” to me means if one happens then turn off the lights.

- id: turn_off_garage_light
  alias: Turn off Garage light
  trigger:
    - platform: state
      entity_id: binary_sensor.inside_motion_1
      to: 'off'
      for: '00:02:00'
    - platform: state
      entity_id: binary_sensor.inside_motion_2
      to: 'off'
      for: '00:02:00'
    condition:
      - condition: or
        conditions:
          - condition: state
            entity_id: binary_sensor.inside_motion_1
             to: 'off'
             for: '00:02:00'
          - condition: state
            entity_id: binary_sensor.inside_motion_2
            to: 'off'
            for: '00:02:00'
  action:
    - service: homeassistant.turn_off
      entity_id: switch.g_inlight

What is the purpose of the OR condition in your example?

The automation can only be triggered by either of the two motion sensors remaining off for at least 2 minutes. The condition checks if either of the two motion sensors is off for at least 2 minutes. Seems like a redundant check.


EDIT

This is functionally equivalent to the automation you posted:

- id: turn_off_garage_light
  alias: Turn off Garage light
  trigger:
    - platform: state
      entity_id:
        - binary_sensor.inside_motion_1
        - binary_sensor.inside_motion_2
      to: 'off'
      for: '00:02:00'
  action:
    - service: homeassistant.turn_off
      entity_id: switch.g_inlight

If either of the two motion sensors is off for at least 2 minutes, then the switch is turned off. If that’s not the behavior you want then the example you posted is incorrect.

If you want to turn off the switch only if both motion sensors have been off for at least 2 minutes, convert your example’s condition from or to and.

Your first automation from 2 years ago, was actually almost, what you wanted, except:

  1. It was missing the trigger, which was suggested in the following posts. You put more ore less the same logic in trigger and condition. The difference is that “trigger:” is combine by “OR”, whereas condition is combined by “AND”. So there is no need for your explicit “AND” in your initial conditions.
    You need both, because the automation should trigger (and evaluate the condition) whenever either one of the sensors is off for 2 min, but the action should only execute when both sensors are off for 2 min. You could even leave out the time-constraint in the trigger. It just determines how often the condition is evaluated.
  2. The indentation of “condition:” is incorrect. It should be the same level as “trigger:”. In your latest configuration the “to:” and “for:” are also indented wrong.

I was reading and thinking this person has the same issue I do :rofl:

- alias: 'Motion Lighting Garage turn light off'
  trigger:
      - condition: or
        conditions:
          - condition: state
            entity_id: binary_sensor.inside_motion_1
            to: 'off'
            for: '00:02:00'
          - condition: state
            entity_id: binary_sensor.inside_motion_2
            to: 'off'
            for: '00:02:00'
  condition:
      - condition: and
        conditions:
          - condition: state
            entity_id: binary_sensor.inside_motion_1
            to: 'off'
            for: '00:02:00'
          - condition: state
            entity_id: binary_sensor.inside_motion_2
            to: 'off'
            for: '00:02:00'
  action:
  - service: switch.turn_off
    entity_id: switch.g_inlight    

Ok this passes validation but haven’t tested yet.

Create a 3rd motion sensor

  - platform: template
    sensors:
      inside_motion_all:
        friendly_name: "inside_motion_all"
        value_template: >
          {{ is_state('binary_sensor.inside_motion_1','on') or is_state('binary_sensor.inside_motion_2','on')   }}
        device_class: motion

Then it is simply

- alias: 'Motion Lighting Garage turn light off'
  trigger:
  - platform: state
    entity_id: binary_sensor.inside_motion_all
    to: 'off'
    for: '00:02:00'
  action:
  - service: switch.turn_off
    entity_id: switch.g_inlight    

Really?

I would be surprised if that were true.

There’s a few things why I say that.

  1. As far as I know you can’t put condition statements in the trigger block.

  2. And in the condition block “to:” isn’t an option. It should be “state:”

  3. And your indentations are all over the place.

Not really. Here’s the thing when using “Check Congifuration” it might run and report ‘configuration valid’ but that might not actually be the full story. It may have more to say and it will state it with a Persistent Notification informing you that something is indeed wrong and you should check the Log for more details.

Case in point is the code you posted. Anyone who has seen an automation or two knows that its trigger is dead wrong. Running `Check Configuration’ produces the following persistent notification and error message in the Log:

Screenshot from 2021-08-19 13-07-07

Logger: homeassistant.config
Source: config.py:445
First occurred: 1:00:00 PM (1 occurrences)
Last logged: 1:00:00 PM

Invalid config for [automation]: extra keys not allowed @ data[‘condition’][0][‘conditions’][0][‘to’]. Got None required key not provided @ data[‘condition’][0][‘conditions’][0][‘state’]. Got None required key not provided @ data[‘trigger’][0][‘platform’]. Got None. (See ?, line ?).

So, no, it didn’t truly pass config check despite the ‘configuration valid’ message you saw. Chalk it up to one of Home Assistant’s quirks.