Retriggerable timer [solved]

Hello there,

Still quite new to HA (core install 2022.12.3) I want to create a retriggerable timer that switches on a light. After the timer timeouts, the lamp is turned off. When the timer receives a trigger during countdown, the initial timer value should be restored.

  1. trigger switches on light and a timer (e.g. 180s)
  2. the running timer receives a trigger, reset to 180s
  3. after the timeout the lamp switches off.

Question: is this configurable in the GUI?

Richard.

What kind of trigger are you planning to use?

For example, is it a State Trigger that is monitoring the state of a Binary Sensor (like a door or motion detector)?

It is a PCF8574 input that goes from 1 to 0, a state trigger. To switch on a light by that trigger is no problem, that works.

R

If your automation starts a timer whenever the PCF8574 input that goes from 1 to 0, it will do that each time this state-change occurs. In other words, if the timer is active and then the state-change occurs, the automation restarts the timer. The automation should specify the duration value when starting the timer.

alias: example
mode: queued
trigger:
  - platform: state
    entity_id: sensor.test
    from: '0'
    to: '1'
  - platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.test
condition: []
action:
  - if: "{{ trigger.platform == 'state' }}"
    then:
      - service: timer.start
        target:
          entity_id: timer.test
        data:
          duration: '00:03:00'
      - service: light.turn_on
        target:
          entity_id: light.test
    else:
      - service: light.turn_off
        target:
          entity_id: light.test

Ok, but these sorts of automations, can it be done by the GUI or is “vi” the appropiate tool? I read somewhere I should turn the light on and start a timer. When the timer times out, the light is switched off. Then, if a trigger occurs before timeout, it should re-initiate the timer. But I cannot find a method to achieve this using the GUI…

R.

That’s exactly what the example I posted does.

I assume by “GUI” you mean the Automation Editor in visual mode. Yes, you can create what you want using the Automation Editor. However, posting a series of screenshots, showing each step of the process of creating an automation in visual mode, is not the usual way community members share their automations. Reference: FAQ guideline 16

Every automation you create with the Automation Editor in visual mode is ultimately stored as YAML. You can switch the Automation Editor from visual to YAML mode at any time to see the automation in YAML form (click the ‘three-dots’ icon in the upper right-hand corner of the Automation Editor and select ‘Edit in YAML’). It also makes it very easy to share an automation because you can simple copy-paste the example I posted directly into an new, empty automation in the Automation Editor, while it is in YAML mode, then switch it to visual mode.

Ok, thnx! Clear. I will test it!

Richard

Ok, I got it. I copy/pasted your code, but the lamp does not switch off after 10 seconds. I must have done something stupid somewhere. Anyone a hint?

edit: I remarked the line “mode: queued” is removed when loading the “automations”

R.

alias: test PIR
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.pcf8574_0x38_6
    from: "1"
    to: "0"
    for:
      hours: 0
      minutes: 0
      seconds: 0
  - platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.gang_BG_L5_180s
condition:
  - condition: sun
    before: sunrise
    after: sunset
    after_offset: "-00:30:00"
    before_offset: "+00:30:00"
    enabled: true
action:
  - if:
      - condition: template
        value_template: "{{ trigger.platform == 'state' }}"
    then:
      - service: timer.start
        target:
          entity_id: timer.gang_BG_L5_180s
        data:
          duration: "00:00:10"
      - service: light.turn_on
        target:
          entity_id: light.05_gang_bg
        data: {}
    else:
      - service: light.turn_off
        target:
          entity_id: light.05_gang_bg
        data: {}
mode: single

It implies there may be a problem with the timer.

An entity_id is always in lowercase. Go to Developer Tools > States and check how the timer’s entity_id is spelled.

There is no timer declaration in Developer Tools > States. I fear I missed something crucial somewhere. Tonight I’ll have a closer look at it…

R.

How did you create the timer entity? Via the UI or by defining it in configuration.yaml?

I didn’t declare a timer at all. Now I added a helper, but according to the trace the timer is never fired. I’ll throw everything away and I will try to build a new one. Just before deleting, here is the code:

alias: test PIR
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.pcf8574_0x38_6
    from: "1"
    to: "0"
    for:
      hours: 0
      minutes: 0
      seconds: 0
  - platform: event
    event_type: timer.timer_180s
    event_data:
      entity_id: timer.gang_bg_l5_180s
condition:
  - condition: sun
    before: sunrise
    after: sunset
    after_offset: "-00:30:00"
    before_offset: "+00:30:00"
    enabled: false
action:
  - if:
      - condition: template
        value_template: "{{ trigger.platform == 'state' }}"
    then:
      - service: timer.start
        target:
          entity_id: timer.timer_180s
        data: {}
      - service: light.turn_on
        target:
          entity_id: light.05_gang_bg
        data: {}
    else:
      - service: light.turn_off
        target:
          entity_id: light.05_gang_bg
        data: {}
mode: single

Your example’s Event Trigger is incorrect.

  - platform: event
    event_type: timer.timer_180s
    event_data:
      entity_id: timer.gang_bg_l5_180s

Replace it with this (assumes the timer is timer.timer_180s)

  - platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.timer_180s

Yep, that did it. It works like a charm. I begin to understand a bit how things work…

What happens now is that the light receives a command light.turn_on each time the trigger occurs. This light.turn_on is an X10 signal that is sent to the X10-switch. I would like to prevent the light.turn_on from being sent to the X10-switch when the light is already ON. If I add a condition, the timer would not receive a re-init. IOW: I need to add a condition into the if-then structure. Is that possible?

Thnx for your time BTW!

R.

    then:
      - service: timer.start
        target:
          entity_id: timer.timer_180s
      - condition: "{{ is_state('light.05_gang_bg', 'off') }}"
      - service: light.turn_on
        target:
          entity_id: light.05_gang_bg
    else:

You’re welcome!

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic is resolved. This helps other users find answers to similar questions.

For more information about the Solution tag, refer to guideline 21 in the FAQ.

So this comes above and not after:
- service: light.turn_on
So I assume that if the condition is false, the script just stops there?

R.

Correct.

Please consider marking my post above with the Solution tag (the one containing the example you ultimately used). You marked your own post as the Solution but it simply contains a copy of what I had proposed. That’s not how the Solution tag was meant to be used (refer to guideline 21 in the FAQ).

You mean post 13? Like this.