Trigger on motion, depending on lux and sun = action

Hi,

I guess this is suited for a beginner like me :slight_smile:
I found a few blueprints that would work with a schedule which may be handy for some, but as the sun is changing these value would need constant adjustment wheras I was looking for an automation.

Moreover, other blueprints were often grown over time and so it was impossible for me to amend it to my needs as these were too complex for my know how. So, this one is very basic with descriptions and alias inside, so even months later I will understand, when looking at it.

It reduces brightness before turning off. A signal for you in case you want to keep the light on, but the sensor doesn’t know you are still in the room.

I guess I have this for tile on my dashboard, not for the automation.
Go to your file configuration.yaml and add this:

# User area

template:
  - sensor:
      - name: "Sun - rise time"
        state: "{{ as_timestamp(state_attr('sun.sun', 'next_rising')) | timestamp_custom('%-I:%M', true ) }}"

Continuation of the blueprint

Motion-Light-Sun-Brightness (HA-m-l-s_b-00.yaml)

https://gist.github.com/tido-/205b1e24f38172034888d461fcbbdd24

Some of my notes that helped me to use the editor within Home Assistant to build a draft.

Input

link
These are the input fields that your blueprint can provide.
The input_select integration allows the user to define a list of values that can be selected via the frontend and can be used within conditions of an automation. When a user selects a new item, a state transition event is generated. This state event can be used in an automation trigger.

Target

empty

Mode

empty

Trigger

The trigger(s) which will start the automation. Multiple triggers can be added and the automation will start when any of these triggers trigger.

Conditions

For example, a condition can test if a switch is currently turned on or off.

Unlike a trigger, which is always or, conditions are and by default - all conditions have to be true.
All conditions support an optional alias.

Action

The sequence of actions to be performed in the script.

brightness_pct = Percent

Template condition

The template condition tests if the given template renders a value equal to true. This is achieved by having the template result in a true boolean expression or by having the template render True.

Sequence (auch Teil eines Skripts)

Scripts are a sequence of actions that Home Assistant will execute. In scripts, an action is called sequence.

HA-m-l-s_b_20240522-1

blueprint:
  name: Trigger on motion, depending on lux and sun = action
  description: Motion triggers, if there is not enough light, based on the position of the sun you can choose the brightness. To adjust the offset of dawn or dusk you have to edit the file in /config/blueprints/automation/...
  domain: automation
  author: tido
  source_url: https://gist.github.com/tido-/205b1e24f38172034888d461fcbbdd24

  input:
    motion_entity:
      name: Motion Sensor
      description: This sensor detects motion.
      selector:
        entity:
          domain: binary_sensor
          device_class: motion
    illuminance_sensor:
      name: Illuminance Sensor
      description: This sensor serves illuminance (lux).
      default:
      selector:
        entity:
          domain: sensor
          device_class: illuminance
    illuminance_trigger:
      name: Illuminance Threshold
      description: The threshold (0 - 500) to compare to the current illumination (lux above this value NO light).
      default: 37
      selector:
        number:
          min: 0
          max: 500
          unit_of_measurement: lx
    light_target:
      name: Light Source
      selector:
        target:
          entity:
            domain: light
    day_brightness:
      name: In Day Brightness
      description: Brightness, in day time period.
      default: 81
      selector:
        number:
          min: 1.0
          max: 100.0
    night_brightness:
      name: In Night Brightness
      description: Brightness, in night time period.
      default: 69
      selector:
        number:
          min: 1.0
          max: 100.0
    no_motion_wait:
      name: Wait Time
      description: Time to leave the light on after last motion is detected.
      default: 360
      selector:
        number:
          min: 0
          max: 3600
          unit_of_measurement: seconds

# If motion is detected within the Countdown, restart the automation.
mode: restart
trace:
    stored_traces: 30

trigger:
  - platform: state
    entity_id: !input motion_entity
    from: "off"
    to: "on"

condition:
  - condition: numeric_state
    entity_id: !input illuminance_sensor
    below: !input illuminance_trigger

action:
  - choose:
      - conditions:
          - alias: Illumination from dawn (day time period).
            condition: sun
            after: sunrise
            before: sunset
            after_offset: "00:30:00"
        sequence:
          - service: light.turn_on
            data:
              brightness_pct: !input day_brightness
            target: !input light_target

      - conditions:
          - alias: Illumination from dusk (night time period).
            condition: sun
            after: sunset
            before: sunrise
            after_offset: "-00:30:00"
            before_offset: "00:30:00"
        sequence:
          - service: light.turn_on
            data:
              brightness_pct: !input night_brightness
            target: !input light_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: Countdown, restarts upon motion_sensor signal
    delay: !input no_motion_wait
  - alias: Reduce brightness before turn off.
    service: light.turn_on
    data:
      brightness_pct: 25
      transition: 3
    target: !input light_target
  - alias: Countdown, before off.
    service: light.turn_off
    data:
      transition: 41
    target: !input light_target
1 Like