Condition completely stops automation - even when true

I’ve made an automation that turns on the closet light at 10 percent between 10pm and 7am, and it works like a champ until I add the condition to make sure the light is off before triggering the action. What have I done wrong here? I’ve tried it several different ways to no avail.

Any tips?


alias: Closet Light On Low With Motion
description: ''
trigger:
  - type: motion
    platform: device
    device_id: 3e6956c9621109e2b9d7dc0616d055dd
    entity_id: binary_sensor.motion_sensor_ii_41_5e_41_motion
    domain: binary_sensor
condition:
  - condition: time
    after: '22:00:00'
    before: '06:59:59'
  - condition: and
    conditions:
      - condition: device
        type: is_off
        device_id: 14e4fb7d11c38b24324c9381636b7f83
        entity_id: light.switchlinc_dimmer_4e_d7_f9
        domain: light
action:
  - type: turn_on
    device_id: 14e4fb7d11c38b24324c9381636b7f83
    entity_id: light.switchlinc_dimmer_4e_d7_f9
    domain: light
    brightness_pct: 10
mode: single



alias: Closet Light On Low With Motion
description: ''
trigger:
  - type: motion
    platform: device
    device_id: 3e6956c9621109e2b9d7dc0616d055dd
    entity_id: binary_sensor.motion_sensor_ii_41_5e_41_motion
    domain: binary_sensor
condition:
  - condition: and
    conditions:
      - condition: device
        type: is_off
        device_id: 14e4fb7d11c38b24324c9381636b7f83
        entity_id: light.switchlinc_dimmer_4e_d7_f9
        domain: light
      - condition: time
        after: '22:00:00'
        before: '06:59:00'
action:
  - type: turn_on
    device_id: 14e4fb7d11c38b24324c9381636b7f83
    entity_id: light.switchlinc_dimmer_4e_d7_f9
    domain: light
    brightness_pct: 10
mode: single


Although I am not sure it would cause the automation to not run so probably isn’t your fix, but for your reference conditions are always ‘and’ and therefore you should drop the ‘and’ and simply list the conditions as they all have to be true for the automation to pass.

I would try it like this:

condition:
  - condition: time
    after: '22:00:00'
    before: '06:59:59'
  - condition: state
    entity_id: light.switchlinc_dimmer_4e_d7_f9
    state: 'off'

My yaml now reads:


alias: Closet Light On Low With Motion
description: ''
trigger:
  - type: motion
    platform: device
    device_id: 3e6956c9621109e2b9d7dc0616d055dd
    entity_id: binary_sensor.motion_sensor_ii_41_5e_41_motion
    domain: binary_sensor
condition:
  - condition: time
    after: '22:00:00'
    before: '06:59:00'
  - condition: state
    entity_id: light.switchlinc_dimmer_4e_d7_f9
    state: 'off'
action:
  - type: turn_on
    device_id: 14e4fb7d11c38b24324c9381636b7f83
    entity_id: light.switchlinc_dimmer_4e_d7_f9
    domain: light
    brightness_pct: 10
mode: single

And I get the following error, saying that the switch state isn’t off when it clearly is:

Very strange.

If you pull up that entity in developer tools > states what does it show as state. No typo in entity ID?

Look at the entity in developer tools and ensure that it’s not on, but dimmed down almost all of the way.

1 Like

No typo in entity ID. Here’s the current state, and the automation still doesn’t work.

I do not know why the condition fails to see the state is “off”, when it is. The only 2 suggestions I can offer are to try and build the automation from the GUI, and see if the trigger condition and action sections look the same as your YAML. Otherwise hopefully someone else with more experience will jump in.

I believe this is what’s happening

When you set a condition like this

      - condition: time
        after: '22:00:00'

HA understands it is true between 22:00 and 00:00

When you set this one:

      - condition: time
        before: '06:59:00'

HA understands condition will be true from 00:00 to 06:59.

When you set both like that

      - condition: time
        after: '22:00:00'
        before: '06:59:00'

HA understands condition will only be true when time is between 22:00 - 00:00 AND between 00:00 - 06:59, which can never be true. Both conditions can’t be true at the same time.

Right way of doing it

condition:
- condition: or
  conditions:
      - condition: time
        after: '22:00:00'
      - condition: time
        before: '06:59:00'

I thought that too, but from HA docs, time example:Conditions - Home Assistant

condition:
  alias: "Time 15~02"
  condition: time
  # At least one of the following is required.
  after: "15:00:00"
  before: "02:00:00

Time condition windows can span across the midnight threshold if both after and before keys are used. In the example above, the condition window is from 3pm to 2am.

So, I am confused; not for the first time.

Try this version:

alias: Closet Light On Low With Motion
description: ''
variables:
  light: light.switchlinc_dimmer_4e_d7_f9
trigger:
  - platform: state
    entity_id: binary_sensor.motion_sensor_ii_41_5e_41_motion
    to: 'on'
condition:
  - "{{ now().hour < 7 or now().hour >= 22 }}"
  - "{{ is_state(light, 'off') }}"
action:
  - service: light.turn_on
    target:
      entity_id: "{{ light }}"
    data:
      brightness_pct: 10

Thank you. I will and will let you know how it works out! Unfortunately, it’ll have to wait a day because I’m not at home.

Again, this failed for the same reason. The light does not return ‘off’ as its status. I just tested it with a different entity being off, and the automation worked. I’ll try a factory reset on this dimmer and see if that solves the problem.

After all these posts, it turns out that a factory reset of the switch and then re-pairing it with the hub solved the problem. So, I’m back to my original automation.

Thanks for all the help!