Motion activated entity. Optional lux sensor, time of day, blocking boolean

I got it all to work.

  1. I created Input Datetimes using the Helper UI:
  • input_datetime.sunrise
  • Input_datetime.sunset
  • input_datetime.sunset_minus_60_min
  • input_datetime.sunset_minus_90_min
  1. I then created matching Automations in the UI to trigger at the Sunset and Sunrise (and offset) times.
  • The Trigger type is Sun. From the UI you can then choose Sunrise, or Sunset, with any Offset you want.

  • The Action is Call Service Input:datetime: Set. You choose Time, but then have to edit the Action in yaml to change the time to ‘{{ now().strftime(’’%H:%M:%S’’) }}’, since there is no option to do that in the UI. It works great. Here is the yaml from one of my Automations:

alias: Set Sunset Time minus 90 min
description: ''
trigger:
  - platform: sun
    event: sunset
    offset: '-01:30:00'
condition: []
action:
  - service: input_datetime.set_datetime
    data:
      time: '{{ now().strftime(''%H:%M:%S'') }}'
    target:
      entity_id: input_datetime.sunset_minus_90_min
mode: single

After working through this, it seems to me that Sun offsets should be easier to access from the UI, but at least it works, and I learned a lot.

1 Like

Why is illuminance_cutoff range set to 0-100 in lux, when it could be even 100k lx?

I take it from Xiaomi GZCGQ01LM and it varies from 0-3000 in dark hall.

I didn’t need the range to be any more then that for my setup. So it was just easier to be able to easily manipulate the slider with a smaller range. I can expand the range if that is going to make it more generally useful.

I’ve expanded the range for the lux

Thank you! I’ll test it tomorrow.

I’ve been using this blueprint for sometime and it’s great. I want to get more granular control throughout the day but am struggling to get it to work as expected. I have defined 4 time periods (Morning, Daytime, Evening and Nighttime) and have one automation for each period. I have been using the time_limit_after / before with date.time input helpers but the automations are not triggering when they should (by my obviously flawed logic).

Here are the Living Room automations:

- id: '1'
  alias: Living Room Lights Nighttime On Motion or Lux
  description: ''
  use_blueprint:
    path: apollo1220/motion_activated_entity.yaml
    input:
      motion_sensor: input_boolean.living_room_room_occupancy
      target_entity: light.living_room_light_main
      no_motion_wait: 10
      illuminance_sensor: sensor.living_room_motion_illuminance_lux
      turn_off_blocker_entity: group.morning_or_evening
      illuminance_cutoff: 80
      time_limit_after: input_datetime.sleepy_time
      time_limit_before: input_datetime.wakey_time
- id: '2'
  alias: Living Room Lights Evening On Motion or Lux
  description: ''
  use_blueprint:
    path: apollo1220/motion_activated_entity.yaml
    input:
      motion_sensor: input_boolean.living_room_room_occupancy
      target_entity: light.living_room_lights
      no_motion_wait: 300
      illuminance_sensor: sensor.living_room_motion_illuminance_lux
      turn_off_blocker_entity: group.morning_or_evening
      illuminance_cutoff: 80
      time_limit_after: input_datetime.evening_begins
      time_limit_before: input_datetime.sleepy_time
- id: '3'
  alias: Living Room Lights Daytime On Motion or Lux
  description: ''
  use_blueprint:
    path: apollo1220/motion_activated_entity.yaml
    input:
      motion_sensor: input_boolean.living_room_room_occupancy
      target_entity: light.living_room_light_main
      no_motion_wait: 60
      illuminance_sensor: sensor.living_room_motion_illuminance_lux
      turn_off_blocker_entity: group.morning_or_evening
      illuminance_cutoff: 100
      time_limit_after: input_datetime.morning_ends
      time_limit_before: input_datetime.evening_begins
