Need Help with Condition Time trigger [SOLVED]

Having trouble getting a few condition time triggers to work. I can trigger the activity manually, but it never gets triggered base on the time rule. Any help would be greatly appreciated.

What I am trying to do is power off an Onkyo receiver if no motion is detected in the room for X time.

  - alias: 'Onkyo Receiver Auto Off - Weekdays'
    trigger:
      platform: state
      entity_id: media_player.onkyo_receiver
      state: 'on'
    condition:
      condition: and
      conditions:
        - condition: time
          after: '21:00:00'
          before: '08:00:00'
          weekday:
            - sun
            - mon
            - tue
            - wed
            - thu
        - condition: state
          entity_id: binary_sensor.room_1_motion
          state: 'off'
          for:
            hours: 2
    action:
      service: media_player.turn_off
      entity_id: media_player.onkyo_receiver

You will have to split up the time condition as it can’t be after 21:00 and before 08:00 at the same time. Midnight is the reference point for the time condition.

This should do the trick:

- alias: 'Onkyo Receiver Auto Off - Weekdays'
  trigger:
    platform: state
    entity_id: media_player.onkyo_receiver
    state: 'on'
  condition:
    condition: and
    conditions:
      - condition: or
        conditions:
          - condition: time
            after: '21:00:00'
            weekday:
              - sun
              - mon
              - tue
              - wed
              - thu
          - condition: time
            before: '08:00:00'
            weekday:
              - sun
              - mon
              - tue
              - wed
              - thu
      - condition: state
        entity_id: binary_sensor.room_1_motion
        state: 'off'
        for:
          hours: 2
  action:
    service: media_player.turn_off
    entity_id: media_player.onkyo_receiver
2 Likes

David,
Thanks for the info. I will give that a shot.

For the record, I also tried the below which didn’t work. Do I have always explicitly specify the ‘or’ close for after/before?
- condition: time
after: ‘00:00:00’
before: ‘08:00:00’

I am getting error with the updated condition clause:
ERROR (Thread-1) [homeassistant.util.yaml] duplicate key: “condition”

Post your config, using the correct formatting (as explained in the blue box at the top of every forum page), and somebody should be able to help you.

1 Like

Here is that section of my config:

automation:
  - alias: 'Onkyo Receiver Auto Off - Weekdays'
    hide_entity: True
    trigger:
      platform: state
      entity_id: media_player.onkyo_receiver
      state: 'on'
    condition:
      condition: and
      conditions:
        - condition: or
          condition:
            - condition: time
              after: '21:00:00'
              weekday:
                - sun
                - mon
                - tue
                - wed
                - thu
            - condition: time
              before: '08:00:00'
              weekday:
                - sun
                - mon
                - tue
                - wed
                - thu
        - condition: state
          entity_id: binary_sensor.sensor01_motion
          state: 'off'
          for:
            hours: 2
    action:
      service: media_player.turn_off
      entity_id: media_player.onkyo_receiver

Also, is it recommended to use the motion sensor ‘off’ time or should I use the ‘last tripped’ time? If last tripped time is better, please also provide pointers on how it should be called.

        - condition: or
          condition:
            - condition: time

The second line in this sample is missing an ‘s’, so it needs to be:

        - condition: or
          conditions:
            - condition: time
1 Like

Thanks David. That works. But I still cannot get it to trigger automatically. What am I missing?

Well, if your automation is set up as shown above, then one of your conditions isn’t met as today is Friday (at least, it is in my timezone).

Plus, if you restarted Home Assistant after fixing the automation, I can only assume that the counter for your state condition was reset, meaning the motion sensor hasn’t been in the ‘off’ state for 2 hours yet.

David,
I removed 2 hour sensor constraint and was expecting receiver to be turned off immediately after stop moving. But nothing happened. I’ll keep at it until I figure which condition is not met.

It seems HA is not evaluating multiple conditions. Can some post or provide a line with multiple conditions and sensors so I can test? I am not having any success with this.

I think I figured this one out as well. It seem HA automation is triggered once for every state change of the trigger item. In my case, the Onkyo receiver is usually turned on after 08:00 but before 21:00. This attempts to trigger the automation, but since the time condition is NOT met nothing happens. The receiver is kept on all day without another state change, i.e. from off to on, so the automation is never triggered again.

This is very important information to know as it helps decide which item to use to trigger an automation when other constraints are a factor. Since the motion sensor changes state most frequently, it is now used as the trigger item.

This is the working code:

automation:
  - alias: 'Onkyo Receiver Auto Off - Weekdays'
    trigger:
      platform: state
      entity_id: binary_sensor.sensor01_motion
      from: 'on'
      to: 'off'
      state: 'off'
      for:
        hours: 2
    condition:
      condition: and
      conditions:
        - condition: state
          entity_id: media_player.onkyo_receiver
          state: 'on'
        - condition: or
          conditions:
            - condition: time
              before: '08:00:00'
              weekday:
                - mon
                - tue
                - wed
                - thu
                - fri
            - condition: time
              after: '21:00:00'
              weekday:
                - sun
                - mon
                - tue
                - wed
                - thu
    action:
      service: media_player.turn_off
      entity_id: media_player.onkyo_receiver

There is still a problem if the receiver is turned on in the morning after 8:00 and no one returns to the room after 21:00. However, that is unlikely in my case.

@fanaticDavid thanks for the help. You actually pointed me in the right direction.

@Tinkerer thanks for the formatting pointer. Sometimes you don’t see things that are directly in front of you until someone else points it out.

2 Likes