Problems with the delay

Please tell me where I could have made a mistake? There is automation of automatic switching on of the light. The light turns on, but the delay of 3 minutes does not work, for some reason the light does not turn off.

alias: Автоматическое включение светом над раковиной
description: ""
trigger:
  - type: motion
    platform: device
    device_id: 07dcaa7522cd4afe0d5e91a51d7c423e
    entity_id: binary_sensor.datchik_dvizheniia_rakoviny_occupancy
    domain: binary_sensor
condition:
  - type: is_illuminance
    condition: device
    device_id: b92c7483c2136dca6f6237c19c441080
    entity_id: sensor.datchik_osveshcheniia_illuminance_lux
    domain: sensor
    below: 600
    enabled: true
  - condition: device
    type: is_off
    device_id: 0f010b6471e32de3b1c5166cceb80de4
    entity_id: switch.rozetka_osveshcheniia_rakoviny
    domain: switch
action:
  - type: turn_on
    device_id: 0f010b6471e32de3b1c5166cceb80de4
    entity_id: switch.rozetka_osveshcheniia_rakoviny
    domain: switch
  - if:
      - type: is_no_motion
        condition: device
        device_id: 07dcaa7522cd4afe0d5e91a51d7c423e
        entity_id: binary_sensor.datchik_dvizheniia_rakoviny_occupancy
        domain: binary_sensor
        for:
          hours: 0
          minutes: 3
          seconds: 0
    then:
      - type: turn_off
        device_id: 0f010b6471e32de3b1c5166cceb80de4
        entity_id: switch.rozetka_osveshcheniia_rakoviny
        domain: switch
mode: single

You are not using a delay or wait.

Conditions are evaluated immediately. Since your automation is triggered by the motion sensor, there is no way that it will register no motion for 3 minutes immediately after it registered motion… The better method is to use multiple triggers.

alias: Автоматическое включение светом над раковиной
description: ""
trigger:
  - id: motion
    type: motion
    platform: device
    device_id: 07dcaa7522cd4afe0d5e91a51d7c423e
    entity_id: binary_sensor.datchik_dvizheniia_rakoviny_occupancy
    domain: binary_sensor
  - id: no motion
    type: is_no_motion
    platform: device
    device_id: 07dcaa7522cd4afe0d5e91a51d7c423e
    entity_id: binary_sensor.datchik_dvizheniia_rakoviny_occupancy
    domain: binary_sensor
    for:
      hours: 0
      minutes: 3
      seconds: 0
condition:
action:
  - choose:
      - conditions:
          - condition: trigger
            id: motion 
          - type: is_illuminance
            condition: device
            device_id: b92c7483c2136dca6f6237c19c441080
            entity_id: sensor.datchik_osveshcheniia_illuminance_lux
            domain: sensor
            below: 600
            enabled: true
          - condition: device
            type: is_off
            device_id: 0f010b6471e32de3b1c5166cceb80de4
            entity_id: switch.rozetka_osveshcheniia_rakoviny
            domain: switch
        sequence:
          - type: turn_on
            device_id: 0f010b6471e32de3b1c5166cceb80de4
            entity_id: switch.rozetka_osveshcheniia_rakoviny
            domain: switch
      - conditions:
          - condition: trigger
            id: no motion
        sequence:
          - type: turn_off
            device_id: 0f010b6471e32de3b1c5166cceb80de4
            entity_id: switch.rozetka_osveshcheniia_rakoviny
            domain: switch
mode: single

It is also possible to have similar functionality using a Wait for trigger action.

1 Like

Much simpler version as per @Didgeridrew’s modifications, but with the action templated and all of the device cruft removed:

alias: Автоматическое включение светом над раковиной
trigger:
  - platform: state
    entity_id: binary_sensor.datchik_dvizheniia_rakoviny_occupancy
    to: 'on'
  - platform: state
    entity_id: binary_sensor.datchik_dvizheniia_rakoviny_occupancy
    to: 'off'
    for:
      minutes: 3
condition:
  - condition: numeric_state
    entity_id: sensor.datchik_osveshcheniia_illuminance_lux
    below: 600
  - condition: state
    entity_id: switch.rozetka_osveshcheniia_rakoviny
    state: 'off'
action:
  - service: switch.turn_{{ states('binary_sensor.datchik_dvizheniia_rakoviny_occupancy') }}
    entity_id: switch.rozetka_osveshcheniia_rakoviny
1 Like

Thank you so much for your help. Are there video tutorials where I can learn automation?

You might find some on YouTube, but they risk being out of date as the system has evolved a lot. Better to use the extensive official documentation:

…and ask here for help if you get stuck.

1 Like

Tested automation. For some reason, your option does not work. ( ( Turns it on, but does not turn it off.
It seems that something is wrong with the system itself, since another automation has stopped working normally.

An example of a simple automation that suddenly stopped working.

alias: Включение вентиляция в ванной с кнопки
description: ""
trigger:
  - platform: device
    domain: mqtt
    device_id: e786d9c40f50cc77441cd90c09580692
    type: click
    subtype: single
    discovery_id: 0x00158d0001d87cb4 click_single
condition: []
action:
  - type: toggle
    device_id: 8446042dfdd4dfe2ad33a406427b9488
    entity_id: switch.vytiazhka_i_teplyi_pol_l2
    domain: switch
  - delay: "00:15:00"
  - type: toggle
    device_id: 8446042dfdd4dfe2ad33a406427b9488
    entity_id: switch.vytiazhka_i_teplyi_pol_l2
    domain: switch

How are you writing these automations — through the HA UI or manually into a file? Are there any errors in your logs?

There is a trace feature in the UI so that you can debug your automations, but for that to work for manually-written ones, you must have a unique id for each one, like this:

alias: Автоматическое включение светом над раковиной
id: bb229a3c-78eb-494c-9c96-f1bd54a1640d
trigger:
  - platform: state

The ID just needs to be unique on your system. I use this tool to avoid having to think up new ones.

Then you can inspect each run of your automation in a visual tool:

1 Like

I am writing automation through the Home Assistant user interface. But unfortunately I don’t know how to use the tracing function. I didn’t find a tutorial video. (((

I can open a trace. But I don’t understand how to use it

Here’s an example:

This automation has three triggers, shown by the three circles at the top. For this run, it was the middle one that fired: by clicking on it, I can see that it was triggered by the time being 09:00 (bottom right).

It then passed through two conditions (the A/B icons), which I can click on to see the details. Here’s the second one, which is a condition that the time is after 07:00:

image

…but the action is then blocked by the third condition (A/B, directing the flow to the X), that there is no-one at home — as there are two people at home, the action does not run:

image

You can use that to work out why your switch is not turning off after three minutes without motion — is it not triggering or is it being blocked by a condition?

The Trace Timeline gives a summary of the above:

1 Like

Thanks for the help, I will study my problem using tracing.