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

The blueprint with a normal entitie work fine, but with the scene the automation doesen’t get triggerd.


Im getting a similar problem…works fine with a single light for testing, but trying to call a script seems to stop it running

Trying to understand all of this logic is a little beyond me atm , but it ‘seems’ to be checking if an on/off state can be recorded unless its a scene or a script. Which it is. So shouldnt fail on that ?

Ok ive got it working by blindly hacking code :wink:

So i determined it was that condition check that was causing the problem, and I kind of figured it was all to do with illuminance checking…and I dont need that.

So I copied the blueprint and ripped that section out , saved it as a new one and built an automation off it-

- condition: template
  value_template: '{% set illuminance_defined = illuminance_currently != none and
    illuminance_cutoff != none  %} {% set illuminance_defined_and_low = (illuminance_defined
    and (states[illuminance_currently].state | int(0) < states[illuminance_cutoff].state
    | int(0)))  %}

    {% set target_entity_domain_supports_on_state_check = entity_domain != ''scene''
    and entity_domain != ''script'' %} {{  ( target_entity_domain_supports_on_state_check
    and states[target_entity].state == ''on'') or ( target_entity_domain_supports_on_state_check
    and states[target_entity].state == ''off'' and not illuminance_defined) or ( target_entity_domain_supports_on_state_check
    and states[target_entity].state == ''off'' and illuminance_defined_and_low) or
    (not target_entity_domain_supports_on_state_check and illuminance_defined_and_low)
    }}

    '

And now it works calling a script!

This is probably sacrilege and I may have broken something im not fully aware of yet, but I was pretty proud of myself being only 2 weeks into HA :wink:

@daschmidt , @Groove
It seems I missed a condition for when the entity was a script or scene, and no illuminance was defined. Please import the updated blueprint and check if it works for you now.

2 Likes

@freakshock BOOM! Working! Thanks man…awesome support

1 Like

thanks work now :slight_smile:

I saw you use homeassistant.turn_off to turn off the sene but this dosen’t work by me

Read the docs, if the target entity is a scene, you need to also define a turn off entity. Because indeed, scenes cannot be turned off.

1 Like

I would love to be able to set a light colour and intensity given different times of day and night.

For example after midnight until 7 set light to 10% when motion is trggered.

2 Likes

that’s right, I modified the config file:

  - choose:
  - conditions:
    - condition: template
      value_template: '{{ (target_off_entity != none) }}'
    sequence:
    - service: homeassistant.turn_on
      entity_id: !input 'target_off_entity'

Check this part of the newly added F.A.Q.

Hello! This blueprint sounds really good! I am having a very hard time setting up time to limit the light going on all day… My YAML:

alias: chandelier
description: ''
use_blueprint:
  path: freakshock88/motion_illuminance_activated_entity.yaml
  input:
    motion_sensor: binary_sensor.zooz_multipurpose_sensor_motion
    target_entity: light.chandelier
    time_limit_after: '14:00:00'

What am i doing wrong, please help! I have tried ‘14:00’ too. Error:

Executed: April 22, 2022, 4:06:42 PM
Error: In 'template' condition: TemplateError: str: Invalid domain name '14:00:00'

I tripped on this one as well, you can’t put the times directly in you have to use a helper ( the box’s in the UI do say Entity* but i did the same and typed in times!)

From the UI mine looks like this

YAML looks like this

alias: Bathroom Night Light
description: ''
use_blueprint:
  path: freakshock88/motion_illuminance_activated_entity.yaml
  input:
    motion_sensor: binary_sensor.bathroom_multisensor_home_security_motion_detection
    target_entity: script.bathroom_nightlight
    time_limit_after: input_datetime.bathroom_night_light_start
    time_limit_before: input_datetime.bathroom_night_light_end
    no_motion_wait: input_number.bathroom_night_time_hold_minutes
    target_off_entity: light.bathroom_strip

You just need to create a time/date helper, give it your time, save it as a recognisable name, then call that entity in the UI/YAML

EDIT - just realised ive mixed up the examples from my daytime and night time routines, but you get the idea

@freakshock thank you for this blueprint! I have a question. Why don’t you make use of is_state() and states() methods as suggested by Home Assistant instead of working with the state object directly? Home Assistant Templating State Documentation

image

