Motion sensor + light + tv condition

Hey fellow Home Assistant users,

I’ve just started using HA with my new Hue bridge and motion sensor setup. I’m trying to create an automation that turns off the lights after 2m if no motion is detected and both tv’s are off.

I’ve created an automation that turns off the light when no motion is detected and the tv’s are off, but I can’t figure out how to delay that the light goes off.

I’ve set up the automation like this:

- id: '1538243191011'
  alias: Turn off lights - attick
  trigger:
  - entity_id: sensor.hue_motion_sensor__motion_attick
    from: 'on'
    platform: state
    to: 'off'
  condition:
  - condition: state
    entity_id: media_player.sony_XXXX
    state: 'off'
  - condition: state
    entity_id: media_player.panasonic_XXXX
    state: 'off'
  - condition: state
    entity_id: light.attick
    state: 'on'                                                           
  action:
  - service: homeassistant.turn_off
    entity_id: light.attick

I tried using (just above ‘condition:’):
for:
minutes: 2

But that doesn’t seem to work. I’m thinking that option doesn’t work with ‘condition’ but I’m not sure.

Can anyone give me any pointers? Thanks in advance for the help!

I think you’ll find the answer you’re looking for in this thread. Scroll down to Petro’s reaction for some nice examples.

Thanks for the tip. I’ll have a look at it

You need to post your YAML code properly formatted (see instructions at the top of the page), otherwise it’s basically impossible for anyone to spot common errors like improper indentation.

If your automation was correct and was working, then adding the “for” parameter to the trigger will definitely cause it to be delayed. Basically the way that works is once the trigger would normally have fired (without the for), it then waits the for period. If the trigger becomes false during that time (e.g., in your case, if the state changed), then the trigger will reset. But if the state continues to meet the trigger parameters for the specified “for” time, then it will fire.

So, basically, for the trigger part you should have:

  trigger:
    platform: state
    entity_id: sensor.hue_motion_sensor__motion_attick
    from: 'on'
    to: 'off'
    for:
      minutes: 2

Thanks for the explanation. I’ve changed the trigger accordingly. Is it normal to have the for/minutes part in brackets in the gui btw?

At the action part it’s not showing the entity_id part either. It only shows brackets at the ‘service data’ part at the bottom. I can only post one image atm. I’ve added the image in my next reply.

I’ll check the automation tonight since I’m not at home. In the meantime I’ll have a look at the formatting. I’m still very new to yaml. Thanks for all the help.

Image for the action part:

So my new code is:

- id: '1538243191011'
  alias: Turn off lights - attick
  trigger:
    platform: state
    entity_id: sensor.hue_motion_sensor__motion_attick
    from: 'on'
    to: 'off'
    for:
      minutes: 2
  condition:
  - condition: state
    entity_id: media_player.sony_XXXX
    state: 'off'
  - condition: state
    entity_id: media_player.panasonic_XXXX
    state: 'off'
  - condition: state
    entity_id: light.attick
    state: 'on'                                                           
  action:
  - service: homeassistant.turn_off
    entity_id: light.attick

I can’t speak to the automation editor in the frontend – I don’t use that.

But your automation YAML code looks ok now. (And thanks for formatting it properly! :slight_smile:)

Some minor comments…

You probably don’t need the last condition. Generally turning something off that is already off doesn’t do anything.

The homeassistant services can turn anything on or off or toggle them, and it’s good for doing that with a heterogeneous set of entities. But when you’re turning on a set of entities in the same “domain” (i.e., the first part of the entity ID), it’s more efficient to use the corresponding service. So, in this case, it’s probably better to call light.turn_off instead of homeassistant.turn_off.

Thanks for all the help. It’s working finally :). I’ve added an extra automation so the lights turn on when motion is detected after sunset and between 17:00 and 00:00:

- id: '1538243191011'
  alias: Turn off lights - attick
  trigger:
    platform: state
    entity_id: sensor.hue_motion_sensor__motion_attick
    from: 'on'
    to: 'off'
    for:
      minutes: 2
  condition:
  - condition: state
    entity_id: media_player.sony_49xf9005
    state: 'off'
  - condition: state
    entity_id: media_player.panasonic_58dxw904
    state: 'off'
  - condition: state
    entity_id: light.attick
    state: 'on'
  action:
  - service: light.turn_off
    entity_id: light.attick
- id: '1538672660104'
  alias: Turn on Attick light
  trigger:
    platform: state
    entity_id: sensor.hue_motion_sensor__motion_attick
    from: 'off'
    to: 'on'
  condition:
  - condition: state
    entity_id: sun.sun
    state: below_horizon
  - after: '17:00'
    before: '00:00'
    condition: time
  action:
  - service: light.turn_on
    entity_id: light.attick

I’m converting all my automations from the ‘All 4 Hue’ app so HA can deal with everything.

Maybe I should change this:

  - after: '17:00'
    before: '00:00'
    condition: time

to this:

  - condition: time
    after: '17:00'
    before: '00:00'

Those are the same. And they’re also the same as:

  - condition: time
    after: '17:00'

But if you have a condition for between sunset and sunrise, why would you also want a time-based condition as well?

If you’re trying to make a condition that is true only between sunset and midnight, then you can do it with just this:

  - condition: sun
    after: sunset

Yeah, you’re right about that. I had that configured via the ‘All 4 Hue’ app for the lights downstairs. I’m actually not sure why I set that up :laughing:.