Problem with rule firing when I don't think it should

  - id: heat_left_on
    alias: 'Notify if heat left on'
    trigger:
      platform: time
      minutes: '/5'
      seconds: 00
    condition:
      condition: and
      conditions:
        - condition: state
          entity_id: input_boolean.heat_detected
          state: 'on'
        - condition: time
          before: '08:00:00'
          after: '17:00:00'
    action:
      - service: notify.hawaii_ron_notifier
        data:
          title: 'Heat Left On'
          message: 'Left Heat on at Desk - Switching off'
      - service: shell_command.turn_off_heat

It is currently 10am but by rule is firing. I have it set to fire before 8am and after 5pm when heat is detected. But it is firing at 10am when I enable the heat.

Am I using the time condition incorrectly? It should be an “and” condition set so both need to be true to fire, right? Not sure what is wrong with this automation.

It generally looks good to me, but you might want to try one change. Conditions are naturally ANDs, so there is no need for the ’ condition: and’ condition.

Conditions are ANDs and triggers are ORs by default

This condition doesn’t work the way you think it does. It is performing an and on that condition, not or. In order to do a before / after in this fashion, you need to use this condition:

        - condition: or
          conditions:
          - condition: time
            before: '08:00:00'
          - condition: time
            after: '17:00:00'

so full context would be:

  - id: heat_left_on
    alias: 'Notify if heat left on'
    trigger:
      platform: time
      minutes: '/5'
      seconds: 00
    condition:
      condition: and
      conditions:
        - condition: state
          entity_id: input_boolean.heat_detected
          state: 'on'
        - condition: or
          conditions:
          - condition: time
            before: '08:00:00'
          - condition: time
            after: '17:00:00'
    action:
      - service: notify.hawaii_ron_notifier
        data:
          title: 'Heat Left On'
          message: 'Left Heat on at Desk - Switching off'
      - service: shell_command.turn_off_heat

@petro Thanks…I will try that.

The documentation should be fixed then. Because it states that time can be done this way.

The example given status that “the condition window is from 3pm to 2am”.

I’ll test that and if it works I will try to see if I can put a fix request for the documentation if I can figure out how to do that.

I mean, it may have changed… I’m not always on top of the changes that occur to things.

EDIT: I may be miss remembering and thinking of the sun platform. Either way, that condition will still work for you regardless.

@petro Looks like that fixed it. I need to test that it still works when in the window but it is no longer firing outside of the window.

If I edit the page do you know if I should just add a NOTE about my findings or should I change the page entirely. I feel like it should work the way they are stating so I prefer to just add a note that I found this issue in the current version.
I suppose an issue ticket should be created also but I suspect since there is an easy workaround that will never get implemented anyway.

1 Like

First, I should have tested before replying because the rule still doesn’t work. Now it never fires.

One stupid mistake. I needed OR not AND. (BUT read on because it still does NOT work)

I want the outside time range not the inside. So I need to switch to OR condition.
AND condition only works for inside a time range
after:08:00 AND before 17:00

But I wanted the trigger to fire when the it was outside the range so I need to negate and switch to OR (negate means switch before and after)
before:08:00 OR after 17:00

So I simplified my rules to the following tests.
test1 was to check heat…it’s working

  # This test should NOT fire as it is 11:30AM which is NOT before 8 or after 5pm
  - id: heat_left_on_test2
    alias: 'Notify if heat left on test 2'
    trigger:
      platform: time
      minutes: '/5'
      seconds: 00
    condition: or
    conditions:
      - condition: time
        before: '08:00:00'
      - condition: time
        after: '17:00:00'
    action:
      - service: notify.hawaii_ron_notifier
        data:
          title: 'Heat Left On Test 2'
          message: 'Left Heat on at Desk Test 2'

  # This test should fire as it is before 12 (noon) so the OR should fire
  - id: heat_left_on_test3
    alias: 'Notify if heat left on test 3'
    trigger:
      platform: time
      minutes: '/5'
      seconds: 00
    condition: or
    conditions:
      - condition: time
        before: '12:00:00'
      - condition: time
        after: '17:00:00'
    action:
      - service: notify.hawaii_ron_notifier
        data:
          title: 'Heat Left On Test 3'
          message: 'Left Heat on at Desk Test 3'

