Timer for my Lights

Hello,

I tried searching for a solution but can`t really get the hang of it… and with the visuelle Editor my plan can´t be done. So I had to try with YAML but it doesn´t work.

My plan is that the light turns on for 1 Minute if motion is detected and Lux is below 30.
Everything works, my only problem is the Timer, that it doesn´t turn off again after 1 minute.

alias: Treppenhaus Licht EG
trigger:
  - type: motion
    platform: device
    device_id: 6649468e9a206326bb3f6bb3840453c9
    entity_id: binary_sensor.0x54ef4410007761a6_occupancy
    domain: binary_sensor
condition:
  - type: is_illuminance
    condition: device
    device_id: cb3b50c09e835698879c7acf9db10793
    entity_id: sensor.0x04cf8cdf3c8a20ff_illuminance_lux
    domain: sensor
    below: 30
action:
  - parallel:
      - type: turn_on
        device_id: 56e520e95d7a1e6f1e8c3819d971cb41
        entity_id: light.0xa49e69fffe94d9a3
        domain: light
        minutes: 1
      - type: turn_on
        device_id: b77ead0a4bee2b65f13217906d7c8b10
        entity_id: light.0xa49e69fffe9a3355
        domain: light
        minutes: 1
mode: single

Still learning, I guess it´s super easy to solve :sweat_smile:

Best regards
Alex

Is this your invention?

        minutes: 1

When creating a Device Action, whatever the Automation Editor presents in Visual mode is all that’s permissible.

1 Like

If you’re using a timer, you have to call the service to start it with the number of seconds you want like

- service: timer.start
    data:
      duration: '60'
    target:
      entity_id: timer.my_timer

At the end, it will trigger the event timer.finished or timer.cancelled.
You have to add that event in your trigger to off the light.

Yeah, didn´t really know what to do…

And how can I put get that working in my configuration I posted!
I don´t really know how to do that… sorry…

First, go back to the UI editor :slight_smile:
Then it is quite easy, let me do it plain English for you

  1. Trigger
    motion + add id → motion
    end of timer + add id → timer_end
  2. Conditions, remove as it will prevent the timer_end trigger
  3. In your actions, add 2 if / then action
    if triggered by motion AND lux below 30 then turn on the lights and start the timer
    if triggered by timer_end then turn_off the lights

Also, change the mode to restart so it will restart your 1 minute timer if motion is detected during the 1 minute of light and you’ll not end in the dark.

By the way, I’m not a blueprint guy but have a look, what you try to do is very “common”.

EDIT You can create a timer in Settings > Devices & Services > Helpers

1 Like
alias: Treppenhaus Licht EG
trigger:
  - platform: state
    entity_id: binary_sensor.0x54ef4410007761a6_occupancy
    from: 'off'
    to: 'on'
condition:
  - condition: numeric_state
    entity_id: sensor.0x04cf8cdf3c8a20ff_illuminance_lux
    below: 30
action:
  - service: light.turn_on
    target:
      entity_id:
        - light.0xa49e69fffe94d9a3
        - light.0xa49e69fffe9a3355
  - delay:
      minutes: 1
  - service: light.turn_off
    target:
      entity_id:
        - light.0xa49e69fffe94d9a3
        - light.0xa49e69fffe9a3355
mode: restart

EDIT

Enhancement. Changed mode to restart to fulfill additional requirement:

1 Like

Thank you very much, will try that later!

I feel like a lot of stuff can only been done with the YAML Editior and not via visuelle Editor :smiley:

Another question: Is it possible, that the delay is resets if the binary sensor catches motion again.
So that the lights dont go off and on again after 1 Minute, when im still running around in the hallway?

All that yaml can be done with the GUI.

Change the mode to restart

1 Like

Thanks a lot!

I couldn’t find anything in the GUI to get a delay in the lights so they turn back off.

But I will get the hang of it, hopefully.

okay wow, I`m just blind… :skull:

Did you get a chance to test it?

