Cookbook Automation recipe not working properly

I am using an automation based on one from the HA Cookbook called Turn on Lights for 10 Minutes After Motion Detected. It works based on a PIR sensor I have on the wall and turn on only after sundown and before sunrise. There is however one minor issue: the script always turns the light off after the delay (2 min) even when there is movement in the room in the meantime. I would like the script to turn on the light and only turn it off when there is no movement in the meantime or if the light is already off. What am I doing wrong?

automation:
  alias: "Turn on gabinet lights when there is movement"
  hide_entity: false
  initial_state: True
  trigger:
    - platform: state
      entity_id: binary_sensor.pir_gabinet
	  to: "on"
  condition:
    condition: and
    conditions:
      - condition: or
        conditions:
          - condition: sun
            after: sunset
          - condition: sun
            before: sunrise
      - condition: state
        entity_id: switch.gabinet2
        state: "off"
  action:
    - service: script.turn_on
      entity_id: script.timed_lamp

script:
  timed_lamp:
    alias: "Turn on lamp and set timer"
    sequence:
      # Cancel ev. old timers
      - service: script.turn_off
        data:
           entity_id: script.timer_off
      - service: switch.turn_on
        data:
          entity_id: switch.gabinet2
      # Set new timer
      - service: script.turn_on
        data:
          entity_id: script.timer_off
  timer_off:
    alias: "Turn off lamp after 1 minutes"
    sequence:
      - delay:
          minutes: 2
      - condition: state
        entity_id: switch.gabinet2

I did something similar that seems to work for me.

- alias: 'Stair Switch Timeout'
  trigger:
    platform: state
    entity_id: switch.stairway_switch
    state: 'on'
    for:
      minutes: 5
  condition:
    condition: time
    after: '22:00'
  action:
    - service: script.turn_on
      entity_id: script.stairswitchoff

in your case you might want to throw in your check for motion into the condition. It’s an idea, it might not work, I’m new to this stuff too.

You check for the switch being off as a condition, which will prevent it from running once the switch turns on, you need to check something you don’t change (i would recommend setting s seperate hidden input_boolian when your script turns the light on.

1 Like

Thanks! That was it! How come I didn’t notice it was so simple :).

Will need to do my homework on how to use the input boolean but now you set me on the right direction at least!

I’m still trying to figure out exactly how to do that one myself, I think it’ll be along the lines of check to see if the light is on when you detect motion, if it is, then do nothing, if it is not, turn on an input boolean, then turn on the light, when you detect no motion (state back to off) then check the input boolean to see if you are supposed to turn off the light.
the tricky part is that you have to be able to switch on the light before you trip the motion sensor.

If your light has a dimmer it’s a bit easier, just set a specific dim level when you turn it on automatically, then turn it off only if it’s still at that dim level.

Why not just take out the condition around the switch being off. You really don’t care if you turn on the switch while it’s already on. This would also have the effect of restarting the timer each time it sees motion so it wouldn’t turn off and back on real quick each time the timer expires? (I’m new to this so it may an obvious question/answer, sorry)

what I’m trying to prevent is the light from going off if you turn on the switch manually first.

When you say manually, please elaborate? Are you talking about a smart switch, a standard wall switch, or turning it on in HA?

smart switch or from the HA dashboard aka not based on the automation we’re talking about.

ok so if it’s turned on by some method other than by motion, you want it to stay on regardless of whether there is motion or not. It’s only if it is turned on by motion that you want it to go into timer mode. Right?

right, i have a motion sensor in my entryway which also happens to be able to see the switch where you turn on the same light that the motion sensor controls.

Yep, sounds like you are going to have to adjust the sensor so it can’t see the switch. Otherwise, you might have to use a stick or something to switch the light switch on to keep it from always thinking that it was turned on by the switch.

Won’t work unfortunately - my switch is a script which flips the switch regardless of its current state. I have a separate script to feedback the current status. Hence turning it on again would turn it off :). What is more I don’t want the switch to turn off if the person turned on the light manually (I have a switch on the wall as well as a HA switch - that work in combination as a staircase swich i.e. flipping the wall or HA switch changes the light status).

In conclusion I want it to work as follows:

  1. After sundown/before sunrise - if switch off, someone turns it on then it won’t turn off until someone turns it off
  2. After sundown/before sunrise - if switch off, there is movement, turn it on for 2 min and turn off
  3. During day - only switch on the wall will turn light on/off

you are still going to have to have something persistent and independent of the state of the sensor and the switch to track whether the switch was turned on manually, or because of motion. Then that becomes part of the condition in your “off” automation.