- id: '4'
  alias: Living Room Lights Morning On Motion or Lux
  description: ''
  use_blueprint:
    path: apollo1220/motion_activated_entity.yaml
    input:
      motion_sensor: input_boolean.living_room_room_occupancy
      target_entity: light.living_room_lights
      no_motion_wait: 200
      illuminance_sensor: sensor.living_room_motion_illuminance_lux
      turn_off_blocker_entity: group.morning_or_evening
      illuminance_cutoff: 80
      time_limit_after: input_datetime.wakey_time
      time_limit_before: input_datetime.morning_ends

And here are my input helper times:

input_datetime.wakey_time = 05:45
input_datetime.morning_ends = 08:30
input_datetime.evening_begins = Sunset - 45 mins
input_datetime.sleepy_time = 22:30

If anyone can give me some pointers as to where I am going wrong I’d much appreciate it!!

Thanks

I know that the one that wraps around (Living Room Lights Nighttime On Motion or Lux) won’t work because it is time based and current time being greater then sleepy_time and less then wakey_time will never happen. That is just due to how the blueprint is setup and not anything that you’re doing. The others should work. Can you get a trace of them when they should be triggered? Might help with figuring out why they aren’t working.

Thanks for the swift feedback, and for the really useful blueprint!!

I’ll have a think about how best to reconfigure the nighttime automation - presumably setting up a blocking entity to stop at sleepy_time and start again at wakey_time would be the best way around the issue? Any better suggestions most welcome :slight_smile:

Re tracing why the other automations are triggering when they shouldn’t, I am still learning the basics of automations and to be honest don’t really understand what I’m looking for… here’s a recent example where all the automations are triggering at the same time:

And a trace of one of the automations that shouldn’t have triggered:

Any advice on what to look for would be really appreciated.

Thanks again!

When you are looking at that trace in the UI, there should be a download button in the top right. If you use that download, then you can paste all the relevant information here (easier to work with then screenshots :relaxed:)

Aha - learned something new there… thanks!

Here’s a recent download of morning automation that shouldn’t have run:

{
  "trace": {
    "last_step": "action/3",
    "run_id": "1788",
    "state": "stopped",
    "script_execution": "cancelled",
    "timestamp": {
      "start": "2021-10-10T11:58:26.775245+00:00",
      "finish": "2021-10-10T12:00:15.690681+00:00"
    },
    "domain": "automation",
    "item_id": "1631092861494",
    "trigger": "state of input_boolean.living_room_room_occupancy",
    "trace": {
      "trigger/0": [
        {
          "path": "trigger/0",
          "timestamp": "2021-10-10T11:58:26.775492+00:00",
          "changed_variables": {
            "target_entity": "light.living_room_lights",
            "illuminance_currently": "sensor.living_room_motion_illuminance_lux",
            "illuminance_cutoff": 80,
            "blocker_entity": "input_boolean.lights_manual_mode",
            "time_limit_before": "input_datetime.morning_ends",
            "time_limit_after": "input_datetime.wakey_time",
            "no_motion_wait": 300,
            "turn_off_blocker_entity": "group.morning_or_evening",
            "target_off_entity": null,
            "target_off_action": null,
            "this": {
              "entity_id": "automation.living_room_lights_on_motion_or_lux",
              "state": "on",
              "attributes": {
                "last_triggered": "2021-10-10T11:55:38.324610+00:00",
                "mode": "restart",
                "current": 1,
                "id": "1631092861494",
                "friendly_name": "Living Room Lights Morning On Motion or Lux"
              },
              "last_changed": "2021-10-10T06:36:21.811953+00:00",
              "last_updated": "2021-10-10T11:55:38.324946+00:00",
              "context": {
                "id": "4bbb92b948200fbd2ef71e6f1590f2ec",
                "parent_id": "9997918a2ff7527e2730b68aef245ebc",
                "user_id": null
              }
            },
            "trigger": {
              "id": "0",
              "idx": "0",
              "platform": "state",
              "entity_id": "input_boolean.living_room_room_occupancy",
              "from_state": {
                "entity_id": "input_boolean.living_room_room_occupancy",
                "state": "off",
                "attributes": {
                  "editable": true,
                  "friendly_name": "Living Room Room Occupancy",
                  "icon": "hass:walk"
                },
                "last_changed": "2021-10-10T11:58:18.093502+00:00",
                "last_updated": "2021-10-10T11:58:18.093502+00:00",
                "context": {
                  "id": "1138bf6ac1804fcab13b6618d96c674b",
                  "parent_id": "f5a9412f694ad37c209cd4b54ad90ec8",
                  "user_id": null
                }
              },
              "to_state": {
                "entity_id": "input_boolean.living_room_room_occupancy",
                "state": "on",
                "attributes": {
                  "editable": true,
                  "friendly_name": "Living Room Room Occupancy",
                  "icon": "hass:walk"
                },
                "last_changed": "2021-10-10T11:58:26.773383+00:00",
                "last_updated": "2021-10-10T11:58:26.773383+00:00",
                "context": {
                  "id": "307c2605b275c38e10398b8b7377f825",
                  "parent_id": "c46bdb3111b04c76cea8bb5770419f2b",
                  "user_id": null
                }
              },
              "for": null,
              "attribute": null,
              "description": "state of input_boolean.living_room_room_occupancy"
            }
          }
        }
      ],
      "condition/0": [
        {
          "path": "condition/0",
          "timestamp": "2021-10-10T11:58:26.775589+00:00",
          "result": {
            "result": true,
            "entities": [
              "light.living_room_lights"
            ]
          }
        }
      ],
      "condition/1": [
        {
          "path": "condition/1",
          "timestamp": "2021-10-10T11:58:26.776133+00:00",
          "result": {
            "result": true,
            "entities": [
              "input_boolean.lights_manual_mode"
            ]
          }
        }
      ],
      "condition/2": [
        {
          "path": "condition/2",
          "timestamp": "2021-10-10T11:58:26.776393+00:00",
          "result": {
            "result": true,
            "entities": [
              "input_datetime.morning_ends",
              "input_datetime.wakey_time"
            ]
          }
        }
      ],
      "action/0": [
        {
          "path": "action/0",
          "timestamp": "2021-10-10T11:58:26.790004+00:00",
          "changed_variables": {
            "context": {
              "id": "5b5537bf347e2b7c77b70e17509d785f",
              "parent_id": "307c2605b275c38e10398b8b7377f825",
              "user_id": null
            }
          },
          "result": {
            "params": {
              "domain": "homeassistant",
              "service": "turn_on",
              "service_data": {},
              "target": {
                "entity_id": [
                  "light.living_room_lights"
                ]
              }
            },
            "running_script": false,
            "limit": 10
          }
        }
      ],
      "action/1": [
        {
          "path": "action/1",
          "timestamp": "2021-10-10T11:58:26.834071+00:00",
          "result": {
            "result": true,
            "entities": []
          }
        }
      ],
      "action/2": [
        {
          "path": "action/2",
          "timestamp": "2021-10-10T11:58:26.834751+00:00",
          "result": {
            "wait": {
              "remaining": null,
              "trigger": {
                "id": "0",
                "idx": "0",
                "platform": "state",
                "entity_id": "input_boolean.living_room_room_occupancy",
                "from_state": {
                  "entity_id": "input_boolean.living_room_room_occupancy",
                  "state": "on",
                  "attributes": {
                    "editable": true,
                    "friendly_name": "Living Room Room Occupancy",
                    "icon": "hass:walk"
                  },
                  "last_changed": "2021-10-10T11:58:26.773383+00:00",
                  "last_updated": "2021-10-10T11:58:26.773383+00:00",
                  "context": {
                    "id": "307c2605b275c38e10398b8b7377f825",
                    "parent_id": "c46bdb3111b04c76cea8bb5770419f2b",
                    "user_id": null
                  }
                },
                "to_state": {
                  "entity_id": "input_boolean.living_room_room_occupancy",
                  "state": "off",
                  "attributes": {
                    "editable": true,
                    "friendly_name": "Living Room Room Occupancy",
                    "icon": "hass:walk"
                  },
                  "last_changed": "2021-10-10T11:59:56.770900+00:00",
                  "last_updated": "2021-10-10T11:59:56.770900+00:00",
                  "context": {
                    "id": "2c6c6bd04eb6c0c1a034ff7eeb873753",
                    "parent_id": "440ab2df53955575e28d7ca73e8d9fe9",
                    "user_id": null
                  }
                },
                "for": null,
                "attribute": null,
                "description": "state of input_boolean.living_room_room_occupancy"
              }
            }
          }
        }
      ],
      "action/3": [
        {
          "path": "action/3",
          "timestamp": "2021-10-10T11:59:56.778242+00:00",
          "changed_variables": {
            "wait": {
              "remaining": null,
              "trigger": {
                "id": "0",
                "idx": "0",
                "platform": "state",
                "entity_id": "input_boolean.living_room_room_occupancy",
                "from_state": {
                  "entity_id": "input_boolean.living_room_room_occupancy",
                  "state": "on",
                  "attributes": {
                    "editable": true,
                    "friendly_name": "Living Room Room Occupancy",
                    "icon": "hass:walk"
                  },
                  "last_changed": "2021-10-10T11:58:26.773383+00:00",
                  "last_updated": "2021-10-10T11:58:26.773383+00:00",
                  "context": {
                    "id": "307c2605b275c38e10398b8b7377f825",
                    "parent_id": "c46bdb3111b04c76cea8bb5770419f2b",
                    "user_id": null
                  }
                },
                "to_state": {
                  "entity_id": "input_boolean.living_room_room_occupancy",
                  "state": "off",
                  "attributes": {
                    "editable": true,
                    "friendly_name": "Living Room Room Occupancy",
                    "icon": "hass:walk"
                  },
                  "last_changed": "2021-10-10T11:59:56.770900+00:00",
                  "last_updated": "2021-10-10T11:59:56.770900+00:00",
                  "context": {
                    "id": "2c6c6bd04eb6c0c1a034ff7eeb873753",
                    "parent_id": "440ab2df53955575e28d7ca73e8d9fe9",
                    "user_id": null
                  }
                },
                "for": null,
                "attribute": null,
                "description": "state of input_boolean.living_room_room_occupancy"
              }
            }
          },
          "result": {
            "delay": 300,
            "done": false
          }
        }
      ]
    },
    "config": {
      "mode": "restart",
      "max_exceeded": "silent",
      "variables": {
        "target_entity": "light.living_room_lights",
        "illuminance_currently": "sensor.living_room_motion_illuminance_lux",
        "illuminance_cutoff": 80,
        "blocker_entity": "input_boolean.lights_manual_mode",
        "time_limit_before": "input_datetime.morning_ends",
        "time_limit_after": "input_datetime.wakey_time",
        "no_motion_wait": 300,
        "turn_off_blocker_entity": "group.morning_or_evening",
        "target_off_entity": null,
        "target_off_action": null
      },
      "trigger": {
        "platform": "state",
        "entity_id": "input_boolean.living_room_room_occupancy",
        "to": "on"
      },
      "condition": [
        {
          "condition": "template",
          "value_template": "{{ (states(target_entity) == 'on') or (illuminance_currently == none) or (illuminance_cutoff == none) or (states[illuminance_currently].state | int < illuminance_cutoff | int) }}"
        },
        {
          "condition": "template",
          "value_template": "{{ (blocker_entity == none) or (states[blocker_entity].state == 'off') }}"
        },
        {
          "condition": "template",
          "value_template": "{% set current_time = now().strftime(\"%H:%M\")  %}\n{% if time_limit_before != none and time_limit_after == none %} {{ states[time_limit_before].state > current_time }} {% elif time_limit_before == none and time_limit_after != none %} {{ states[time_limit_after].state < current_time }} {% elif time_limit_before != none and time_limit_after != none %} {% set before_limit_is_on_next_day = time_limit_after > time_limit_before  %} {% if not before_limit_is_on_next_day %} {{ (states[time_limit_after].state < current_time) and (states[time_limit_before].state > current_time) }} {% elif before_limit_is_on_next_day %} {{ (states[time_limit_before].state > current_time) or (states[time_limit_after].state < current_time) }} {% endif %} {% else %} true {% endif %}\n"
        }
      ],
      "action": [
        {
          "service": "homeassistant.turn_on",
          "entity_id": "light.living_room_lights"
        },
        {
          "condition": "template",
          "value_template": "{{ no_motion_wait != none }}"
        },
        {
          "wait_for_trigger": {
            "platform": "state",
            "entity_id": "input_boolean.living_room_room_occupancy",
            "from": "on",
            "to": "off"
          }
        },
        {
          "delay": {
            "seconds": "{{ no_motion_wait | int }}"
          }
        },
        {
          "condition": "template",
          "value_template": "{{ (turn_off_blocker_entity == none) or (states[turn_off_blocker_entity].state == 'off') }}"
        },
        {
          "choose": [
            {
              "conditions": [
                {
                  "condition": "template",
                  "value_template": "{{ (target_off_entity != none) }}"
                }
              ],
              "sequence": [
                {
                  "service": "homeassistant.turn_off",
                  "entity_id": null
                }
              ]
            },
            {
              "conditions": [
                {
                  "condition": "template",
                  "value_template": "{{ (target_off_action != none) }}"
                }
              ],
              "sequence": null
            }
          ],
          "default": [
            {
              "service": "homeassistant.turn_off",
              "entity_id": "light.living_room_lights"
            }
          ]
        }
      ],
      "id": "1631092861494",
      "alias": "Living Room Lights Morning On Motion or Lux",
      "description": ""
    },
    "blueprint_inputs": {
      "id": "1631092861494",
      "alias": "Living Room Lights Morning On Motion or Lux",
      "description": "",
      "use_blueprint": {
        "path": "apollo1220/motion_activated_entity.yaml",
        "input": {
          "motion_sensor": "input_boolean.living_room_room_occupancy",
          "target_entity": "light.living_room_lights",
          "no_motion_wait": 300,
          "illuminance_sensor": "sensor.living_room_motion_illuminance_lux",
          "turn_off_blocker_entity": "group.morning_or_evening",
          "illuminance_cutoff": 80,
          "time_limit_after": "input_datetime.wakey_time",
          "time_limit_before": "input_datetime.morning_ends",
          "blocker_entity": "input_boolean.lights_manual_mode"
        }
      }
    },
    "context": {
      "id": "5b5537bf347e2b7c77b70e17509d785f",
      "parent_id": "307c2605b275c38e10398b8b7377f825",
      "user_id": null
    }
  },
  "logbookEntries": [
    {
      "name": "Living Room Lights Morning On Motion or Lux",
      "message": "has been triggered by state of input_boolean.living_room_room_occupancy",
      "source": "state of input_boolean.living_room_room_occupancy",
      "entity_id": "automation.living_room_lights_on_motion_or_lux",
      "context_id": "5b5537bf347e2b7c77b70e17509d785f",
      "when": "2021-10-10T11:58:26.787775+00:00",
      "domain": "automation"
    }
  ]
}