Sorry for the delayed answer!
Yes and it works perfect, after I knew how it worked I changed all my automation so they have a timer included. Befor I had a lights on automation and lights off automation, and then for weekend. So there where like 20 Automation and now I have only 7 :metal:

Glad to hear it!

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 has been resolved. This helps users find answers to similar questions.

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

1 Like

There is a problem with the Automation.
The Automation triggers if I walk in the hallway and the motion sensor triggers, the problem is as long as it detecteds motion the hole time the timer doesn’t restart and the lights turn off after 30 seconds, but if it doesnt detected motion for about 5 seconds, the timer restarts (I stopped the time to measure it)

The detection_interval is set 2 seconds of the motion sensor and I checked in Zigbee2mqtt, if the motion detection triggers from off/inkativ to on/aktiv the lights go on perfect like it should, but if then if it triggers very fast like under 1 second form on to off to on ( I waved my hand in front of it), the timer doesn’t restart, but if it is off/inkativ for like 3-4 seconds and the detected motion againt the timer restarts.

Kinda hard to describe whats the problem, but I hope somebody understands what I mean and knows a solution for it. :thinking:

Perhaps you can reverse the automation.
So set up a trigger when there i no movement for 30 seconds and make this turn off the lights.
Then have another trigger when there is movement to turn them on.

description: ""
trigger:
  - platform: state
    entity_id: binary_sensor.0x54ef4410007761a6_occupancy
    from: "off"
    to: "on"
    id: "on"
  - platform: state
    entity_id:
      - binary_sensor.0x54ef4410007761a6_occupancy
    from: "on"
    to: "off"
    id: "off"
    for:
      hours: 0
      minutes: 0
      seconds: 30
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: "on"
          - condition: numeric_state
            entity_id: sensor.0x04cf8cdf3c8a20ff_illuminance_lux
            below: 30
        sequence:
          - service: light.turn_on
            target:
              entity_id:
                - light.0xa49e69fffe94d9a3
                - light.0xa49e69fffe9a3355
            data: {}
      - conditions:
          - condition: trigger
            id: "off"
        sequence: 
          - service: light.turn_off
            target:
              entity_id:
                - light.0xa49e69fffe94d9a3
                - light.0xa49e69fffe9a3355
            data: {}
mode: single
1 Like

okay, that sounds good, will try that later! Thanks :metal:

Works perfect, Thanks a lot for the help!

When the motion sensor turns on, there’s no need for it to trigger the automation unless the light level is below 30.

If you don’t make that part of the automation’s trigger or condition, and only check the light level in the automation’s action, it means the automation will be executed (and its last_triggered updated) every time motion is detected regardless of the light level.

Here’s one way to restrict execution to only when needed:

alias: example 
description: ""
trigger:
  - platform: state
    entity_id: binary_sensor.0x54ef4410007761a6_occupancy
    from: 'off'
    to: 'on'
  - platform: state
    entity_id: binary_sensor.0x54ef4410007761a6_occupancy
    from: 'on'
    to: 'off'
    for:
      seconds: 30
condition:
  - condition: template
    value_template:  >
      {{ trigger.to_state.state == 'off' or 
        (trigger.to_state.state == 'on' and
         states('sensor.0x04cf8cdf3c8a20ff_illuminance_lux') | float(0) < 30) }}
action:
  - service: "light.turn_{{ trigger.to_state.state }}"
    target:
      entity_id:
        - light.0xa49e69fffe94d9a3
        - light.0xa49e69fffe9a3355
mode: single

The automation’s condition can be refined even further and only turn the lights off if they are currently on (and turn on only if they are currently off).

condition:
  - condition: template
    value_template:  >
      {% set lights_on = ['light.0xa49e69fffe94d9a3', 'light.0xa49e69fffe9a3355']
        | map('states') | select('eq', 'on') | count %}
      {{ (trigger.to_state.state == 'off' and lights_on > 0) or 
         (trigger.to_state.state == 'on' and
           states('sensor.0x04cf8cdf3c8a20ff_illuminance_lux') | float(0) < 30 and
           lights_on < 2) }}