How to stop time pattern trigger

Hi everybody,

I’m running an automation with a time pattern every 20 minutes between 14:59:59 and 22:00:01.
Every 20 minutes, the automation will trigger. The action will follow if the conditions are met. Because the conditions are relying on the cloud cover percentage and the time offset from sunset, the action will trigger another time every single day. Is it possible to stop the time pattern trigger once the scene state is true? Now, when the scene is activated, the automation will still trigger every 20 minutes. To avoid problems, I made and action that when the scene is true, the automation will stop. But then again after 20 minutes, the automation triggers again. Is there anyway to stop the triggering? My logs are full with unnecessary logfiles from the automation.

alias: "Woonkamer aan "
description: Livingroom on. Cloud coverage 80-100 sunset offset -02:00:00. CC 60-80 sunset offset -01:00:00. CC 30-60 sunset offset -00:30:00. CC 0-30 sunset
trigger:
  - platform: time_pattern
    minutes: /20
condition:
  - condition: and
    conditions:
      - condition: time
        after: "14:59:59"
        before: "22:00:01"
      - condition: or
        conditions:
          - condition: and
            conditions:
              - condition: sun
                after: sunset
                after_offset: "-02:00:00"
              - type: is_value
                condition: device
                device_id: 
                entity_id: sensor.home_cloud_cover
                domain: sensor
                above: 80
                below: 100
          - condition: and
            conditions:
              - condition: sun
                after: sunset
                after_offset: "-01:00:00"
              - type: is_value
                condition: device
                device_id: 
                entity_id: sensor.home_cloud_cover
                domain: sensor
                above: 60
                below: 80
          - condition: and
            conditions:
              - condition: sun
                after: sunset
                after_offset: "-00:30:00"
              - type: is_value
                condition: device
                device_id: 
                entity_id: sensor.home_cloud_cover
                domain: sensor
                above: 30
                below: 60
          - condition: and
            conditions:
              - condition: sun
                after: sunset
              - type: is_value
                condition: device
                device_id: 
                entity_id: sensor.home_cloud_cover
                domain: sensor
                above: 0
                below: 30
action:
  - if:
      - condition: device
        type: is_on
        device_id: 
        entity_id: light.plank_local
        domain: light
    then:
      - stop: Scene running 
    else:
      - service: scene.turn_on
        target:
          entity_id: scene.woonkamer_aan
        metadata: {}
mode: single

Hope anybody can help. I couldn’t find anything about this online. I’m still pretty new to HASS. Been playing with it for 2 weeks now.

Re-design your automation to be event-based as opposed to polling-based. That will give you precise control over when it triggers and executes its actions.

BTW, once a Time Pattern Trigger is created, it triggers according to the period you specified. There’s no enable/disable for triggers. You can enable/disable an automation but if that’s needed as part of its normal operation then the overall design needs improvement.

What specifically is the log file that you do not want in there?

It’s mostly the notification that the time pattern trigger has been triggered/the automation has been triggered. Not really a problem, more an annoyance

How would you design the automation based on my needs? Like the cloud cover value and the offset to sunset based on the cloud cover value? Like I said in the post, I’m still fairly new to HASS.

What are your requirements? Your first post explains the automation’s behavior but, rather than reverse-engineer the requirements from the automation, I would prefer if you explain what you want to achieve.

I would like to have the abillity that the lights turn on automatically depending on the cloud cover percentage on my home location. If the cloud cover percentage is 80 to 100%, the lights should turn on 2 hours before sunset. If the percentage is 60 to 80%, the lights should turn on 1,5 hours before sunset. If the percentage is 30 to 60 %, the lights should turn on 0,5 hour before sunset. If the percentage is 0 to 30%, the lights should turn on at sunset. When one of the requirements is met, the automation should activate the scene and after that go to, let’s call it sleepmode, so it won’t try to trigger again until the next day. The way I made the automation now, the automation will still trigger after it already has activated the scene.