Hello,
First of all this automation ROcks! Ive been using this for a while now. constant updating it to my needs!
But today ive encounter a problem that is strange… ive installed some knx lights and for some odd reason with this automation when it triggers it doesnt show me the state of the light on the dashboard. what i mean is that the light turns on, but it doesnt show up as on… and if i dont a simple automation to turn on the light with movement it shows the state. anyone know whats going on ?

Force of habit I guess. I was able to reduce the amount of times I call .state.
There is still one occurance where I need to access the domain of the target entity. Unfortunately state_attr() doet not work for domain which is not really an attribute of the entity. I think this is as good as it gets (although I never had any practical problems with the previous version of the code). If you think anything else can be improved I’m happy to hear it!

That’s strange indeed.
The blueprint uses the service homeassistant.turn_on to turn on the target entity.
Maybe this does not change the showed state for your lights?
But light.turn_on does?
Maybe you can check this in the services tab of the developer tools of HA.

Cool, please remember I’m not trying to poke holes in this blueprint, it is awesome. Just trying to help make it better if at all possible.

Maybe use “is_state()” instead of “==”, replace things like:

condition:
  # First condition: When entity was already on because the automation ran recently, do not check illuminance because it could have increased above threshold because of a light that was just turned on.
  - condition: template
    value_template: >
      {% set illuminance_defined = illuminance_currently != none and illuminance_cutoff != none  %}
      {% set illuminance_defined_and_low = (illuminance_defined and (states(illuminance_currently) | int(0) < states(illuminance_cutoff) | int(0)))  %}

      {% set target_entity_domain_supports_on_state_check = entity_domain != 'scene' and entity_domain != 'script' %}
      {{ 
      ( target_entity_domain_supports_on_state_check and states(target_entity) == 'on') or
      ( target_entity_domain_supports_on_state_check and states(target_entity) == 'off' and not illuminance_defined) or
      ( target_entity_domain_supports_on_state_check and states(target_entity) == 'off' and illuminance_defined_and_low) or
      ( not target_entity_domain_supports_on_state_check and illuminance_defined_and_low) or
      ( not target_entity_domain_supports_on_state_check and not illuminance_defined)
      }}
  - condition: template
    value_template: "{{ (blocker_entity == none) or (states(blocker_entity) == 'off') }}"

with

condition:
  # First condition: When entity was already on because the automation ran recently, do not check illuminance because it could have increased above threshold because of a light that was just turned on.
  - condition: template
    value_template: >
      {% set illuminance_defined = illuminance_currently != none and illuminance_cutoff != none  %}
      {% set illuminance_defined_and_low = (illuminance_defined and (states(illuminance_currently) | int(0) < states(illuminance_cutoff) | int(0)))  %}

      {% set target_entity_domain_supports_on_state_check = entity_domain != 'scene' and entity_domain != 'script' %}
      {{ 
      ( target_entity_domain_supports_on_state_check and is_state(target_entity, 'on') ) or
      ( target_entity_domain_supports_on_state_check and is_state(target_entity, 'off') and not illuminance_defined) or
      ( target_entity_domain_supports_on_state_check and is_state(target_entity, 'off') and illuminance_defined_and_low) or
      ( not target_entity_domain_supports_on_state_check and illuminance_defined_and_low ) or
      ( not target_entity_domain_supports_on_state_check and not illuminance_defined )
      }}
  - condition: template
    value_template: "{{ (blocker_entity == none) or is_state(blocker_entity, 'off') }}"

There is one more condition at the bottom that can also use is_state(). :slight_smile:

@freakshock Just another idea, I have a scenario where I’m using a script to turn on either one of two lights. The issue is I don’t know what light is on to turn off that light. I made the following adjustments.

Changes made:

  • target_off_entity (input)
  • added variable “target_off_entity_that_is_on”
  • first condition added → ( not target_entity_domain_supports_on_state_check and target_off_entity_that_is_on | length > 0 ) or
  • bottom choose condition: value_template: “{{ (target_off_entity != none) and (target_off_entity | length > 0) }}”
.......
    target_off_entity:
      name: (OPTIONAL) Turn-off entity
      description: If defined, this entity will be turned off instead of the default target entity. This can be helpful when using target entities of type scene or script.
      default: []
      selector:
        entity:
          domain:
            - light
            - switch
          multiple: true

mode: restart
max_exceeded: silent

