Best way to do this automation

Good night everyone, I’m new to HA and I’m here with a doubt about what will be the best way to do what I need. This is what I want to do:

  • I have two motion sensors, S1 and S2 let’s call them that
  • I have a light, L1
  • If S1 or S2 detect motion and L1 = off, then L1 ON for 1 minute
  • If S1 or S2 detect motion and L1 = on, then L1 resets the counter

I have a simple Automation, it don’t have the last part of restart the counter. This is what I have in YAML:

alias: Sensor entrada
description: ""
trigger:
  - type: motion
    platform: device
    device_id: 3629f990acc7161643468b8383e3ab64
    entity_id: binary_sensor.hall_de_entrada_mov_hall_109
    domain: binary_sensor
  - type: motion
    platform: device
    device_id: 3629f990acc7161643468b8383e3ab64
    entity_id: binary_sensor.corredor_quartos_mov_corredor_105
    domain: binary_sensor
condition:
  - condition: sun
    after: sunset
    enabled: false
  - condition: and
    conditions:
      - condition: device
        type: is_off
        device_id: 3629f990acc7161643468b8383e3ab64
        entity_id: light.hall_de_entrada_luz_parede_97
        domain: light
action:
  - type: turn_on
    device_id: 3629f990acc7161643468b8383e3ab64
    entity_id: light.hall_de_entrada_luz_parede_97
    domain: light
  - delay:
      hours: 0
      minutes: 1
      seconds: 0
      milliseconds: 0
  - type: turn_off
    device_id: 3629f990acc7161643468b8383e3ab64
    entity_id: light.hall_de_entrada_luz_parede_97
    domain: light
mode: single

Why not use entity states rather than devices? Something like:

trigger:
  - platform: state
    entity_id:
      - binary_sensor.hall_de_entrada_mov_hall_109
    to: "on"
  - platform: state
    entity_id:
      - binary_sensor.corredor_quartos_mov_corredor_105
    to: "on"
condition: []
action:
  - if:
      - condition: state
        entity_id: light.hall_de_entrada_luz_parede_97
        state: "off"
    then:
      - service: light.turn_on
        data: {}
        target:
          entity_id: light.hall_de_entrada_luz_parede_97
    else:
      - service: counter.reset
        data: {}
        target:
          entity_id: counter.xxxxxxx

It will be more reliable if you include the motion sensors’ being off as both a trigger and condition. This will help avoid a common situation where the time eclipses but one or more motion sensors haven’t completed their cool down period, so they are unable to trigger a reset of the timer.

trigger:
  - platform: state
    id: 'on'
    entity_id:
      - binary_sensor.corredor_quartos_mov_corredor_105
      - binary_sensor.hall_de_entrada_mov_hall_109
    to: "on"
  - platform: state
    id: 'off'
    entity_id:
      - binary_sensor.corredor_quartos_mov_corredor_105
      - binary_sensor.hall_de_entrada_mov_hall_109
    to: "off"
    for: "00:01:00"
  - platform: event
    id: 'off'
    event_type: timer.finished
    event_data:
      entity_id: timer.hall_de_entrada_luz
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: 'on'
          - condition: sun
            after: sunset 
          - condition: state
            entity_id: light.hall_de_entrada_luz_parede_97
            state: "off"
        sequence:
          - service: light.turn_on
            data: {}
            target:
              entity_id: light.hall_de_entrada_luz_parede_97
      - conditions:
          - condition: trigger
            id: 'off'
          - condition: state
            entity_id:
              - binary_sensor.corredor_quartos_mov_corredor_105
              - binary_sensor.hall_de_entrada_mov_hall_109
            state: "off"
        sequence:
          - service: light.turn_off
            data: {}
            target:
              entity_id: light.hall_de_entrada_luz_parede_97
  - condition: trigger
    id: 'on'
  - service: timer.start
    data:
      duration: "00:01:00"
    target:
      entity_id: timer.hall_de_entrada_luz
mode: queued
1 Like

Yeh. What he said. :grinning_face_with_smiling_eyes:

Thanks for the reply, because the entity is a fibre HC2 and have multiple devices :wink:

Ok, I really don’t understand nothing about HA. Entitys are working and the scene is working TOP.
Many thanks, gone analyse the code to make sure I understand it.