Help me, what I doing wrong?

Hello, I want to do an automation so that the light comes on (30 minutes before dark) and that it is active for 30 seconds or more if it detects movement, but I don’t know what happens that it doesn’t work for me, I don’t know what I’m doing wrong. I need help. Here I leave the code.

alias: "Sensor ENCENDIDO luz entrada "
description: >-
  Detecta presencia 30 minutos antes del anochecer y enciende la luz de la
  entrada durante 30 segundos
triggers:
  - type: occupied
    device_id: 901eac47d8be1bc8f2f0cc17adecc9e5
    entity_id: a9948453cd8aeb0baa1fdb108ab2aae6
    domain: binary_sensor
    trigger: device
    for:
      hours: 0
      minutes: 0
      seconds: 30
conditions:
  - condition: sun
    after: sunset
    after_offset: "-00:30:00"
    before: sunset
actions:
  - type: turn_on
    device_id: 815c2082ba4a19fcf387ac1d86f8c5f7
    entity_id: 6d7691d9e4bca9714c052a79b5ff7fb6
    domain: switch
mode: single

The motion sensor is a Tuya TS0202
The smart switch is a SonOff ZBMINI
Thank you

Before sunset and after sunset may be the issue. :slight_smile:

use sun - below horizon instead.

try this:


alias: "Sensor ENCENDIDO luz entrada "
description: "Detecta presencia 30 minutos antes del anochecer y enciende la luz de la entrada durante 30 segundos"
triggers:
  - trigger: state
    entity_id:
      - binary_sensor.tuya_ts0202_detector   # change this to the correct entity_id
    from: "off"
    to: "on"
conditions:
  - condition: sun
    after: sunset
    after_offset: "-00:30:00"
actions:
  - type: turn_on
    device_id: 815c2082ba4a19fcf387ac1d86f8c5f7
    entity_id: 6d7691d9e4bca9714c052a79b5ff7fb6
    domain: switch
  - delay:
      hours: 0
      minutes: 0
      seconds: 30
  - type: turn_off
    device_id: 815c2082ba4a19fcf387ac1d86f8c5f7
    entity_id: 6d7691d9e4bca9714c052a79b5ff7fb6
    domain: switch
mode: restart


Hello, thanks for answering. I’m going to try this code to see if it goes better

alias: Luz Entrada Automatizacion Presencia
description: "Detecta presencia 30 minutos antes del anochecer y apaga la luz de la entrada "
triggers:
  - entity_id: binary_sensor.sensor_de_movimiento_entrada_occupancy
    to: "on"
    trigger: state
  - entity_id: binary_sensor.sensor_de_movimiento_entrada_occupancy
    to: "off"
    for:
      seconds: 30
    trigger: state
conditions:
  - condition: sun
    after: sunset
    after_offset: "-00:30:00"
    before: sunset
actions:
  - choose:
      - conditions:
          - condition: state
            entity_id: binary_sensor.sensor_de_movimiento_entrada_occupancy
            state: "on"
        sequence:
          - target:
              entity_id: switch.conmutador_entrada
            action: switch.turn_on
            data: {}
      - conditions:
          - condition: state
            entity_id: binary_sensor.sensor_de_movimiento_entrada_occupancy
            state: "off"
        sequence:
          - target:
              entity_id: switch.conmutador_entrada
            action: light.turn_off
            data: {}
mode: restart