Time condition with exclusion?

I have an automation that can fire between 9 am and 1 hour prior to sunset. That’s the easy part. But I do not want it to between 2:15 pm - 2:45 pm. I added a NOT condition, but I’m pretty sure it will not work correctly. Thoughts?

  condition:
    condition: and
    conditions:
      - condition: time
        after: '09:00:00' # Starts at 9:00 am
      - condition: sun
        before: sunset
        before_offset: '-01:00:00' # Stops firing 1 hour prior to sunset
      - condition: not
        conditions:
          - condition: time # Should not fire between 2:15 pm - 2:45 pm
            after: '14:15:00'
            before: '14:45:00'

I can’t say if your idea will work, but I’d do it a little simpler. Set the time in an or condition and make one from 09:00 (after) to 14:15 (before) and the other one from 14:45 (after). The sunset can stay in place. :slight_smile:

Post the entire automation. We need to see how it’s triggered.

Thanks @paddy0174 and @123.

It is actually a little more complicated because there are a couple of different windows when I do not want the automation to fire. I came up with a way to handle it, but maybe you’ll have a cleaner/better way. Below is the full automation:

# Turn On Jumping Spider
- id: jumping_spider_on
  alias: 'Jumping Spider On'
  initial_state: true
  mode: single
  trigger:
    - platform: state
      entity_id: binary_sensor.front_door_motion 
    - platform: state
      entity_id: binary_sensor.front_porch_motion_sensor_motion 
  condition:
    condition: and
    conditions:
      - condition: time
        after: '09:00:00' # Starts at 9:00 am
      - condition: sun
        before: sunset
        before_offset: '-01:00:00' # Stops firing 1 hour prior to sunset

  action:

    - choose:
      - conditions:
          # Do nothing if Monday between 1:15 pm and 1:45 pm
          - condition: time
            weekday:
              - mon
          - condition: time
            after: '13:15:00'
          - condition: time
            before: '13:45:00'
        sequence:
          - service: script.do_nothing

      - conditions:
          # Do nothing if Tuesday-Friday between 2:15 pm and 2:45 pm
          - condition: time
            weekday:
              - tue
              - wed
              - thu
              - fri
          - condition: time
            after: '14:15:00'
          - condition: time
            before: '14:45:00'
        sequence:
          - service: script.do_nothing

      default:
        - service: switch.turn_on
          entity_id: switch.koogeek_power_strip_jumping_spider

        - delay: '00:02:00'

        - service: switch.turn_off
          entity_id: switch.koogeek_power_strip_jumping_spider

Instead of using choose with time ranges to “do nothing”, just specify those time ranges in the condition (as logical NOT, meaning not during this time range). That’s where all the filtering belongs.

That makes sense. But that brings me back to my original question - How would I use the NOT condition in this scenario?

I think I have it now. What I was unsure of was whether the conditions within the NOT were ANDs or ORs. But discovered that the documentation says:

NOT CONDITION

Test multiple conditions in one condition statement. Passes if all embedded conditions are not valid.

Given that, I added the following conditions. Thoughts?

      - condition: not
        conditions:
          - condition: time
            weekday:
              - mon
          - condition: time
            after: '13:15:00'
          - condition: time
            before: '13:45:00'
      - condition: not
        conditions:
          - condition: time
            weekday:
              - tue
              - wed
              - thu
              - fri
          - condition: time
            after: '14:15:00'
          - condition: time
            before: '14:45:00'

Combine that with your original two conditions and you should have what you want.

1 Like