Entity (light/switch) off after n minutes after last motion detected

I have a blueprint for switchting off an entity after selected value of minutes, but not so really happy with the solution.

Do you see how to improve the blueprint ?

Use case: The light should be switched off after defined time since last detected motion (since motion sensor swichted from “on” to “off”), but also, the light should be switched off after some time, if the light was switched on manualy, and therfore the motion sensor has not detected motion.

blueprint:
  name: Entity (light/switch) OFF after n minutes after last motion detected
  domain: automation
  input:
    motion_entity_group:
      name: Motion Sensor Group
      selector:
        entity:
          domain: group
    light_target:
      name: Entity to be switched
      description: The light, switch, scene to turn on (or script to run) when the automation is triggered.
      selector:
        entity:
    no_motion_wait:
      name: Wait time
      description: Time to leave the light on after last motion is detected.
      default: 3
      selector:
        number:
          min: 0
          max: 60
          step: 1
          unit_of_measurement: minutes
    no_motion_wait_at_night:
      name: Wait time at night
      description: Time to leave the light on after last motion is detected at night.
      default: 1
      selector:
        number:
          min: 0
          max: 20
          step: 1
          unit_of_measurement: minutes
mode: restart
max_exceeded: silent
#variables:
#  wait_duration_in_minutes: !input no_motion_wait
#  wait_duration_in_seconds: '{{ wait_duration_in_minutes * 60}}'

trigger:
  - platform: state
    entity_id: !input 'light_target'
    to: 'on'
    from: 'off'
condition: []
action:
  - choose:
      - conditions:
          - condition: state
            entity_id: !input motion_entity_group
            state: 'on'
        sequence:
          - choose:
              - conditions:
                  - condition: time
                    after: '00:00'
                    before: '06:00'
                sequence:
                  - wait_for_trigger:
                      - platform: state
                        entity_id: !input motion_entity_group
                        from: 'on'
                        to: 'off'
                        for:
                          minutes: !input no_motion_wait_at_night
            default:
              - wait_for_trigger:
                  - platform: state
                    entity_id: !input motion_entity_group
                    from: 'on'
                    to: 'off'
                    for:
                      minutes: !input no_motion_wait
    default:
      - choose:
          - conditions:
              - condition: time
                after: '00:00'
                before: '06:00'
            sequence:
              - wait_for_trigger:
                  - platform: state
                    entity_id: !input motion_entity_group
                    from: 'off'
                    to: 'on'
                timeout:
                  minutes: !input no_motion_wait_at_night
        default:
          - wait_for_trigger:
              - platform: state
                entity_id: !input motion_entity_group
                from: 'off'
                to: 'on'
            timeout:
              minutes: !input no_motion_wait
        #if motion detected, we have to wait again
      - choose:
          - conditions:
              - condition: state
                entity_id: !input motion_entity_group
                state: 'on'
            sequence:
              - choose:
                  - conditions:
                      - condition: time
                        after: '00:00'
                        before: '06:00'
                    sequence:
                      - wait_for_trigger:
                          - platform: state
                            entity_id: !input motion_entity_group
                            from: 'on'
                            to: 'off'
                            for:
                              minutes: !input no_motion_wait_at_night
                default:
                  - wait_for_trigger:
                      - platform: state
                        entity_id: !input motion_entity_group
                        from: 'on'
                        to: 'off'
                        for:
                          minutes: !input no_motion_wait                  
        default: []
  - delay: '00:00:01'
  - service: homeassistant.turn_off
    data: {}
    entity_id: !input 'light_target'

1 Like

Hey i been looking for something like this. My only issue is you use Motion Group, any chance of just allowing single motion entities?

Hi
Easiest would be to go to blueprint directly and edit it:

/config/blueprints/automation/iwanttobeafriendofha/entity-light-switch-off-after-n-minutes-after-last-motion-detected.yaml

and then just change domain for motion sensor and add domain for light. LIke this:

blueprint:
  name: Entity (light/switch) OFF after n minutes after last motion detected
  domain: automation
  input:
    motion_entity_group:
      name: Motion Sensor
      selector:
        entity:
          domain: binary_sensor
          device_class: motion
    light_target:
      name: Entity to be switched
      description: The light, switch, scene to turn on (or script to run) when the
        automation is triggered.
      selector:
        entity:
          domain: light
    no_motion_wait:
      name: Wait time
      description: Time to leave the light on after last motion is detected.
      default: 3
      selector:
        number:
          min: 0.0
          max: 60.0
          step: 1.0
          unit_of_measurement: minutes
          mode: slider
    no_motion_wait_at_night:
      name: Wait time at night
      description: Time to leave the light on after last motion is detected at night.
      default: 1
      selector:
        number:
          min: 0.0
          max: 20.0
          step: 1.0
          unit_of_measurement: minutes
          mode: slider
  source_url: https://community.home-assistant.io/t/entity-light-switch-off-after-n-minutes-after-last-motion-detected/360129
mode: restart
max_exceeded: silent
trigger:
- platform: state
  entity_id: !input 'light_target'
  to: 'on'
  from: 'off'
condition: []
action:
- choose:
  - conditions:
    - condition: state
      entity_id: !input 'motion_entity_group'
      state: 'on'
    sequence:
    - choose:
      - conditions:
        - condition: time
          after: 00:00
          before: 06:00
        sequence:
        - wait_for_trigger:
          - platform: state
            entity_id: !input 'motion_entity_group'
            from: 'on'
            to: 'off'
            for:
              minutes: !input 'no_motion_wait_at_night'
      default:
      - wait_for_trigger:
        - platform: state
          entity_id: !input 'motion_entity_group'
          from: 'on'
          to: 'off'
          for:
            minutes: !input 'no_motion_wait'
  default:
  - choose:
    - conditions:
      - condition: time
        after: 00:00
        before: 06:00
      sequence:
      - wait_for_trigger:
        - platform: state
          entity_id: !input 'motion_entity_group'
          from: 'off'
          to: 'on'
        timeout:
          minutes: !input 'no_motion_wait_at_night'
    default:
    - wait_for_trigger:
      - platform: state
        entity_id: !input 'motion_entity_group'
        from: 'off'
        to: 'on'
      timeout:
        minutes: !input 'no_motion_wait'
  - choose:
    - conditions:
      - condition: state
        entity_id: !input 'motion_entity_group'
        state: 'on'
      sequence:
      - choose:
        - conditions:
          - condition: time
            after: 00:00
            before: 06:00
          sequence:
          - wait_for_trigger:
            - platform: state
              entity_id: !input 'motion_entity_group'
              from: 'on'
              to: 'off'
              for:
                minutes: !input 'no_motion_wait_at_night'
        default:
        - wait_for_trigger:
          - platform: state
            entity_id: !input 'motion_entity_group'
            from: 'on'
            to: 'off'
            for:
              minutes: !input 'no_motion_wait'
    default: []
- delay: 00:00:01
- service: homeassistant.turn_off
  data: {}
  entity_id: !input 'light_target'

Should work :slight_smile:

I would love this too!