Help with Motion Detection automation

Hi Everyone,

Second post of the day from a new user who is struggling to build automations, I am coming over from Hubitat where I have been using webcore, I am trying to replicate this webcore script\piston.

I am using the UI to build the automations out but really struggling to find some features that leads me to believe I may need to edit some Yaml but not sure if I am just missing soemthing.

Things I cant find which is causing me some problems are:

  • Unable to find where to set colour temp
  • Unable to find the time setting to turn the light off when no motion has been detected for 60 seconds

Another issue I had was when I set 30 minutes to sunset and 30 minute past sunrise the automation stopped working, so I removed this part but would like to know how to achieve that too.

Here is the Yaml:

- id: '1637523993802'
  alias: Rear Garden Motion Detection
  description: ''
  trigger:
  - type: motion
    platform: device
    device_id: f55ee4aa6c897bd26753bed686c2059b
    entity_id: binary_sensor.hue_outdoor_motion_sensor_1_motion_2
    domain: binary_sensor
  condition:
  - condition: state
    entity_id: sensor.hub_mode
    state: Home
  - type: is_illuminance
    condition: device
    device_id: f55ee4aa6c897bd26753bed686c2059b
    entity_id: sensor.hue_outdoor_motion_sensor_1_light_level_2
    domain: sensor
    below: 20
  action:
  - type: turn_on
    device_id: 355588737af8c3aa202dc270655e88cb
    entity_id: light.rear_security_light
    domain: light
    brightness_pct: 75
  mode: restart

I have had a search around and read a lot of the documentation but cannot see what I am looking for.

I did find this piece of code:

from: 'on'
    to: 'off'
    for:
      minutes: 2

