Time trigger/condition not acting as expected

Hi,
I have a basic automation to turn on/off a plug for a coffee machine at some times given by input_datetime ( on == 6 am and off == 4 pm). The two times serve as a time trigger and there is a choose action with conditions checking if we are within the on or off time window.

Turning on works fine. However, turning off does not – instead the turn on choose branch gets chosen.

The triggers are working ok

trigger:
  - platform: state
    entity_id: group.inhabitants
  - platform: time
    at: input_datetime.plug_coffee_on
  - platform: time
    at: input_datetime.plug_coffee_off

But when the coffee_off time triggers, the turn on conditions are satisifed:

action:
  - choose:
      - alias: Turn it on
        conditions:
          - condition: and
            conditions:
              - condition: time
                after: input_datetime.plug_coffee_on
                before: input_datetime.plug_coffee_off
              - condition: state
                entity_id: group.inhabitants
                state: 'on'

instead of the turn off conditions

         - condition: or
            conditions:
              - condition: not
                conditions:
                  - condition: time
                    after: input_datetime.plug_coffee_on
                    before: input_datetime.plug_coffee_off
              - condition: state
                entity_id: group.inhabitants
                state: 'off'

What am I messing up? Is it that the trigger shoots at 16:00:00 and this is still satisfying the time condition because it’s not 16:00:01?

Can you elaborate on how you want this to work because your automation’s conditions allow for more than just turning the switch on/off at specific times.

  • It appears to turn on the switch at a specific time but only if someone is present (group.inhabitants is on). It will also turn it on when presence is detected within the time range.

  • It appears to turn off the switch at a specific time, regardless of presence. It will also turn it off if no presence is detected within the time range.

Is that correct?

That’s right: ‘group.inhabitants’ is a boolean which is on whenever there is at least one person home, tracking mobile phones.

I want the coffee to only turn on if somebody is actually present (i.e. trigger at coffee_on if someone is present or when presence turns to on if inside the on/off time range). Turn to off should happen at any time if group.inhabitants turns to off or the coffee_off time is reached, regardless of presence.

The logic with turning on and turning off when triggered by presence works fine. The only problem is the seemingly simple trigger at time coffee_off when presence is on, which for some reason seems to satisfy the turn_on conditions and executes the on actions.

Here’s how I would do it:

- alias: Coffee Pot Controller
  trigger:
    - platform: state
      entity_id: group.inhabitants
    - platform: time
      at: input_datetime.plug_coffee_on
    - platform: time
      at: input_datetime.plug_coffee_off
  action:
    - variables:
        t: "{{ (now().time()|string)[:5] }}"
        start: "{{ states('input_datetime.plug_coffee_on')[:5] }}"
        end: "{{ states('input_datetime.plug_coffee_off')[:5] }}"
        in_range: "{{ start <= t <= end }}"
        present: "{{ is_state('group.inhabitants', 'on') }}"
    - choose:
        - conditions: "{{ trigger.platform == 'time' and present }}"
          sequence:
            - service: "switch.turn_{{'on' if t == start else 'off'}}"
              entity_id: switch.coffee
        - conditions: "{{ trigger.platform == 'state' and in_range }}"
          sequence:
            - service: "switch.turn_{{ trigger.to_state_state }}"
              entity_id: switch.coffee

If it’s triggered by time, and someone is present, it turns on the switch if the current time is equal to the start time (otherwise, it turns it off).

If it’s triggered by state, and the current time is within the time range, it turns on the switch if there’s presence (otherwise, it turns it off).

Just to close this off: this was a bud in the time condition with input_datetimes that was fixed in ha 2021.6

The example I posted doesn’t use a Time Condition so I don’t see how it could have been affected by the alleged bug.

I meant that my problem was because of the bug: I was using the time condition and was having the issue. Once that got fixed, it works fine. Your code was fine, but I am trying to get away from doing all these date manipulations in templates for readability, now that there are inbuilt ways of doing it.

Thanks!

If you didn’t use the alternative I offered, what did you do between April 10, when you created this topic, to the first Wednesday in June when version 2021.6.0, with the correction, was released?