Test 3 which should fire does not.

Could it be that the time condition doesn’t honor my time zone?
Is there a way to display what time it is using.
I have time and UTC displayed as sensors and they are both correct.

it should use your local time zone, is that set up properly?

Yes as I mentioned I have time displayed as a sensor and it has the correct local time. Difficult to determine what it is doing wrong. The test is pretty simple at this point.

Is HASS and the OS that HASS is running on set to the same timezone? If they are different, even if HASS is using the correct timezone, it can mess up timed events.

Yes they are. Thanks for the feedback.

OK I finally figured it out. I was on the right track with the switch to OR but I had a small indentation issue and didn’t see it wasn’t loading up on config.

So anyone else trying to fire a trigger based on time outside of a range here is the correct config.

It seems inefficient to fire every 5 min to check for a state that will rarely be true. I wonder if there is a better way to implement this. But for now I finally have it working.

  # if heat is on before 8am or after 5pm....report it and turn it off
  - id: heat_left_on
    alias: 'Notify if heat left on'
    trigger:
      platform: time
      minutes: '/5'
      seconds: 00
    condition:
      - condition: state
        entity_id: input_boolean.heat_detected
        state: 'on'
      - condition: or
        conditions:
          - condition: time
            before: '08:00:00'
          - condition: time
            after: '17:00:00'
    action:
      - service: notify.hawaii_ron_notifier
        data:
          title: 'Heat Left On'
          message: 'Left Heat on at Desk - Switching off'
      - service: shell_command.turn_off_heat

So basically you want the automation actions to run when input_boolean.heat_detected goes on and time is between 17:00:00 and 08:00:00, or you want them to run at 17:00:00 if input_boolean.heat_detected is on.

And, BTW, the time condition you originally had is perfectly valid and does what you want. The time condition code is specifically written to handle before and after specifying a time period that spans midnight.

This is what I suggest:

  - id: heat_left_on
    alias: If heat left on notify and turn off
    trigger:
      - platform: time
        at: '17:00:00'
      - platform: state
        entity_id: input_boolean.heat_detected
        to: 'on'
    condition:
      - condition: time
        after: '17:00:00'
        before: '08:00:00'
      - condition: state
        entity_id: input_boolean.heat_detected
        state: 'on'
    action:
      - service: notify.hawaii_ron_notifier
        data:
          title: 'Heat Left On'
          message: 'Left Heat on at Desk - Switching off'
      - service: shell_command.turn_off_heat

Any idea why my initial post wasn’t working then? I think the original post should have worked exactly as it was.

I may have a thought. I think it is possible I had a time zone issue initially and when I fixed that my configuration was altered away from my initial setup. I think I has switched from the two values to the “condition: and” and that messed me up. Because with the “and condition” it breaks and needs to be changed to an “or condition”. But with before and after in the same section it should work as a range. The documentation said it could pass over a 24hr period.

At first I thought your trigger wouldn’t catch if I turn on the heat after 5pm but then I realized someone mentioned that triggers are “or” and conditions are “and”. So your configuration will work and is a lot more efficient. I was thinking I needed to poll because I couldn’t trigger on both time and heat on/off. Now I know triggers will fire if ANY are true and that is much easier.

I guess I will try the time range again to see if it works. I will give this a try tomorrow.

Thanks!

I agree - the automation in your original post should have worked if time-zones were set correctly.

And, yes, you could say triggers are “or”. But really, triggers are events, not conditions. They happen when something changes (like a state, time, etc.) So effectively they are instantaneous. When you think of them that way it’s really impossible for two events to happen at exactly the same time (especially given the fact that HA processes events individually.) Therefore, it doesn’t make sense to say event A and event B happened simultaneously. It only makes sense to say event A or event B happened. Or, to put that another way, the actions run when any of the trigger events occur.

I also like to think of triggers and conditions this way - trigger (events) decide when the actions should run, and conditions decide if the actions should run (when triggered.)

1 Like