Grateful if you can point me in the right direction with fault finding. Also I don’t see any personally identifiable information but if there is please let me know and I’ll strip out asap!!

Thanks again

Thanks, found an issue in the blueprint. I’ve fixed the issue, so if you get the latest version it should work for you (even the one which wraps around midnight).

Ok that’s really helpful, thanks very much. I’ve taken the latest commit, will test overnight and feedback tomorrow.

Update - everything now works as expected and all automations fire within the helper thresholds.

Thanks @apollo1220 for all your help.

1 Like

Having lived with the time bound automations I set up per above (morning, daytime, evening and nighttime) for a couple of weeks I can report they work very well. However I have a couple of gremlins I’m struggling to work though and was hoping for some expert advice.

I have found that when transitioning between automations, if a light has been triggered on but is not included in the next automation it remains on indefinitely. I guess I could build an automation to turn off all unwanted lights (that are on) at the end of the automation period but was wondering if there is a better / simpler way to achieve this?

I also have some seemingly random lights turning off at the end of an automation period (but while the room is still occupied) and I can’t figure out why. This one doesn’t seem to happen consistently so I haven’t been able to fault find.

Any ideas gratefully received.

The mode being ‘restart’ is causing that. That is necessary for the light to stay on and still turn off at the right time when motion is off and turns on again before the delay for turning off expires. Not sure how I could make modifications to the blueprint for your case. I’ll keep thinking about it, but I haven’t come up with a full proof solution to it.

