home-assistant / core

:house_with_garden: Open source home automation that puts local control and privacy first.
https://www.home-assistant.io
Apache License 2.0
71.88k stars 30.11k forks source link

time_pattern platform not working correctly #24691

Closed PeteJB closed 5 years ago

PeteJB commented 5 years ago

Home Assistant release with the issue:

0.94.3

Last working Home Assistant release (if known): Unknown

Operating environment (Hass.io/Docker/Windows/etc.):

Hass.io Hassio.supervisor version: 166 Hassos version: 2.12

Component/platform:

time_pattern

Description of problem: Time Pattern not working reliably.

Time Pattern does not trigger in the intervals specified in the configuration. I have a test configuration below. In this test configuration, the automation triggered for the first time after 55 minutes. It is supposed to configure every 5 minutes. In some instances when testing, I have seen it trigger for the first time after 15 minutes and sometimes after 30 minutes for the first time.

NOTE: When testing this I have confirmed via the states page that the automation was enabled and also that the input boolean had been turned on. I also extracted the home assistant database and validated that the state showed on in the database.

Once it eventually triggers it then seems to trigger every 5 minutes as configured.

To reproduce this problem: 1 Paste the configuration into home assistant. I have it configured via a package.

  1. Restart home assistant

NOTE 2: I confirmed that the automation conditions are correct by also creating some test automations to trigger at specific times based on the same condition as the automation using the time_pattern. These automations all triggered which shows that the condition was satisified. For completeness I have included these further automations below (automation 4a to automation 4g)

Problem-relevant configuration.yaml entries and (fill out even if it seems unimportant):

input_boolean:
  repeat_reminder:
    name: Repeat Electric Blanket Reminder
    initial: off
script:
  temp_reporting_script:
    alias: "temporary reporting script"
    sequence:
      - service: notify.pushover
        data_template:
          message: "Current Temperature: {{ states.sensor.temperature_158d00036b21f9.state }}

                    Plug Status: {{ states. switch.plug_158d0003432704.state }}

                   Time since on Automation: {{ ((as_timestamp(now()) - as_timestamp(states.automation.bedroom_heater_on.attributes.last_triggered)) |int(0)) }}

                   Time since off Automation: {{ ((as_timestamp(now()) - as_timestamp(states.automation.bedroom_heater_off.attributes.last_triggered)) |int(0)) }}"
          title: "{{ title }}"
automation:

  - alias: ' Enable Input Boolean - case 1'
    trigger:
      - platform: time
        at: '14:58:00'
    action:
      - service: input_boolean.turn_on
        data:
          entity_id: input_boolean.repeat_reminder

  - alias: 'test case 1'
    trigger:
      - platform: time_pattern
        hours: '15'
        minutes: '/5'
    condition:
      - condition: state
        entity_id: input_boolean.repeat_reminder
        state: 'on'
    action:
      - service: script.temp_reporting_script
        data:
          title: "Testing input boolean case 1"

  - alias: 'test case 4a'
    trigger:
      - platform: time
        at: '15:02:00'
    condition:
      - condition: state
        entity_id: input_boolean.repeat_reminder
        state: 'on'
    action:
      - service: script.temp_reporting_script
        data:
          title: "Testing input boolean case 4a"

  - alias: 'test case 4b'
    trigger:
      - platform: time
        at: '15:07:00'
    condition:
      - condition: state
        entity_id: input_boolean.repeat_reminder
        state: 'on'
    action:
      - service: script.temp_reporting_script
        data:
          title: "Testing input boolean case 4b"

  - alias: 'test case 4c'
    trigger:
      - platform: time
        at: '15:12:00'
    condition:
      - condition: state
        entity_id: input_boolean.repeat_reminder
        state: 'on'
    action:
      - service: script.temp_reporting_script
        data:
          title: "Testing input boolean case 4c"

  - alias: 'test case 4d'
    trigger:
      - platform: time
        at: '15:17:00'
    condition:
      - condition: state
        entity_id: input_boolean.repeat_reminder
        state: 'on'
    action:
      - service: script.temp_reporting_script
        data:
          title: "Testing input boolean case 4d"

  - alias: 'test case 4e'
    trigger:
      - platform: time
        at: '15:22:00'
    condition:
      - condition: state
        entity_id: input_boolean.repeat_reminder
        state: 'on'
    action:
      - service: script.temp_reporting_script
        data:
          title: "Testing input boolean case 4e"

  - alias: 'test case 4f'
    trigger:
      - platform: time
        at: '15:27:00'
    condition:
      - condition: state
        entity_id: input_boolean.repeat_reminder
        state: 'on'
    action:
      - service: script.temp_reporting_script
        data:
          title: "Testing input boolean case 4f"

  - alias: 'test case 4g'
    trigger:
      - platform: time
        at: '15:32:00'
    condition:
      - condition: state
        entity_id: input_boolean.repeat_reminder
        state: 'on'
    action:
      - service: script.temp_reporting_script
        data:
          title: "Testing input boolean case 4g"

