Help with a timer please

I could use some help as I try to learn how to use timers.

The situation: In my master bathroom, the shower is behind it’s own door with its own dimmer light.

What I HAD working was: During the night, if motion is detected, turn on the night light and the shower light at 40%. Five minutes after motion stops being detected turn off the lights.

The problem: I walk in and light turns on, I close the shower door, I start a shower, and the lights go off leaving me in the pitch black because motion outside the door stopped…

Additional tidbit: Since my shower light is a Zooz I was able to program the double-tap scene to turn on the shower light at full brightness.

If I listed all the ways I’ve attempted to get this to work, this post would be monstrously long. I’m jumping to the point. I’m attempting to use a timer so that when I double-tap the light for full brightness, I have an extra 40 minutes of light before it auto-turns off.

As I said, I’ve tried a bunch of things. This is the latest horror of creation came from attempting to follow a 2 year old youtube video. I know it’s wrong and busted - I’d really like some help please. I don’t mind restructuring everything (I’ve already done that a few times with me trying to start timers on motion detection vs motion stop and things like that). I’m just trying to wrap my head around how this timer logic is supposed to work within HA.

Thank you!

alias: MasterTurnOffNightLights
description: Turn Night Lights Off with Motion
trigger:
  # The below is my motion sensor
  - type: no_motion
    platform: device
    device_id: 1496bf40ddf54bcc412ffa77280396ba
    entity_id: 0df0906ba602f98910bfb4a2329a123f
    domain: binary_sensor
    id: Motion Stopped
  # My attempt to use a timer
  - platform: event
    event_type: timer.bathroom_finished
    event_data:
      # super simple timer of 10 minutes
      entity_id: timer.bathroom_lights
    id: Timer finished
  # The double tap on the Zooz
  - platform: event
    event_type: KeyPressed
    event_data:
      entity_id: event.master_bathroom_scene_001
    id: Light Pressed
condition:
  - condition: sun
    before: sunrise
    after: sunset
    before_offset: "+00:20:00"
action:
 # This is the night light on a power switch - I don't care if it turns off or not via timer
  - type: turn_off
    device_id: 9daa88fb904ed23fb02889fbe8e6c574
    entity_id: 42818d4e4486f2f72578fa7b81118707
    domain: switch
 # This is the light which I disabled when I start to muck about with timers
  - target:
      device_id: 076e90b5458b9e3b538a55093b8b80ae
    data: {}
    action: light.turn_off
    enabled: false
  # The latest attempt to do timers.
  - choose:
      - conditions:
          - condition: trigger
            id:
              - Motion Stopped
        sequence:
          - action: timer.start
            data:
              duration: "10"
            target:
              device_id: 076e90b5458b9e3b538a55093b8b80ae
      - conditions:
          - condition: trigger
            id:
              - Light Pressed
        sequence:
          - action: timer.change
            metadata: {}
            data:
              duration: "+40"
mode: single
  • The event_type timer.bathroom_finished isn’t a valid event… it should be timer.finished
  • The actions seem to be missing an entity ID for your timer… in the first option of the choose, you have provided the device ID of the light from the previous action and in the second there is no target supplied.

Before we get in the weeds… are there any other sensors in this bathroom that might help make a more reliable automation? Door sensors or humidity sensors can often be very useful adjuncts to a motion based automation in a bathroom.

There is a temp/humidity sensor. And I have an automation that works well for the humidity that auto turns on the fan. I did attempt to have it turn on the light, but if the motion stops after the fan is on, the light turns off.

Thanks for the pointers! I’ll poke at it again. I’m curious what thoughts you have about the humidity sensor.

Greetings,
I got some more time to tinker with it tonight, but still can’t get the timers to work.

I just don’t quite understand the logic. To me it would make more sense to say “when this, turn on the timer. If this, add more time. At the end of the timer do that.” Yet, the examples I see seem to have the logic for what the timer is and how to interact with it inside the trigger. That seems very dumb to me - it’s like defining multiple if/ifelse/else all at once then writing the code for them in one weird block. I don’t understand why the timer logic isn’t defined in the sequence itself. But that’s not how the examples do it. (it also really sucks that a lot of the examples are such small snippets they are completely useless - I can’t copy paste them and have them work well enough I can tinker and learn)

I was also looking at this:

And it seems really dumb to set the timer with the max value then subtract because it can’t go higher then the initial value.

I’m sure some really smart people have got some clever logic/reasons behind this, but right now I can’t figure it out. It seems like a timer should be super simple and a powerful way of controlling events yet it’s quite convoluted in the logic.

Can someone please help me understand these things?

Thanks.

[Edit]: Watched another youtube video about timers - another example that doesn’t work. His logic makes a lot more sense, but when I copy his code I get a warning
Key 'service' is not expected or not supported by the visual editor. :weary:

Ugh. OK. So figured out why their code was wrong. Looks like things changed in the last few months. That’s reassuring.

  1. set my timer to be an hour.
  2. change my motion detection to be this:
alias: MasterTurnOnNightLight
description: Turn Night Lights On with Motion
trigger:
  - type: motion
    platform: device
    device_id: 1496bf40ddf54bcc412ffa77280396ba
    entity_id: 0df0906ba602f98910bfb4a2329a123f
    domain: binary_sensor
condition:
  - condition: sun
    before: sunrise
    after: sunset
action:
  - type: turn_on
    device_id: 9daa88fb904ed23fb02889fbe8e6c574
    entity_id: 42818d4e4486f2f72578fa7b81118707
    domain: switch
  - target:
      device_id: 076e90b5458b9e3b538a55093b8b80ae
    data:
      brightness_pct: 40
    action: light.turn_on
  - action: timer.start
    target:
      entity_id: timer.bathroom_lights
    data:
      duration: "00:15:00"
