Turn on light, switch, scene, script or group based on motion, illuminance, sun (+ more conditions)

@freakshock I’m testing Home Assistant 2022.6 beta now, and I found that your blueprint does not turn off the lights now. They turn on at motion and at the correct luminance, but they don’t turn off. Anyone else experiencing this with this blueprint?

It all worked in previous HA versions, but as soon as I installed 2022.6 all automations with this blueprint started to behave the same. No changes to the system was made just before installing 2022.6, so something in that version has changed that in turn affects this blueprint, I guess.

’ Error: ValueError: Template error: int got invalid input ‘None’ when rendering template ‘{{ states[no_motion_wait].state | int }}’ but no default was specified’

I think this is the problem but no clue how to solve this.

1 Like

Have you guys updated to the latest version of the blueprint?
I think this error might only occur because an older version of the blueprint did not define default values in templates. I see that the template that Remco is providing from the logs is missing the |int(0) part which is in the latest version of the blueprint.
Please try to update the blueprint (delete and readd it) to the latest version and try again.
If that still does not work let me know (and provide logs if you can).

1 Like

Have updated and unfortunately no improvement; attached the logs: trace automation.bewegung_kuche 2022-05-30T19_13_12.168651+00_00.json - Pastebin.com

1 Like

For me it seems solved when I updated it. I will test more though, I only tested on one light.

How can I set the Blocking Entity to something that opens/closes as opposed to on/off?

Hi,
I’ve just set up the automation along with time constrains. Tested it earlier, and it worked great, but now after setting before time to 2am and after time to 8am it doesn’t seem to work, lights are still turned on, and it’s after 2am. I tried changing to 1am with no success, but when I tried before time to 11pm, scripts correctly halted and didn’t turn on the lights.
Any ideas what can I be doing wrong, or how can I help to find the issue?

alias: Bathroom light on motion - day
description: ''
use_blueprint:
  path: freakshock88/motion_illuminance_activated_entity.yaml
  input:
    motion_sensor: binary_sensor.sensor_bathroom_motion_occupancy
    target_entity: switch.tasmota2
    turn_off_blocker_entity: binary_sensor.bathroom_motion_turn_off_blocker
    time_limit_after: input_datetime.bathroom_motion_after_time
    time_limit_before: input_datetime.bathroom_motion_before_time
    no_motion_wait: input_number.bathroom_motion_turn_off_time

True after 2AM:


False after 8AM:

Some more testing revealed that after 10AM local time it works only if I set ‘after’ to 8AM - it stops working for 9AM or 10AM put in the field. I’m in GMT+2 now, so it seems like it ignores timezones - which means that all timing will break when we switch to winter time.

Is there a solution to that?

EDIT: Resolved, I didn’t have timezone setup in general Home Assistant settings.

I have two questions, the “no_motion_wait” have to be seconds instead of minutes. I have a motion Sensor in the floor and 1min is really to much. 15sec totally fine. But is not possible with the default blueprint.

Is there any advantage to use a helper for e.g. Illuminance cutoff value, Turn off wait time (minutes) instead of a direct input? I just find it unnecessarily complicated to have to create an extra helper.

Hey,
I’ve been having an automation problem since I accelerated the lighting sensor update. My light turns on under defined conditions, but it won’t turn off anymore. I assume that it doesn’t turn off because immediately after the light is turned on, the light level has increased above the defined threshold and thus the condition is not met. Can anyone advise?

Hello, I am new to the Home Assistant topic and this script is exactly what I am looking for. However, when I set it up, it does not work. Are there any problems with the script or am I doing something wrong?

id: ‘1654469431516’
alias: Küchenlicht Lux
description: ‘’
use_blueprint:
path: freakshock88/motion_illuminance_activated_entity.yaml
input:
motion_sensor: binary_sensor.bewegungsmelder_kuche_25651609_occupancy
illuminance_sensor: sensor.bewegungsmelder_kuche_25651609_illuminance
illuminance_cutoff: ‘30’
target_entity: light.kuche_level_light_color_on_off
no_motion_wait: ‘1’

Looks like I figured it out. The light doesn’t turn off when I don’t use the optional parameter “Turn off wait time”. I require the light to turn off immediately after the motion sensor clears. When I set the parameter “Turn off wait time”, it works and the light turns off.

You can create a template sensor for that, and use that als the blocking entity.
A YAML example:

template:
  - binary_sensor:
      - name: "Cover is open"
        state: "{{ is_state('cover_kitchen_blinds', 'open') }}"

1 Like

As far as I’m reading in the docs, input_number is a float so you vould also set it to 0.25 to set the wait time to 15 seconds. I agree seconds might have been a more logical design choice for this template, but I don’t want to change it now and break all existing automations when people update.

As for having a helper entities for some configuration, I like to sometimes change the values without having to change the automation code. But I understand it might be a bit overkill for most people. I might look into also supporting hardcoded values in the future, but in the next few weeks I will not have time for it.

Read the first post again. You cannot use hardcoded values for illuminance_cutoff or no_motion_wait, you need to create input_number helper entities for that.

I have configured this automation in my kitchen to turn off the lights if no motion is detected after 20 minutes. I have a helper defined with the input_number 20 however at the 20 minute mark the lights turn off as per the automation but it also turns off even though there has been motion during the 20 minutes. I thought if there is motion detected then the timer resets and the 20 minutes is extended for another 20 minutes if there is no motion detected and it will turn off.

That should not happen. If the motion detector state turns ‘on’ within those 20 minutes, the automation (so also the ‘timer’) should restart. Have you tried reinstalling the blueprint? Maybe you don’t have the latest version installed.

