Automation - Lights keep timing out when we are in front of them

So I kind of have a small problem with automating my light timers, we constantly seem to have lights that turn off when we a all actively in front of them, I’ve got the automation set to restart on the trigger side and have moved the automation to call a script instead of delay timers being used as im told these if triggered again can stop the delay. Any help would be great (the wife now thinks its not funny…)

the Goal

the goal is to turn on lights in different combinations when the light level changes and time of day. and to turn off when we are not in front of the Philips Pir detector

The system is running on Zigbee2Mqtt.

advance apologies for the complex automation>script>scene, but I couldn’t think of a way to script it down to a pick and option, then pick another option based on time of the day vs light level.

- id: '1615045956352'
  alias: '1- Automation - Kitchen Lights '
  description: ''
  trigger:
  - platform: state
    entity_id: binary_sensor.kitchen_pir_occupancy
    from: 'off'
    to: 'on'
    for: 00:00:01
  condition: []
  action:
  - choose:
    - conditions:
      - condition: time
        after: '23:31'
        before: 05:29
      sequence:
      - service: script.kitchen_lights_at_night
      - service: light.turn_off
        data: {}
        target:
          area_id:
          - 82e9b832929a48e59806ac8f2efa08e4
          - acf47f95c1044de6bac72f531a1b7953
    - conditions:
      - condition: time
        after: 05:30
        before: '23:30'
      - condition: numeric_state
        entity_id: sensor.driveway_light_level
        below: '30000'
        attribute: light_level
      sequence:
      - service: script.kitchen_lights_in_the_day
      - service: light.turn_off
        data: {}
        target:
          area_id:
          - acf47f95c1044de6bac72f531a1b7953
          - 82e9b832929a48e59806ac8f2efa08e4
    - conditions:
      - condition: numeric_state
        entity_id: sensor.driveway_light_level
        attribute: light_level
        above: '30000'
      - condition: time
        after: 05:30
        before: '23:30'
      sequence:
      - service: script.kitchen_lights_in_the_day_not_utility
      - service: light.turn_off
        data:
          transition: 10
        target:
          area_id:
          - acf47f95c1044de6bac72f531a1b7953
          - 82e9b832929a48e59806ac8f2efa08e4
    default: []
  mode: restart
  max: 3

the “day” Script

kitchen_lights_in_the_day:
  alias: Kitchen Lights in the Day
  sequence:
  - choose:
    - conditions:
      - condition: time
        after: 05:29
        before: '11:59'
      sequence:
      - scene: scene.kitchen_lights_morning
    - conditions:
      - condition: time
        after: '12:00'
        before: '14:00'
      sequence:
      - scene: scene.kitchen_lights_lunch
    - conditions:
      - condition: time
        after: '14:00'
        before: '23:30'
      sequence:
      - scene: scene.kitchen_lights_night
    default: []
  - delay:
      hours: 0
      minutes: 5
      seconds: 10
      milliseconds: 0
  - condition: state
    entity_id: binary_sensor.kitchen_pir_occupancy
    state: 'off'
    for:
      hours: 0
      minutes: 1
      seconds: 0
  - service: light.turn_off
    target:
      area_id:
      - acf47f95c1044de6bac72f531a1b7953
      - 82e9b832929a48e59806ac8f2efa08e4
  mode: restart
  icon: mdi:lightbulb-on

and the “night scene”

  icon: mdi:weather-sunny