mode: single
  1. Make my off like this:
alias: Turn Off Night Light with Timer
description: Turn Night Lights Off with Timer
trigger:
  - platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.bathroom_lights
condition: []
action:
  - type: turn_off
    device_id: 9daa88fb904ed23fb02889fbe8e6c574
    entity_id: 42818d4e4486f2f72578fa7b81118707
    domain: switch
  - target:
      device_id: 076e90b5458b9e3b538a55093b8b80ae
    data: {}
    action: light.turn_off
    enabled: true
mode: single

Huzzah!

  1. Now enable the double tap to increase the time to the max limit. Note, the timer.change won’t update the timer. The only way I can get it to increase is to start it over with the max amount of time. :man_shrugging:
alias: MasterNightLightUpdate
description: Update the timer to max
trigger:
  - platform: event
    event_type: KeyPressed
    event_data:
      entity_id: event.master_bathroom_scene_001
    id: Shower Light Pressed
condition: []
action:
  - action: timer.start
    target:
      entity_id: timer.bathroom_lights
    data:
      duration: "01:00:00"
mode: single
  1. But wait… there’s a problem. If I’m hanging out in the bathroom I don’t want the lights to turn off on me! And it seems that my sensor has to stop detecting motion before it will send another update! So my timer for 15 minutes will turn off the lights if I’m still moving around! :rage:

OK. So what if I add yet another automation so that on motion stop, that’s what starts the timer? Well, if I do that, step into the bathroom, double tap to set the timer to an hour, then motion stop resets the timer to five minutes! :rage:

UGH! Why can’t I just add time? Why does it reset?

So breaking it out into smaller logical steps makes a lot more sense then the tutorials that try to cram it all into one. I’ve “made progress” tonight and yet I still don’t feel like I have a grasp on how these timers are supposed to work… I’m calling it a night. Any help that can explain how timer logic is supposed to work would be appreciated.

:weary:

You don’t need to subtract anything, just use the timer.start action with a duration. You are limited to a max length of the configured duration value. This is no different than other helpers where you have to set a max value (Input Numbers) or a max character length (Input Text). I’m not a dev, but I would guess this has something to do with memory allocation or some other efficiency-related need.

The action timer.change only works on active timers, so if you’re testing it without the timer running or you press the button because the timer timed out and you’re in the dark, it won’t do anything. But, timer.start works on both active and inactive timers.

Check the condition of the motion sensor, add time if it’s on:

"Turn Off" Automation with this addition
alias: Turn Off Night Light with Timer
description: Turn Night Lights Off with Timer
trigger:
  - platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.bathroom_lights
condition: []
action:
  - if:
      - condition: state
        entity_id: 0df0906ba602f98910bfb4a2329a123f
        state: 'off'
    then:
      - type: turn_off
        device_id: 9daa88fb904ed23fb02889fbe8e6c574
        entity_id: 42818d4e4486f2f72578fa7b81118707
        domain: switch
      - action: light.turn_off
        target:
          device_id: 076e90b5458b9e3b538a55093b8b80ae
        data: {}
        enabled: true
    else:
      - action: timer.start
        target:
          entity_id: timer.bathroom_lights
        data:
          duration: "00:05:00"
mode: single

This gives you a timer-based, self-sustaining loop.

Most similar scenarios do not need a timer, but instead rely on State triggers and/or conditions with defined duration components. When combined when the restart automation mode and proper conditional logic, this creates a similar sustained loop to keep the lights on.

Again, use conditional logic… this time in the “Turn On” automation:

"Turn On" Automation with this addition
alias: MasterTurnOnNightLight
description: Turn Night Lights On with Motion
trigger:
  - type: motion
    platform: device
    device_id: 1496bf40ddf54bcc412ffa77280396ba
    entity_id: 0df0906ba602f98910bfb4a2329a123f
    domain: binary_sensor
condition:
  - condition: sun
    before: sunrise
    after: sunset
action:
  - type: turn_on
    device_id: 9daa88fb904ed23fb02889fbe8e6c574
    entity_id: 42818d4e4486f2f72578fa7b81118707
    domain: switch
  - target:
      device_id: 076e90b5458b9e3b538a55093b8b80ae
    data:
      brightness_pct: 40
    action: light.turn_on
  - alias:  Check if timer is already running before setting motion-based timer duration
    condition: not
    conditions:
      - condition: state
        entity_id: timer.bathroom_lights
        state: active
  - action: timer.start
    target:
      entity_id: timer.bathroom_lights
    data:
      duration: "00:15:00"
mode: single
1 Like

Thanks @Didgeridrew for your help. I really do appreciate it. I used this information as a basis and I’ve been tweaking for the past three weeks. I can’t get it to work the way I want. I’ve decided to give up and take another approach. I’m going to buy a second motion / presence sensor for the small room itself. I think that will be easier and a lot simpler. I’ve found the timer to be too inconsistent. Some times it counts down then goes to “inactive” before switching to idle and that won’t trigger the turn-off automation. Some times the timer adds time the way it’s supposed to and other times it resets on me. I can’t ever get it to trigger a fault consistently so when I try to capture a debug event I get nothing of value.
:weary:

I’ll revisit timers later. But a second sensor is cheap compared to the frustration. :smile: :man_shrugging:

But I still really appreciate you taking the time to help me learn a bit and figure a few things out.