variables:
  target_entity: !input target_entity
  illuminance_currently: !input illuminance_sensor
  illuminance_cutoff: !input illuminance_cutoff
  blocker_entity: !input blocker_entity
  time_limit_before: !input time_limit_before
  time_limit_after: !input time_limit_after
  no_motion_wait: !input no_motion_wait
  entity_domain: "{{ states[target_entity].domain }}"
  turn_off_blocker_entity: !input turn_off_blocker_entity
  target_off_entity: !input target_off_entity
  target_off_entity_that_is_on: >
    {{ expand(target_off_entity)
      | selectattr("state", "eq", "on") 
      | map(attribute='entity_id')
      | list }}

trigger:
  platform: state
  entity_id: !input motion_sensor
  to: "on"

condition:
  # First condition: When entity was already on because the automation ran recently,
  # do not check illuminance because it could have increased above threshold because of a light that was just turned on.
  - condition: template
    value_template: >
      {% set illuminance_defined = illuminance_currently != none and illuminance_cutoff != none  %}
      {% set illuminance_defined_and_low = (illuminance_defined and (states(illuminance_currently) | int(0) < states(illuminance_cutoff) | int(0)))  %}

      {% set target_entity_domain_supports_on_state_check = entity_domain != 'scene' and entity_domain != 'script' %}
      {{ 
      ( target_entity_domain_supports_on_state_check and is_state(target_entity, 'on') ) or
      ( target_entity_domain_supports_on_state_check and is_state(target_entity, 'off') and not illuminance_defined) or
      ( target_entity_domain_supports_on_state_check and is_state(target_entity, 'off') and illuminance_defined_and_low) or
      ( not target_entity_domain_supports_on_state_check and target_off_entity_that_is_on | length > 0 ) or
      ( not target_entity_domain_supports_on_state_check and illuminance_defined_and_low ) or
      ( not target_entity_domain_supports_on_state_check and not illuminance_defined )
      }}
  - condition: template
    value_template: "{{ (blocker_entity == none) or is_state(blocker_entity, 'off') }}"
  - condition: template
    value_template: >
      {% set current_time = now().strftime("%H:%M")  %}

      {% if time_limit_before == none and time_limit_after == none %}
      true
      {% endif %}

      {% if time_limit_before != none and time_limit_after == none %}
      {% set current_time_is_before_limit = current_time < states(time_limit_before)  %}
      {{ current_time_is_before_limit }}
      {% elif time_limit_before == none and time_limit_after != none %}
      {% set current_time_is_after_limit = current_time > states(time_limit_after)  %}
      {{ current_time_is_after_limit }}
      {% endif %}

      {% if time_limit_before != none and time_limit_after != none %}
      {% set before_limit_is_tomorrow = states(time_limit_before) < states(time_limit_after)  %}
      {% set current_time_is_before_limit = current_time < states(time_limit_before)  %}
      {% set current_time_is_after_limit = current_time > states(time_limit_after)  %}
      {% set time_window_spans_midnight = states(time_limit_after) > states(time_limit_before)  %}
        {% if time_window_spans_midnight != none and time_window_spans_midnight and before_limit_is_tomorrow %}
        {{ current_time_is_after_limit or current_time_is_before_limit }}
        {% elif time_window_spans_midnight != none and not time_window_spans_midnight %}
        {{ current_time_is_before_limit and current_time_is_after_limit }}
        {% endif %}
      {% endif %}

action:
  - service: homeassistant.turn_on
    entity_id: !input target_entity
  - condition: template
    value_template: "{{ no_motion_wait != none }}"
  - wait_for_trigger:
      platform: state
      entity_id: !input motion_sensor
      from: "on"
      to: "off"
  - delay:
      minutes: "{{ states(no_motion_wait) | int(0) }}"
  - condition: template
    value_template: "{{ (turn_off_blocker_entity == none) or is_state(turn_off_blocker_entity, 'off') }}"
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ (target_off_entity != none)  and (target_off_entity | length > 0) }}"
        sequence:
          - service: homeassistant.turn_off
            data:
              entity_id: !input target_off_entity
    default:
      - service: homeassistant.turn_off
        entity_id: !input target_entity

NOTE: If you make this change you will need to update your automation yaml from to something like

.....
target_off_entity: light.kitchen_ceiling

to

.....
target_off_entity:
    - light.kitchen_ceiling

I found the reason… im so sorry. i had modiffy your blueprint so, that all my rgbw would go back to the color white again, when the automation would kick again. and those configs were not valid for thosel lights… thank you!