Run automation if device off for x minutes

Hi, I have an AppDaemon automation to turn on the light if my Fibaro sensor detects motion.
But, when I turn off the light using my IKEA On/Off switch and then after 1-2 seconds I pass in front of the motion sensor, the lights turns on automatically.

How can I fix this? Or how can I convert this HA automation into Python code?
Senza titolo

Please post the actual code, not just a screen shot. I would suggest posting the python code you have as well.

This is the yaml code which is working fine

- id: '1617606891790'
  alias: Accendi luce con movimento
  description: Accendi luce quando rilevato movimento, se dopo tramonto e se sono
    a casa
  trigger:
  - type: motion
    platform: device
    device_id: 2f6dfca69c6ff86393887743400fdd20
    entity_id: binary_sensor.motion_sensor_home_security_motion_detection
    domain: binary_sensor
  condition:
  - condition: device
    device_id: 1253ed607bd2c607768da157f2b4c5fc
    domain: device_tracker
    entity_id: device_tracker.iphone_12
    type: is_home
  - condition: sun
    after: sunset
  - condition: device
    type: is_off
    device_id: 391b45ffbb0e3bf511805c6bd5e4d70e
    entity_id: light.luce
    domain: light
    for:
      hours: 0
      minutes: 2
      seconds: 0
      milliseconds: 0
  action:
  - type: turn_on
    device_id: 391b45ffbb0e3bf511805c6bd5e4d70e
    entity_id: light.luce
    domain: light
  mode: single

but I’m trying to do the same with AppDaemon but I don’t know how to write the condition “if device is off for 2 minutes”
this is the Python code

import hassapi as hass

class MotionLights(hass.Hass):
    def initialize(self):
        self.listen_state(self.motion, "binary_sensor.motion_sensor_home_security_motion_detection", new = "on")

    def motion(self, entity, attribute, old, new, kwargs):
        light_level = self.get_state("sensor.motion_sensor_illuminance")
        if self.sun_down() or (light_level < 5):
            self.turn_on("light.luce")

A pretty simple solution to this would be using a custom constraint. I didn’t test this code but something like this might put you on the right track.

class MotionLights(hass.Hass):
    def initialize(self):
        self.register_constraint("switch_off_for")
        self.listen_state(self.motion, "binary_sensor.motion_sensor_home_security_motion_detection", new="on", switch_off_for=120)

    def switch_off_for(self, value):
        switch_state = self.get_state("light.luce", attribute="all")
        last_changed = switch_state["last_changed"]
        elapsed = (self.get_now() - self.convert_utc(last_changed)).seconds

        if elapsed >= value:
            return True
        return False

    def motion(self, entity, attribute, old, new, kwargs):
        light_level = self.get_state("sensor.motion_sensor_illuminance")
        if self.sun_down() or (light_level < 5):
            self.turn_on("light.luce")
1 Like

Hey! Works like a charm, thanks.

Awesome, glad it worked out. Enjoy! :+1:t2: