Motion illuminance switch

Tags: #<Tag:0x00007fc41ecd7c08> #<Tag:0x00007fc41ecd7b40> #<Tag:0x00007fc41ecd7a78> #<Tag:0x00007fc41ecd79b0>

Base on the official blueprint and this Turn on light, switch, scene, script or group based on motion and illuminance (+ more conditions).

I write my first blueprint script cause I need to detect both motion and light sensor, and I
do not want to create too many help elements.

blueprint:
  name: Motion and illuminance activated Light/Switch
  description: Turn on a light or switch based on motion and illuminance.
  domain: automation
  source_url: https://community.home-assistant.io/t/motion-illuminance-switch/296450
  input:
    motion_entity:
      name: Motion Sensor
      selector:
        entity:
          domain: binary_sensor
          device_class: motion
    turn_on_target:
      name: Turn-on target
      selector:
        target:
          entity: {}
    turn_off_target:
      name: Turn-off target
      description: In general, the turn-on and turn-off target should be the same.
      selector:
        target:
          entity: {}
    illuminance_sensor:
      name: (OPTIONAL) Illuminance sensor
      description: This sensor will be used to determine the illumination.
      default:
      selector:
        entity:
          domain: sensor
          device_class: illuminance
    illuminance_cutoff:
      name: (OPTIONAL) Illuminance cutoff value
      description: This input_number will be used to compare to the current illumination
        to determine if it is low.
      default: 0
      selector:
        number:
          min: 0
          max: 10000
          step: 1
          unit_of_measurement: lux
          mode: slider
    time_after:
      name: Run after time.
      default: '00:00:00'
      selector:
        time: {}
    time_before:
      name: Run before time.
      default: '23:59:59'
      selector:
        time: {}
    no_motion_wait:
      name: Wait time
      description: Time to leave the target on after last motion is detected.
      default: 3
      selector:
        number:
          min: 0.5
          max: 360.0
          step: 0.5
          unit_of_measurement: minutes
          mode: slider
mode: restart
max_exceeded: silent
variables:
  turn_on_target: !input 'turn_on_target'
  illuminance_sensor: !input 'illuminance_sensor'
  illuminance_cutoff: !input 'illuminance_cutoff'
trigger:
  platform: state
  entity_id: !input 'motion_entity'
  to: 'on'
condition:
- condition: time
  after: !input 'time_after'
  before: !input 'time_before'
- condition: or
  conditions:
    - "{{ is_state(turn_on_target,'on') }}"
    - "{{ illuminance_cutoff|int == 0 }}"
    - "{{ illuminance_sensor != None and (states(illuminance_sensor)|int < illuminance_cutoff|int) and (states(illuminance_sensor)!= 'unavailable') }}"
action:
- alias: Turn on the target
  service: homeassistant.turn_on
  target: !input 'turn_on_target'
- alias: Wait until there is no motion from device
  wait_for_trigger:
    platform: state
    entity_id: !input 'motion_entity'
    from: 'on'
    to: 'off'
- alias: Wait the number of seconds that has been set
  delay:
    minutes: !input 'no_motion_wait'
- alias: Turn off the target
  service: homeassistant.turn_off
  target: !input 'turn_off_target'