Thanks for your reply, here’s an idea for the seconds problem. Maybe you could let the user choose a number and additionally the unit (seconds, minutes etc…) Default then minutes so there would be no BC.

Currently I am struggling with a very strange problem, I use Z2M and the Philips HUE sends motion and current light value in one message:

{"battery":100,"illuminance":11193,"illuminance_lux":13,"last_seen":1655550796391,"led_indication":false,"linkquality":58,"motion_sensitivity":"high","occupancy":true,"occupancy_timeout":0,"temperature":25.6,"update":{"state":"idle"},"update_available":null}

This leads (I think) currently to the fact that the light does not always turn on. Because when processing the message, the current light value is used and not the new which is in the message. I do not know, but is there something like:

{% set illuminance = max(payload.illuminance_lux, states(illuminance_currently)) %}?

k, I found out what exactly the problem is. It is random whether the Philips HUE Sensor/Z2M sends motion or light status first. Since your automation only responds to motion, it may be that motion is transmitted first and then the current brightness. If you have set a lux threshold, the automation will fail.

For this reason, it makes sense to pay attention to movement and illuminance separately. What if you move in a room, the light is bright enough. Then you close the door and it is too dark. But movement is still “on” at this time.

I have created an automation that solves this problem. Maybe some suggestions for your blueprint.

Bonus: There is a night mode where the light comes on in 2 levels so you don’t burn your eyes in the middle of the night :wink:

About the Trigger: I know there is a Trigger with "Device → Motion Sensor (Illuminance) → Above/Below. And it can make this task more simple, but the issue is, that the Trigger NOT allow to use a input helper. I hope that the add it in the future (like the Trigger Numeric-Statechnage).

if (timer.finsihed) {
  service: homeassistant.turn_off
} else if (motion.on && light = on) {
  service: timer.start (timer.turn_off_delay)
} else if (motion.off && timer.active) {
  service: timer.cancel (timer.turn_off_delay)
} else if (light == off  &&  lux < threshold  && (motion.on || (lux.changed && motion.state = on))) {
  if (binary_sensor.night_mode) {
    service: sence.night_light
    delay: 4sec
    service: sence.normal_light
  } else {
    default: sence.normal_light
  }
}
alias: Motion Sensor
description: ''
mode: restart
trigger:
  - type: motion
    platform: device
    device_id: 6a08acb8b11fc3b6f3a782801462f7cf
    entity_id: binary_sensor.bewegungssensor_flur_occupancy_2
    domain: binary_sensor
    id: motion.on
  - platform: state
    entity_id:
      - sensor.bewegungssensor_flur_illuminance_lux_2
    id: lux.changed
  - type: no_motion
    platform: device
    device_id: 6a08acb8b11fc3b6f3a782801462f7cf
    entity_id: binary_sensor.bewegungssensor_flur_occupancy_2
    domain: binary_sensor
    id: motion.off
  - platform: event
    id: turn.off
    event_type: timer.finished
    event_data:
      entity_id: timer.flurlicht_turn_off_delay
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: turn.off
        sequence:
          - service: homeassistant.turn_off
            data: {}
            target:
              area_id: flur
      - conditions:
          - condition: trigger
            id: motion.off
          - condition: device
            type: is_on
            device_id: 5e789787ac1435d40f4b2944f9bdcafb
            entity_id: light.flur_deckenlicht_2
            domain: light
        sequence:
          - service: timer.start
            data: {}
            target:
              entity_id: timer.flurlicht_turn_off_delay
      - conditions:
          - condition: trigger
            id: motion.on
          - condition: state
            entity_id: timer.flurlicht_turn_off_delay
            state: active
        sequence:
          - service: timer.cancel
            data: {}
            target:
              entity_id: timer.flurlicht_turn_off_delay
      - conditions:
          - condition: and
            alias: Licht ist aus und Bewegung erkannt oder Raum zu dunkel
            conditions:
              - condition: device
                type: is_off
                device_id: 5e789787ac1435d40f4b2944f9bdcafb
                entity_id: light.flur_deckenlicht_2
                domain: light
              - condition: numeric_state
                entity_id: sensor.bewegungssensor_flur_illuminance_lux_2
                below: input_number.flurlicht_lux_threshold
              - condition: or
                alias: Bewegung erkannt oder Raum zu dunkel
                conditions:
                  - condition: trigger
                    alias: Bewegung erkannt
                    id: motion.on
                  - condition: and
                    alias: Raum zu dunkel
                    conditions:
                      - condition: trigger
                        id: lux.changed
                      - condition: state
                        entity_id: binary_sensor.bewegungssensor_flur_occupancy_2
                        state: 'on'
        sequence:
          - choose:
              - conditions:
                  - condition: state
                    entity_id: binary_sensor.flurlicht_night_mode
                    state: 'on'
                sequence:
                  - service: scene.turn_on
                    target:
                      entity_id: scene.flurlicht_nachtmodus
                    metadata: {}
                  - delay:
                      hours: 0
                      minutes: 0
                      seconds: 4
                      milliseconds: 0
                  - service: scene.turn_on
                    target:
                      entity_id: scene.flurlicht_normal
                    metadata: {}
            default:
              - service: scene.turn_on
                target:
                  entity_id: scene.flurlicht_normal
                metadata: {}
    default: []

Hi,
I do have the same issue when using ‘scenes’ or ‘scripts’ to turn lights on. The lights do turn off after the timer runs down, if no motion was detected only for a moment, a new motion during the time period does not reset the time.
BUT when using individual lights or group of lights everything is working as expected and any movement resets the timer.
Is that by design or should the timer also restart when using ‘scenes’ or ‘scripts’?

This is probably the same bug we had before which has been fixed now. Have you tried updating to the latest version of the blueprint (reimporting it)?