But not sure where it should be placed (I am nearly 2 weeks into home assistant) and been reading lots but really struggling with building automation and editing Yaml (Happy to be pointed in the direction of where I can read more about yaml and what values you can place for what devices\sensors.

1 Like

You messed up the code formatting in your post. Fix that and it will make it much easier for people to help you. :slight_smile:

You shouldn’t need to use YAML to create any of that functionality (but you can always switch back to YAML in the gui by clicking the three dots in the upper right of the automation editor).

Here’s a screenshot of me adding a light.turn_on call to an automation. In the GUI the color etc. are all available for options. The documentation for the light integration also includes this information and some examples.

1 Like

Thanks for the screenshot, I dont really want to edit the yaml to be honest just clutching at straws as I have been trying to achieve this for the last couple of hours without luck :frowning:

I fixed my post too, I got the message to fix the code but couldnt figure out what ``` these characters were but copied and pasted them this time which worked :slight_smile:

It feels like I am missing something really simple here but cannot see it when I look although looking at your screenshot and reading the link tells me I am likely choosing the wrong option I guess, this is what I have when choosing the action for my trigger.

But looking at your screenshot you have call service as an action so let me try that to see if I get the fields I require, also thanks for the prompt reply!

That may be a limitation of device actions/automations. Devices are very nifty for making some automations more straightforward but I think can be limiting in some ways. Use the “call service” action type instead.

1 Like

Thanks, I think its the fact I was using device for both the trigger and action.

Have changed the trigger from device to state which allows me to select an entity and the action from device to call service which allows the colour temp ect to be set, just need to run some test to ensure it is working.

I thought I had the solution but turns out the solution was only for the colour temperature, I changed the trigger to state which seemed works, I then added the time values and it prevents it from working. code is below:

- id: '1637523993802'
  alias: Rear Garden Motion Detection
  description: ''
  trigger:
  - platform: state
    entity_id: binary_sensor.hue_outdoor_motion_sensor_1_motion
    from: 'off'
    to: 'on'
    for:
      hours: 0
      minutes: 2
      seconds: 0
      milliseconds: 0
  condition:
  - condition: state
    entity_id: sensor.hub_mode
    state: Home
  - type: is_illuminance
    condition: device
    device_id: f55ee4aa6c897bd26753bed686c2059b
    entity_id: sensor.hue_outdoor_motion_sensor_1_light_level_2
    domain: sensor
    below: 20
  action:
  - service: light.turn_on
    target:
      device_id: 355588737af8c3aa202dc270655e88cb
    data:
      kelvin: 2700
  mode: restart

I assume above is expecting motion to be detected for 2 minutes before the light will switch on so removed it to look like below:

- id: '1637523993802'
  alias: Rear Garden Motion Detection
  description: ''
  trigger:
  - platform: state
    entity_id: binary_sensor.hue_outdoor_motion_sensor_1_motion
    for:
      hours: 0
      minutes: 0
      seconds: 0
      milliseconds: 0
  condition:
  - condition: state
    entity_id: sensor.hub_mode
    state: Home
  - type: is_illuminance
    condition: device
    device_id: f55ee4aa6c897bd26753bed686c2059b
    entity_id: sensor.hue_outdoor_motion_sensor_1_light_level_2
    domain: sensor
    below: 20
  action:
  - service: light.turn_on
    target:
      device_id: 355588737af8c3aa202dc270655e88cb
    data:
      kelvin: 2700
  mode: restart

Motion is detected and the light comes on fine but I am missing the part where it turns off after 2 minutes if motion is no longer detected.

I could set up a second automation for this part I guess but feels like it should all be part of the same script to me like it was in WebCore, although in WebCore it is 2 separate branches of code with 2 separate if statements.

I’m wondering how other people have achieved this in HA as I guess this would be quite a common automation that people are running

So I managed to get this working just now but I am not sure if it is the most efficient way to work with HA automation.

Essentially what I have had to do here is to create 2 automation’s as per below:

One automation will turn the light on, the second will turn the light off after no motion has been detected, here is the Yaml code just in case anyone is interested although I built the automation using the UI.

- id: '1637523993802'
  alias: Turn On Rear Garden Security Light when Motion is Detected (Home)
  description: ''
  trigger:
  - platform: state
    entity_id: binary_sensor.hue_outdoor_motion_sensor_1_motion
    for:
      hours: 0
      minutes: 0
      seconds: 0
      milliseconds: 0
  condition:
  - condition: state
    entity_id: sensor.hub_mode
    state: Home
  - type: is_illuminance
    condition: device
    device_id: f55ee4aa6c897bd26753bed686c2059b
    entity_id: sensor.hue_outdoor_motion_sensor_1_light_level_2
    domain: sensor
    below: 20
  action:
  - service: light.turn_on
    target:
      device_id: 355588737af8c3aa202dc270655e88cb
    data:
      kelvin: 2700
      brightness_pct: 75
  mode: single
- id: '1637533473166'
  alias: Turn Off Rear Garden Security Light when no Motion is Detected
  description: ''
  trigger:
  - type: no_motion
    platform: device
    device_id: 154fde006f6a7da72fc58ea8f3538826
    entity_id: binary_sensor.hue_outdoor_motion_sensor_1_motion
    domain: binary_sensor
    for:
      hours: 0
      minutes: 1
      seconds: 15
      milliseconds: 0
  condition: []
  action:
  - type: turn_off
    device_id: 355588737af8c3aa202dc270655e88cb
    entity_id: light.rear_security_light
    domain: light
  mode: restart

Not sure if this is the most efficient or elegant way but it does seem to work the way I expect it to now.

Happy to accept any pointers if there is a way to make do it all in one single automation.

You don’t need the for at all in your trigger since you aren’t using it.
There are a few ways to get the light to turn back off after 2 minutes.

The easiest, is to simply add a delay after the light is turned on (I think it is called “wait for time to pass” now in the list of things you can select for an action). And set it to 2 minutes. The automation will then pause at that point for 2 minutes. Then after the delay you add a call service to turn the light off again.

If you change the mode of the automation from “single” to “restart”, then every time the automation is triggered by motion being detected, it will stop the previous copy of the automation from running - cancelling the 2 minute delay, and start a new 2 minute delay. That way the light will only turn off after 2 minutes of no motion being detected.

- id: '1637523993802'
  alias: Turn On Rear Garden Security Light when Motion is Detected (Home)
  description: ''
  trigger:
  - platform: state
    entity_id: binary_sensor.hue_outdoor_motion_sensor_1_motion
  condition:
  - condition: state
    entity_id: sensor.hub_mode
    state: Home
  - type: is_illuminance
    condition: device
    device_id: f55ee4aa6c897bd26753bed686c2059b
    entity_id: sensor.hue_outdoor_motion_sensor_1_light_level_2
    domain: sensor
    below: 20
  action:
  - service: light.turn_on
    target:
      device_id: 355588737af8c3aa202dc270655e88cb
    data:
      kelvin: 2700
      brightness_pct: 75
  - delay: "00:02:00"
  - service: light.turn_off
    target:
      device_id: 355588737af8c3aa202dc270655e88cb
  mode: restart

Something like that.

1 Like

Thanks, have given it a try but it does not seem to work.

The code is as follows:

- id: '1637523993802'
  alias: Turn On Rear Garden Security Light when Motion is Detected (Home)
  description: ''
  trigger:
  - platform: state
    entity_id: binary_sensor.hue_outdoor_motion_sensor_1_motion
    for:
      hours: 0
      minutes: 0
      seconds: 0
      milliseconds: 0
  condition:
  - condition: state
    entity_id: sensor.hub_mode
    state: Home
  - type: is_illuminance
    condition: device
    device_id: f55ee4aa6c897bd26753bed686c2059b
    entity_id: sensor.hue_outdoor_motion_sensor_1_light_level_2
    domain: sensor
    below: 20
  action:
  - service: light.turn_on
    target:
      device_id: 355588737af8c3aa202dc270655e88cb
    data:
      kelvin: 2700
      brightness_pct: 75
      delay: 00:00:15
      service: light.turn_off
      target:
  mode: restart

The trace log is showing that the delay is not allowed

This results in the light no longer turning on

Urgh, some parts of home assistant let you put the time in, in that format, and some parts don’t.
In which case use the other format:

  - delay:
      minutes: 2

Edit: Oh wait no I see that you have put it in the wrong place!
It’s an action, it is not part of the light turn on sequence.

You have

  - service: light.turn_on
    target:
      device_id: 355588737af8c3aa202dc270655e88cb
    data:
      kelvin: 2700
      brightness_pct: 75
      delay: 00:00:15
      service: light.turn_off
      target:
  mode: restart

and it should be

  - service: light.turn_on
    target:
      device_id: 355588737af8c3aa202dc270655e88cb
    data:
      kelvin: 2700
      brightness_pct: 75
  - delay: 00:00:15
  - service: light.turn_off
    target:
      device_id: 355588737af8c3aa202dc270655e88cb
  mode: restart

Thank you to the both of you, I got it working by using the wait for delay from your previous post.

To achieve this I have added 3 actions:

1.) Call Service light.turn on
2.) Wait for time to pass
3.)Call Service light.turn off

It now works and here is how the current YAML code looks:

- id: '1637523993802'
  alias: Turn On Rear Garden Security Light when Motion is Detected (Home)
  description: ''
  trigger:
  - platform: state
    entity_id: binary_sensor.hue_outdoor_motion_sensor_1_motion
    for:
      hours: 0
      minutes: 0
      seconds: 0
      milliseconds: 0
  condition:
  - condition: state
    entity_id: sensor.hub_mode
    state: Home
  - type: is_illuminance
    condition: device
    device_id: f55ee4aa6c897bd26753bed686c2059b
    entity_id: sensor.hue_outdoor_motion_sensor_1_light_level_2
    domain: sensor
    below: 20
  action:
  - service: light.turn_on
    target:
      device_id: 355588737af8c3aa202dc270655e88cb
    data:
      brightness_pct: 75
      kelvin: 2700
  - delay:
      hours: 0
      minutes: 0
      seconds: 15
      milliseconds: 0
  - service: light.turn_off
    target:
      device_id: 355588737af8c3aa202dc270655e88cb
  mode: restart

@mobile.andrew.jones & @Dolores thank you both so much for your assistance here but sadly it is not letting me mark solution for the both of you.

Yep, you are right and that would probably have worked if I changed it I guess but stuck to the UI and it sorted it out for me, it was finding the way for delay which you pointed me to so thanks again, just need to build more confident using the UI and will keep reviewing the code that it creates and I am sure I will start to learn a bit of YAML by doing this.

I do the same all the time, I am mostly in the UI. But I switch back to YAML using the 3 dots at the top right of the editor, just to get an idea of what is going on behind the scenes.

Additionally if I need to move something further up, the up and down arrow buttons in the GUI are fine for that, but if I have but in a choose or a repeat loop or something, I can’t move stuff INTO that part of the GUI, I have to start adding actions and conditions for that part all over again. So I dip in to the YAML editor, make my changes in there. SAVE IT, then swap back to the GUI - if the GUI loads properly, then I know my indentation is correct and I haven’t broken anything…

2 Likes

Thanks for the advice here its sounds like a good process to follow.

1 Like

You may also want to check on the developer tools> states page for your “sensor.home_hub” state… most presence devices use “home” and “‘not_home” … so your “Home” condition will not work unless that is a self made template sensor or something.

You’re welcome! I don’t worry too much about the “credit,” it’s mostly so that if someone else has the same issue they can jump to the solution, and so that people who just want to help know to move on to another topic. :slight_smile:

At the risk of complicating things further, you could use a wait_for_trigger before the delay, checking the state of the motion sensor, so that the timer doesn’t start until the motion sensor stops detecting motion.

Also, you may want to add a to: on (or whatever the appropriate state is as indicated by the state history of the motion sensor in developer tools) because as written, that automation will trigger both when the motion sensor starts detecting motion, and when it stops detecting motion. If you trigger just on the state of an entity, it triggers on all state changes.

It’s a sensor that in is imported from the Hubitat integration as I am migrating over from Hubitat, when in Hubitat I was utilising these modes for Home, Night, Away & presence detection, so if all members became not present the mode would be set to away, if one or all became present home mode would be set, I had a list of devices that I monitor like lights, TV’s, computers and if they all go off during certain time periods night mode would be set, I do have other modes that I was utilising too but wont bore you will all the details. It then means I have have a different automation if the mode is set to night\home.

So for instance my rear garden security light when in home mode will come on at a certain colour temp and percentage, when in night mode it gets brighter due to a change in percentage and colour temp but I also get a notification on my phone to check it out on my camera the next morning, after this thread last night I have it working but have lots more to migrate over from Hubitat as majority of my automation are still working and exist there, it looks like I can also control the modes from HA into Hubitat.


These are my first automations

- id: '1637523993802'
  alias: Turn On Rear Garden Security Light when Motion is Detected (Home)
  description: ''
  trigger:
  - platform: state
    for:
      hours: 0
      minutes: 0
      seconds: 0
      milliseconds: 0
    entity_id: sensor.binary_sensor.hue_outdoor_motion_sensor_1_motion_2
  condition:
  - condition: state
    entity_id: sensor.hub_mode
    state: Home
  - type: is_illuminance
    condition: device
    device_id: f55ee4aa6c897bd26753bed686c2059b
    entity_id: sensor.hue_outdoor_motion_sensor_1_light_level_2
    domain: sensor
    below: 20
  action:
  - service: light.turn_on
    target:
      device_id: 355588737af8c3aa202dc270655e88cb
    data:
      brightness_pct: 75
      kelvin: 2700
  - delay:
      hours: 0
      minutes: 2
      seconds: 0
      milliseconds: 0
  - service: light.turn_off
    target:
      device_id: 355588737af8c3aa202dc270655e88cb
  mode: restart
- id: '1637536909269'
  alias: Turn On Rear Garden Security Light when Motion is Detected (Night & Away)
  description: ''
  trigger:
  - platform: state
    entity_id: binary_sensor.hue_outdoor_motion_sensor_1_motion_2
  condition:
  - condition: state
    entity_id: sensor.hub_mode
    state: Night
  - condition: or
    conditions:
    - condition: state
      entity_id: sensor.hub_mode
      state: Away
  - condition: and
    conditions:
    - type: is_illuminance
      condition: device
      device_id: f55ee4aa6c897bd26753bed686c2059b
      entity_id: sensor.hue_outdoor_motion_sensor_1_light_level_2
      domain: sensor
      below: 20
  action:
  - service: light.turn_on
    target:
      device_id: 355588737af8c3aa202dc270655e88cb
    data:
      kelvin: 6400
      brightness_pct: 95
  - service: notify.mobile_app_sm_n976b
    data:
      message: Rear Security Light Activated during Night or Away mode.
  - delay:
      hours: 0
      minutes: 3
      seconds: 0
      milliseconds: 0
  - service: light.turn_off
    target:
      device_id: 355588737af8c3aa202dc270655e88cb
  mode: restart

Hope you get the idea how it is working, I tested it last night and it does work the way I expect but these were my first 2 automation that I built in HA, I have around 70 further automation that I want to migrate onto HA but I guess this will take me time until I get the hang of building automations, starting with the more simple ones first :slight_smile: thanks for the input though I do appreciate it

Thanks again, I will try this as it sound like a slightly better way and have been using the developer tools already to change states to test and find states, the states are on & off.