Well thanks for considering it in any case - I appreciate I may be an edge case… In the meantime I’ll add automations to exclude the lights that shouldn’t be on when transitioning time periods.

Thanks again

Hi @apollo1220… Just wondering if you had come up with a possible solution to the mode restart issue I have. Or alternatively is there anything I can do myself to prevent lights switching off after transitions? There are a couple of particularly “problematic” time periods between evening and sleepy time that it would be great to smooth out for a more harmonious household!! Otherwise I may have to rethink things.

I do appreciate I’m not using your blueprint as intended so don’t worry if it will cause too much work.

Thanks in advance

Would you be up to replacing your local version with a new version to test with? I haven’t had time to setup a test case, but I have a possible fix if you want to try it.

blueprint:
  name: Turn on based on motion
  description: "Turn on a light, switch, scene, script or group based on motion detection,\
    \ and low light level.\n\
    \nRequired entities:\n  - Motion sensor (single sensor or group)\n  - Target entity\
    \ (light, switch, scene or script)\n\n\nOptional features:\n- You can set a cutoff\
    \ entity of which the value determines whether the illuminance level is low and\
    \ the automation needs to trigger. \n- You can define a blocking entity, which blocks\
    \ the automation from running when this entity's state is on. \n- You van define\
    \ a turn-off blocking entity, which blocks the entity from turning off after the\
    \ set delay. \n- Time limits can also be defined to limit the time before and after\
    \ the automation should trigger. \n- If you want the entity to turn off after a\
    \ certain amount of seconds, you can use the Wait Time input. \n- If you want another\
    \ entity than the target_entity to turn off after the delay, you can define a\
    \ separate Turn-off entity. \n- If you do not enable the optional entities the automation\
    \ will skip these conditions."
  domain: automation
  input:
    motion_sensor:
      name: Motion Sensor
      description: This sensor will trigger the turning on of the target entity.
      selector:
        entity:
          domain: binary_sensor
    target_entity:
      name: Target entity.
      description: The light, switch, scene to turn on (or script to run) when the
        automation is triggered.
      selector:
        entity: {}
    no_motion_wait:
      name: Turn off wait time (seconds)
      description: Time in seconds to leave the target entity on after last motion
        is detected.
      default: 0
      selector:
        number:
          min: 0
          max: 300
          unit_of_measurement: seconds
    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:
      selector:
        number:
          min: 0
          max: 100000
          unit_of_measurement: lx
    blocker_entity:
      name: (OPTIONAL) Blocking entity
      description: If this entity's state is on, it will prevent the automation from
        running. E.g. sleepmode or away mode.
      default:
      selector:
        entity: {}
    time_limit_after:
      name: (OPTIONAL) Only run after time.
      description: Automation will only run when time is later than this input_datetime
        value.
      default:
      selector:
        entity:
          domain: input_datetime
    time_limit_before:
      name: (OPTIONAL) Only run before time.
      description: Automation will only run when time is earlier than this input_datetime
        value.
      default:
      selector:
        entity:
          domain: input_datetime
    turn_off_blocker_entity:
      name: (OPTIONAL) Turn-off Blocking entity
      description: If this entity's state is on, it will prevent the target entity
        from turning off after the set delay.
      default:
      selector:
        entity: {}
    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. 
        You cannot use Turn-off entity and Turn-off action, only one or the other. If both are defined, Turn-off entity will be used.
      default:
      selector:
        entity: {}
    target_off_action:
      name: (OPTIONAL) Turn-off action
      description: If defined, this action will be run instead of the turning off the default target entity. This can be helpful when using taget entities of type scene or script. 
        You cannot use Turn-off entity and Turn-off action, only one or the other. If both are defined, Turn-off entity will be used.
      default:
      selector:
        action: {}
  source_url: https://github.com/apollo1220/blueprints/blob/d6539f30ad194d440f15212263b0b2d6077beb17/motion_activated_entity.yaml
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'
  turn_off_blocker_entity: !input 'turn_off_blocker_entity'
  target_off_entity: !input 'target_off_entity'
  target_off_action: !input 'target_off_action'