- id: '1643039277759'
  name: Kitchen Lights Night
  entities:
    light.kitchen_kickspace1:
      min_mireds: 150
      max_mireds: 500
      effect_list:
      - blink
      - breathe
      - okay
      - channel_change
      - finish_effect
      - stop_effect
      supported_color_modes:
      - color_temp
      - xy
      color_mode: xy
      brightness: 255
      hs_color:
      - 299.754
      - 95.686
      rgb_color:
      - 254
      - 11
      - 255
      xy_color:
      - 0.385
      - 0.155
      color:
        h: 300
        hue: 300
        s: 100
        saturation: 100
        x: 0.385
        y: 0.155
      device:
        applicationVersion: 1
        dateCode: '20131206'
        friendlyName: Kitchen kickspace1
        hardwareVersion: 1
        ieeeAddr: '0x00124b00222efb28'
        manufacturerID: 0
        manufacturerName: GLEDOPTO
        model: GL-C-008-1ID
        networkAddress: 32662
        powerSource: Mains (single phase)
        softwareBuildID: 3.0.0
        stackVersion: 2
        type: Router
        zclVersion: 1
      linkquality:
      icon: mdi:led-strip-variant
      friendly_name: Kitchen kickspace1
      supported_features: 63
      state: 'on'
    light.kitchen_main_lights:
      effect_list:
      - blink
      - breathe
      - okay
      - channel_change
      - finish_effect
      - stop_effect
      supported_color_modes:
      - brightness
      color_mode: brightness
      brightness: 255
      device:
        applicationVersion: 0
        dateCode: 'NULL'
        friendlyName: Kitchen Main lights
        hardwareVersion: 0
        ieeeAddr: '0xec1bbdfffe7d15ee'
        manufacturerID: 4107
        manufacturerName: Sunricher
        model: ZG9101SAC-HP
        networkAddress: 7725
        powerSource: Mains (single phase)
        softwareBuildID: 2.5.3_r47
        stackVersion: 0
        type: Router
        zclVersion: 3
      linkquality: 25
      friendly_name: Kitchen Main lights
      supported_features: 45
      state: 'on'
    light.utility_light:
      effect_list:
      - Night
      - Read
      - Meeting
      - Leasure
      - Scenario 1
      - Scenario 2
      - Scenario 3
      - Scenario 4
      - Music
      supported_color_modes:
      - onoff
      color_mode: onoff
      icon: mdi:light-recessed
      friendly_name: Utility Light
      supported_features: 4
      state: 'on'
    switch.kitchen_lights_cabinets:
      color_mode: xy
      device:
        applicationVersion: 67
        friendlyName: kitchen_lights_cabinets
        ieeeAddr: '0xec1bbdfffeac5165'
        manufacturerID: 0
        manufacturerName: _TYZB01_iuepbmpv
        model: MS-104Z
        networkAddress: 15043
        powerSource: Mains (single phase)
        type: Router
        zclVersion: 3
      linkquality: 65
      power_on_behavior: 'on'
      icon: mdi:hockey-puck
      friendly_name: kitchen_lights_cabinets
      state: 'on'
    select.kitchen_lights_cabinets_power_on_behavior:
      options:
      - 'on'
      - 'off'
      - previous
      color_mode: xy
      device:
        applicationVersion: 67
        friendlyName: kitchen_lights_cabinets
        ieeeAddr: '0xec1bbdfffeac5165'
        manufacturerID: 0
        manufacturerName: _TYZB01_iuepbmpv
        model: MS-104Z
        networkAddress: 15043
        powerSource: Mains (single phase)
        type: Router
        zclVersion: 3
      linkquality: 65
      power_on_behavior: 'on'
      icon: mdi:power-settings
      friendly_name: kitchen_lights_cabinets_power_on_behavior
      state: 'on'
    switch.kitchen_dining_lights:
      device:
        applicationVersion: 67
        friendlyName: 'Kitchen Dining Lights '
        ieeeAddr: '0xec1bbdfffeac531a'
        manufacturerID: 0
        manufacturerName: _TYZB01_iuepbmpv
        model: MS-104Z
        networkAddress: 36458
        powerSource: Mains (single phase)
        type: Router
        zclVersion: 3
      linkquality: 25
      power_on_behavior:
      icon: mdi:light-recessed
      friendly_name: 'Kitchen Dining Lights '
      state: 'on'
    select.kitchen_dining_lights_power_on_behavior:
      options:
      - 'on'
      - 'off'
      - previous
      device:
        applicationVersion: 67
        friendlyName: 'Kitchen Dining Lights '
        ieeeAddr: '0xec1bbdfffeac531a'
        manufacturerID: 0
        manufacturerName: _TYZB01_iuepbmpv
        model: MS-104Z
        networkAddress: 36458
        powerSource: Mains (single phase)
        type: Router
        zclVersion: 3
      linkquality: 25
      power_on_behavior:
      icon: mdi:power-settings
      friendly_name: Kitchen Dining Lights  power on behavior
      state: unknown
  icon: mdi:weather-sunny

