Automation to react to "inactivity"

I have a motion sensor in my garage and my intent is to use it as a nighttime alarm. But I’m also in there during the day so I have constant “detection” and “cleared” events.

I could accomplish everything I want with just 2 automations: one that turns on the lights when motion is detected, and another that turns them off after a few minutes of inactivity. I’m stumped on that second one. Seems like “inactivity” and “presence detection” are still a challenge for home automation.

Any ideas for an automation, script or combination of the 2 that does something after 5 minutes with no “motion detected” from a particular sensor?

Motion turns light on (only if it’s currently off) and turns it off (only if light is currently on) if no motion has been detected for at least 5 minutes.

alias: example 2
trigger:
- platform: state
  entity_id: binary_sensor.your_motion
  to: 'on'
- platform: state
  entity_id: binary_sensor.your_motion
  to: 'off'
  for: '00:05:00'
condition: "{{ (trigger.to_state.state == 'on' and is_state('light.your_light', 'off')) or (trigger.to_state.state == 'off' and is_state('light.your_light', 'on')) }}"
action:
- service: "light.turn_{{ trigger.to_state.state }}"
  target:
    entity_id: light.your_light

There are many other automation examples on the forum, including a few Blueprints (like this one).


EDIT

Correction. Added missing single quotes.

description: lights on and off via motion sensor
trigger:
  - type: occupied
    platform: device
    device_id: 123456789
    entity_id: binary_sensor.motion_sensor_motion
    domain: binary_sensor
    id: motion detected
  - type: not_occupied
    platform: : device
    device_id: 123456789
    entity_id: binary_sensor.motion_sensor_motion
    domain: binary_sensor
    id: no motion for 5 mins
    for:
      hours: 0
      minutes: 5
      seconds: 0
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: motion detected
        sequence:
            - service: switch.turn_on
              target:
              entity_id: switch.some_lights
      - conditions:
          - condition: trigger
            id: no motion for 5 mins
        sequence:
          - service: switch.turn_off
            target:
              entity_id: switch.some_lights
mode: single 

Something like this bar the bad spacing, I am on an iPhone and can never get it right, but hopefully helpful none the less

FWIW, if you use a State Trigger (as shown in my example above) there’s no need to use a choose to determine which one of the two triggers occurred (nor a need to specify an id for each trigger). In addition, your example will (needlessly) send a turn_on command even when the light is already on.

1 Like

Thanks for the replies.

I’m held back by having no real knowledge of YAML. I do everything in the visual editor, but these examples can’t be edited that way. I guess I need to learn more about YAML.

I wrote code for years (decades) but YAML just seems weird too weird me. It’s markup; but it’s code; it’s procedural; but it’s not. And all the indenting and hyphens… yuck.

But apparently that’s where I need to go because the visual editor won’t do what I want.

The Automation Editor has a YAML mode. You can simply switch from UI to YAML mode, copy-paste the example posted above, then save the automation. Simple as that.

Click the overflow menu (three vertical dots visible in the upper right corner of the editor) and that’s where you will find the ability to switch modes.

Unless you have already read it, I recommend you review the official documentation: YAML

Hi Taras hope you are well!

I am wondering if you have uploaded (or if you can upload) your automations in guthub or somewhere else. It happens quite often to bookmark your answers here just to study the efficient way you are construct them, and already have saved several pages.

My example, although superseded by the home assistant Jedi that is @123 was created originally completely in the UI (it’s an old automation) and I simply put the UI edition in to yaml mode so I could cut and paste the example for you.

So they are all perfectly doable in the UI

Sometimes the examples I post on the forum are based on what I use in my home. However, I have not uploaded all of them anywhere (and have no plans to do that). Frankly, there’s a wider variety of automation applications on this forum than in my own home so just bookmark the ones that interest you (that’s what I do).

1 Like

Yes I tried that, but the visual editor then told me this:

"Visual editor is not supported for this configuration

  • Templates not supported in visual editor
    You can still edit your config in YAML."

I can see that YAML can do much more than what’s available in the visual editor. I’ll have to get up to speed.

It simply informed you that the editor couldn’t display the YAML you pasted in UI/visual mode, so it remained in YAML mode.

Have you tried the example I posted (after replacing the entities with your own)?

I think I have it working (note, the example is missing a couple of quote chars on string arguments to is_state()). Thanks!

I need to understand the ‘state’ construct, which seems to include a number of useful things. I know there’s a lot of power in HA which I haven’t begun to tap; as I said earlier, YAML sort of puts me off (“template”? “platform”?).

You’re welcome!

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. It will also place a link below your first post that leads to the solution post. All of this helps users find answers to similar questions. For more information, refer to guideline 21 in the FAQ.

I recommend you read the documentation for YAML (as suggested earlier) and Templating.

I can see that HA has more capabilities than I realized - for example, that State construct. And I’ve started looking into YAML. Here’s the thing - it’s a situation very familiar to me from many years in software development - your initial question is “what can I actually DO with this stuff”? Rather than starting at the bottom, with the syntax, I want the high level overview. That’s where the motivation comes from.