trigger:
  platform: state
  entity_id: !input 'motion_sensor'
  to: 'on'
condition:
- condition: template
  value_template: '{{ (states(target_entity) == ''on'') or (illuminance_currently
    == none) or (illuminance_cutoff == none) or (states[illuminance_currently].state
    | int < illuminance_cutoff | int) }}'
- condition: template
  value_template: '{{ (blocker_entity == none) or (states[blocker_entity].state ==
    ''off'') }}'
- condition: template
  value_template: '{% set current_time = now().strftime("%H:%M")  %}

    {% if states(target_entity) == ''on'' %} true {% endif %} {% elif time_limit_before != none and time_limit_after == none %} {{ states[time_limit_before].state
    > current_time }} {% elif time_limit_before == none and time_limit_after != none
    %} {{ states[time_limit_after].state < current_time }} {% elif time_limit_before
    != none and time_limit_after != none %} {% set before_limit_is_on_next_day = time_limit_after
    > time_limit_before  %} {% if not before_limit_is_on_next_day %} {{ (states[time_limit_after].state
    < current_time) and (states[time_limit_before].state > current_time) }} {% elif
    before_limit_is_on_next_day %} {{ (states[time_limit_before].state > current_time)
    or (states[time_limit_after].state < current_time) }} {% endif %} {% else %} true
    {% 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:
    seconds: '{{ no_motion_wait | int }}'
- condition: template
  value_template: '{{ (turn_off_blocker_entity == none) or (states[turn_off_blocker_entity].state
    == ''off'') }}'
