My light automation doesn't work

Hello, dear community. This is my first attempt at automation, and so far, it’s failing. I have three Tuya wifi motion sensors, two Tuya door sensors, and a Tuya light switch.

I have one automation for door sensors and one for motion sensors. I want the light to turn on whenever one of the doors opens and stay like that for 4 minutes regardless of whether the door closes afterward. The other one is for motion sensors. I want the light to keep on as long as a motion is detected on either of the three sensors and keep it on for 4 minutes before turning it off.

My problem is that sometimes(most of the time), the light doesn’t turn on even though the motion sensor sees the movement and reports it to HA. Sometimes, it doesn’t turn to either also, regardless of the fact that the HA registers the change in the sensor state.

Here’s the code for motion sensor:

alias: Garage Lights Control by Motion
description: >-
  Keep garage lights on when motion is detected and turn off after no motion for
  4 minutes.
trigger:
  - platform: state
    entity_id:
      - binary_sensor.senzor_prisutnosti_garaza_1_motion
      - binary_sensor.senzor_prisutnosti_garaza_2_motion
      - binary_sensor.senzor_prisutnosti_garaza_3_motion
    to: "on"
condition: []
action:
  - service: switch.turn_on
    target:
      entity_id: switch.garage_switch_1
    data: {}
  - wait_for_trigger:
      - platform: state
        entity_id: binary_sensor.senzor_prisutnosti_garaza_1_motion
        to: "off"
        for: "00:04:00"
      - platform: state
        entity_id: binary_sensor.senzor_prisutnosti_garaza_2_motion
        to: "off"
        for: "00:04:00"
      - platform: state
        entity_id: binary_sensor.senzor_prisutnosti_garaza_3_motion
        to: "off"
        for: "00:04:00"
    continue_on_timeout: false
  - service: switch.turn_off
    target:
      entity_id: switch.garage_switch_1
    data: {}
mode: restart

And the code for the doors:

alias: Otvaranje vrata u garaži
description: ""
trigger:
  - type: opened
    platform: device
    device_id: bbc129be15f4a821e9a7fc00da804cfa
    entity_id: c39d4fe6099929ba1d35227f2f705eca
    domain: binary_sensor
  - type: opened
    platform: device
    device_id: c6643925d5f0e5e81315e950fb62591b
    entity_id: 06e8656446a0ca0e58d3c6d721ed4106
    domain: binary_sensor
condition:
  - condition: device
    type: is_off
    device_id: 998d3dba3138453c8ccba3f1c83eba52
    entity_id: 52836c3bf856f372bb65f50b46dedd93
    domain: switch
action:
  - type: turn_on
    device_id: 998d3dba3138453c8ccba3f1c83eba52
    entity_id: 52836c3bf856f372bb65f50b46dedd93
    domain: switch
  - delay:
      hours: 0
      minutes: 2
      seconds: 0
      milliseconds: 0
  - type: turn_off
    device_id: 998d3dba3138453c8ccba3f1c83eba52
    entity_id: 52836c3bf856f372bb65f50b46dedd93
    domain: switch
mode: single

Thank you all for your time in advance.
Nik

Create a binary sensor group helper for your motion sensors.

alias: Garage Lights Control by Motion
description: >-
  Keep garage lights on when motion is detected and turn off after no motion for
  4 minutes.
trigger:
  - platform: state
    entity_id: binary_sensor.sva_prisutnost_garaze
    to: "on"
    id: "on"
  - platform: state
    entity_id: binary_sensor.sva_prisutnost_garaze
    to: "off"
    for: "00:04:00"
    id: "off"
condition: []
action:
  - service: switch.turn_{{ trigger.id }}
    target:
      entity_id: switch.garage_switch_1
    data: {}
mode: restart

If you don’t want the lights to turn off after being turned on by the Door sensor remove the “turn off” action.

Thank you. Let me try it. I’ll let you know. I’m removing the “turn off” action as well.

I tried and haven’t succeeded. This is my code:

binary_sensor:
  - platform: template
    sensors:
      garage_motion_group:
        friendly_name: "Garage Motion Group"
        device_class: motion
        value_template: >-
          {{ is_state('binary_sensor.senzor_prisutnosti_garaza_1_motion', 'detected') or
             is_state('binary_sensor.senzor_prisutnosti_garaza_2_motion', 'detected') or
             is_state('binary_sensor.senzor_prisutnosti_garaza_3_motion', 'detected') }}
        delay_off:
          minutes: 4

I have tried with both “on” and “detected”. Both don’t give me any results.
Here are the screenshots:

Here’s the timeline of one of my sensors. Based on the photo, the status should change.

There are a couple issues there, but the main one is that you are using the wrong Helper… You want the Group helper:

image


If you really want to do it as a Template Binary sensor that uses delay_off, you need to do it in the YAML configuration, not the Template Helper creator. The Template Helper does not currently allow advanced configuration features like delay_off, auto_off, or trigger.

FWIW, only the state template should be in the State template box, no YAML configuration should be there:

One of the other errors you’ve made is putting the full YAML config into the state template box in the template sensor.

It should just be the Jinja template — the bit under value_template.

But as Drew says, that’s not what you should be using anyway.

Thank you guys, I believe it’s working well now! :slight_smile:

This community and in my case both of you are very open and willing to help. I will follow your example.

Hello from Serbia!