Traceback (if applicable):

Additional information:

amelchio commented 5 years ago

At what timestamp did it actually trigger?

Does it work as expected without the input_boolean condition? Please give a minimal example that fails and please do not list a lot of stuff that works as expected.

PeteJB commented 5 years ago

Thanks for the comment. In this instance it actually triggered at 15:55, although that's not consistent with each time I test it. Sometimes it has triggered at 15 minutes past the hour, sometimes at 35 minutes past the hour. There doesn't seem to be a pattern.

Minimal code that fails:

input_boolean:
  repeat_reminder:
    name: Repeat Electric Blanket Reminder
    initial: off

script:
  temp_reporting_script:
    alias: "temporary reporting script"
    sequence:
      - service: notify.pushover
        data_template:
          message: "Current Temperature: {{ states.sensor.temperature_158d00036b21f9.state }}

                    Plug Status: {{ states. switch.plug_158d0003432704.state }}

                   Time since on Automation: {{ ((as_timestamp(now()) - as_timestamp(states.automation.bedroom_heater_on.attributes.last_triggered)) |int(0)) }}

                   Time since off Automation: {{ ((as_timestamp(now()) - as_timestamp(states.automation.bedroom_heater_off.attributes.last_triggered)) |int(0)) }}"
          title: "{{ title }}"

automation:

  - alias: ' Enable Input Boolean - case 1'
    trigger:
      - platform: time
        at: '14:58:00'
    action:
      - service: input_boolean.turn_on
        data:
          entity_id: input_boolean.repeat_reminder

  - alias: 'test case 1'
    trigger:
      - platform: time_pattern
        hours: '15'
        minutes: '/5'
    condition:
      - condition: state
        entity_id: input_boolean.repeat_reminder
        state: 'on'
    action:
      - service: script.temp_reporting_script
        data:
          title: "Testing input boolean case 1"
PeteJB commented 5 years ago

Also found this issue which might be relevant: https://github.com/home-assistant/home-assistant/issues/21547

PeteJB commented 5 years ago

I'm retesting with using the persistent notification service instead of the script to make something that is easily reproducible. The time pattern is set to kick off in 30 minutes.

PeteJB commented 5 years ago

OK. I've simplified the config to reproduce the problem. In this instance it eventually triggered at 18:35 when it should have triggered at 18:00 and every 5 minutes thereafter. Here is the simplified configuration. Note: I restart Home Assistant after inputting the configuration.


input_boolean:
  repeat_reminder:
    name: Repeat Electric Blanket Reminder
    initial: off
automation:

  - alias: ' Enable Input Boolean - case 1'
    trigger:
      - platform: time
        at: '17:58:00'
    action:
      - service: input_boolean.turn_on
        data:
          entity_id: input_boolean.repeat_reminder
  - alias: 'test case 6'
    trigger:
      - platform: time_pattern
        hours: '18'
        minutes: '/5'
    condition:
      - condition: state
        entity_id: input_boolean.repeat_reminder
        state: 'on'
    action:
      - service: persistent_notification.create
        data:
          message: "Test Message from test case 6"
          title: "Test Message"
amelchio commented 5 years ago

Okay, I found the bug a submitted a fix.

To elaborate, a minimal example means to remove the condition and the input_boolean since they are irrelevant for showing the bug. When you leave that to me, there is a high chance that I will work on something else instead ... like we did in the previous issue that you found.