- choose:
  - conditions:
    - condition: template
      value_template: '{{ (target_off_entity != none) }}'
    sequence:
    - service: homeassistant.turn_off
      entity_id: !input 'target_off_entity'
  - conditions:
    - condition: template
      value_template: '{{ (target_off_action != none) }}'
    sequence: !input 'target_off_action'
  default:
  - service: homeassistant.turn_off
    entity_id: !input 'target_entity'

Thanks very much. I have a consistent example that I can test tomorrow and I’ll then confirm results. Was going to compile the trace results this evening but you beat me to it (and can still do so if the test fails).

I’ve just replaced the blueprint code but now all related automations are greyed out and marked as inactive, as follows:

I’ve reverted back for the time being…

Thanks

Hello @apollo1220

I used this Blue prints and it’s work well thanks.

I have a question about the first condition :

'{{ (states(target_entity) == ''on'') or (illuminance_currently
    == none) or (illuminance_cutoff == none) or (states[illuminance_currently].state
    | int < illuminance_cutoff | int) }}'

I don’t understand why you added the following check

yaml (states(target_entity) == ''on'')

Could you kindly explain me ?

Just a personal thought, instinctively I would have done the contrary : added a condition before the first one to check that the target_entity is not ‘on’ (we can assume in that case that somebody manually turn on the light and the automation is not needed) but I may have a different use case than your.

'{{ (states(target_entity) == ''off'') }}'

Thanks for your answer and the very good job :+1:.