the sensor config output from zigbee2mqtt

{
    "battery": 64,
    "illuminance": 16697,
    "illuminance_lux": 47,
    "led_indication": true,
    "linkquality": 65,
    "motion_sensitivity": "high",
    "occupancy": false,
    "occupancy_timeout": 0,
    "temperature": 19.99,
    "update": {
        "state": "idle"
    }

Please share your automation ans script.

1 Like

Probably good to revise your title too.

1 Like

done !! sorry was already in the throughs of updating.

done !! many thanks and very true. I’m feeling I’ve made a basic mistake in the restart and turn off association but can’t for the life of me see it.

Sorry that isn’t really much use to us. Please see point 11 in the link Mahko posted above, then edit your post and format it correctly.

1 Like

updated with backticks. thanks again.

Hi Dean,

Just a random thought, do your PIR sensors sleep for a period after sensing motion. My ring motion sensors sleep and won’t retrigger for several minutes after sensing motion to conserve battery. Consequently I have to make sure delays are longer than the sleep time.

Also you don’t necessarily need to use scripts if you don’t want to as you can set automations to have a mode of restart which will cancel your delays in automations when retriggered. Although I note you have max: 3 on the automation - not sure whether that has an effect in restart mode.

1 Like

Hi Adrian,

Thanks for your reply, as I understand it in z2mqqt the default reset of an event is 90 seconds, ive got the duration of delay set in the script to 5mins and to progress back to the automation it needs to be off for 1min.

Now I think about it do you think the error is the 90sec (1.5mins) and 1 min point… saying that the reset of the kitchen sensor comes in much earlier than 90sec judging by the sensor indicating icon on my dash… I will check the log to see and start time and reset of the Kitchen PIR.

1 Like

Kitchen cleared (no motion detected)

10:54:02 - 32 seconds ago

KitchenGroup turned on by 1- Automation - Kitchen Lights

10:53:49 - 45 seconds ago

Thats from the log… Edited
Kitchen Main lights turned on by 1- Automation - Kitchen Lights

10:53:49 - 45 seconds ago

Kitchen Lights turned on

10:53:49 - 45 seconds ago

Kitchen Lights turned on

10:53:49 - 45 seconds ago

Kitchen Lights in the Day Brights no Utility turned on by 1- Automation - Kitchen Lights

10:53:49 - 1 minute ago

Kitchen Lights in the Day Brights no Utility started by 1- Automation - Kitchen Lights

10:53:49 - 1 minute ago

1- Automation - Kitchen Lights has been triggered by state of binary_sensor.kitchen_pir_occupancy by Kitchen

10:53:49 - 1 minute ago - Show trace

Kitchen detected motion

10:53:48 -

Just found this in the trace option of the kitchen Automation

Stopped because of unknown reason “null” at 7 February 2022, 09:47:24 (runtime: 12.00 seconds)

not sure where the null is coming from… is that possibly a another trigger from the detector thus a restart ?

just tested it and yes its a restart event - another trigger from the detector that starts a new timer/automation/event

Rather than using a delay in your script and then turning the lights off, simply write another automation to turn the lights off when there has been no movement:

e.g.

trigger:
  - platform: state
    entity_id: binary_sensor.kitchen_pir_occupancy
    from: 'on'
    to: 'off'
    for: '00:05:10' # no movement for 5 minutes and ten seconds
action:
  - service: light.turn_off
    target:
      area_id:
      - acf47f95c1044de6bac72f531a1b7953
      - 82e9b832929a48e59806ac8f2efa08e4

I’ve removed the max 3 restarts from the automation as I think that this is maxing out when we are all in front off the sensors and killing off the lights prematurely. will test and update.

So like a global off timer? I had this and took it out after reading about reducing the amount of automations and leaning toward all in one solutions. but yeh its a good idea in this case.

TBH, I’m not sure where this comes from.

Sometimes “all in one” solutions may be better. Sometimes they aren’t.

there’s no hard and fast rule that says you should reduce the number of automations.

it really just depends on which makes the most sense to you logically and, just as importantly if not moreso, for readability. You can’t maintain one automation if it’s 100’s of lines long if you can’t read it later.

2 Likes