…and stay on even if the cloud cover reduces to 0% at 1h59m before sunset, or go off again?

Stay on until I turn them of myself (when I go to bed). So when the scene is activated and all the lights are on, the automation is done for the day.

The below automation should:

  • trigger at 120, 90, 30 and 0 minutes before sunset
  • check conditions:
    • if the cloud cover is greater than the thresholds you provided for that time
    • if the light is off (which I assume means we haven’t already triggered)
  • activate scene if both conditions are met
trigger:
  - platform: template
    value_template: "{{ ((state_attr('sun.sun','next_setting')|as_timestamp(0)-now()|as_timestamp)/60)|int in [120, 90, 30, 0] }}"
condition:
  - condition: template
    value_template: >
      {% set mtss = ((state_attr('sun.sun','next_setting')|as_timestamp(0)-now()|as_timestamp)/60)|int %}
      {% set sch = {'120': 80, '90': 60, '30': 30, '0': 0} %}
      {{ states('sensor.home_cloud_cover')|float(100) >= sch[mtss|string] }}
  - condition: state
    entity_id: light.plank_local
    state: off
action:
  - service: scene.turn_on
    target:
      entity_id: scene.woonkamer_aan

(mtss = minutes to sunset; sch = schedule)

Note that I provide a default value of 100 in states('sensor.home_cloud_cover')|float(100) — that way, if your cloud cover sensor is not working, the lights come on at the early time rather than leaving you in the dark.

Perhaps an ambient light sensor would work better in your situation? Trigger when dark rather than via some inferred value from a complex schedule of times and cloud thresholds.

1 Like

Thanks! That automation worked like a charm! Much appreciated!

Can’t resist fiddling, as I didn’t like the complexity, repetition and hard-coding of the original. This is almost the same, and turns on the light when the cloud cover % × 1.5 is greater than or equal to the number of minutes to sunset — from 150 minutes before sunset at 100% cloud cover to sunset itself when the sky is clear. Play with the multiplier to fine-tune it.

trigger:
  - platform: template
    value_template: "{{ states('sensor.home_cloud_cover')|float(100)*1.5 >= ((state_attr('sun.sun','next_setting')|as_timestamp(0)-now()|as_timestamp)/60)|int }}"
condition:
  - condition: state
    entity_id: light.plank_local
    state: off
action:
  - service: scene.turn_on
    target:
      entity_id: scene.woonkamer_aan
1 Like

Sorry for the late reply.

Thanks for providing the requirements. It’s clear that they can be met without the need for a Time Pattern Trigger.

I realize your requirements have already been met by Troon’s automation. However, if you’re interested, here’s another way to do the same thing.

It employs four Sun Triggers, one for each time you specified. At each specified time, it checks if the cloud coverage is within the desired range. If the coverage is within range and the light is off, it turns on the scene.

alias: example
trigger_variables:
  clouds: sensor.home_cloud_cover
trigger:
  - platform: sun
    event: sunset
    offset: "-02:00:00"
    variables:
      coverage: "{{ 80 <= states(clouds)|float(80) <= 100 }}"
  - platform: sun
    event: sunset
    offset: "-01:30:00"
    variables:
      coverage: "{{ 60 <= states(clouds)|float(60) < 80 }}"
  - platform: sun
    event: sunset
    offset: "-00:30:00"
    variables:
      coverage: "{{ 30 <= states(clouds)|float(30) < 60 }}"
  - platform: sun
    event: sunset
    variables:
      coverage: "{{ 0 <= states(clouds)|float(0) < 30 }}"
condition:
  - "{{ coverage }}"
  - "{{ is_state('light.plank_local', 'off' }}"
action:
  - service: scene.turn_on
    target:
      entity_id: scene.woonkamer_aan

Anyway, the main point is that there’s no need for a method to “stop a Time Pattern Trigger” because your requirements can be fulfilled without the use of a Time Pattern Trigger.

3 Likes

Super cool, I like the usage of the variable in the condition, elegant.

2 Likes