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

Yeah if you don’t need an offset, you can just use sun above horizon as the blocking entity. Then it will only run from sunset to sunrise.

I just tested the new turn-ff action and it is perfect. I am using your blueprint for multiple things, including for security lights that only turn on from a motion sensor after dark, like Dixey. I tried to use the blocking entity, but the only choice I see in the drop-down list is “sun.sun”. I did not see a way to create a “sun above the horizon” Blocking Entity using the Helper. It looks like limiting between Sunset and Sunrise (with offset) could be created by using an “Action Type, Condition”, like in a script. Is this something that has to be done using yaml using your Blueprint?

You can setup a binary sensor which converts above_horizon or below_horizon to make it easier to consume in the blueprint. I haven’t tested this out, so the syntax might be a little bit off, but the configuration would look something like this.

binary_sensor:
  - platform: template
    sensors:
      night_time:
        value_template: "{{ is_state('sun.sun', 'below_horizon') }}"

So it looks like from the Template Binary Sensor doc page that I would just add this to my configuration.yaml using a file editor? I haven’t directly edited any files yet, but have browsed the files and seen how the UI writes out the blueprints, scenes and automations, etc.

Yes. and it does work without any errors so just copy and paste it as is in your configuration.yaml.

I got it all up and working (and edited my first yaml file successfully). I modified the binary sensor slightly to have both a night_time and a day_time sensor. For my outdoor lights, I want the motion sensor to turn on the lights between 5 pm and 7 am, but only when it is dark (the sun below the horizon). When the blocking entity’s state is on, it prevents the automation from running. So the blocking entity needs to be day_time. (I use night_time so I am able to test the automation during the day.) My configuration looks like this:

binary_sensor:
  - platform: template
    sensors:
      night_time:
        friendly_name: "Night Time"
        value_template: "{{ is_state('sun.sun', 'below_horizon') }}"

      day_time:
        friendly_name: "Day Time"
        value_template: "{{ is_state('sun.sun', 'above_horizon') }}"

This blueprint is so flexible that I am able to configure exactly what I want my lights to look like with amazing precision. I have a script that activates a scene, so I can set both the brightness and the color of the lights I want to turn on, and have a transition time so they can turn on smoothly. The new turn-off action then allows me to turn all the lights off after the time-out, again with a nice smooth transition.

The only other wish list item would be to have a way to set the run before time, and the run after time, as an offset to sunset and sunrise. I have some rooms inside my house that get darker than others before sunset. So, for example, I would like to be able to have my family room lights turn on an hour before sunset. You mentioned that you could setup a set_datetime entity with sunset and sunrise, but that option is not in the Helper UI. So it looks like you would have to create a yaml configuraiton to do this. I found an example that looks like it might work in Getting sunset (with offset) as a time value

alias: update sunset minus one
trigger:
  platform: sun
  event: sunset
  offset: "-01:00:00"
action:
  service: input_datetime.set_value
  data_template:
    entity_id: input_number.sunset_minus_one_hour
    value: "{{ now().strftime('%H:%M:00') }}"

Would this approach work? Would I add this to configuration.yaml like the binary sensor?

Yes that approach can work. That is an automation which you should be able to setup in the UI. But the input_datetime would need to be setup in your configuration.yaml similar to the binary sensor. Like

input_datetime:
  sunset_offset:

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.