Motion detected - lights on. no motion within 10mins, turn lights off

Hey,

How can i set this on Specific time ? like at in the evening 20:00 to 5:00 morning Only?
where i need to add the timer for 10 min off?
where i need to add the entity_id for the lamp and entity_id for the sensor motion ?

Thanks!
Mp

- alias: kids Night lamp
  trigger:
- platform: state
  entity_id: binary_sensor.XXXX
  to: 'on'
  condition:
- condition: time
  after: '20:00:00'
  before: '05:00:00'
  action:
- service: homeassistant.turn_on
  entity_id: binary_sensor.XXXX

Note the indentation (spacing) this is very important.

Note my comments below:

- id: kids_night_lamp_on
  alias: 'Kids Night Lamp On'
  trigger:
    - platform: state
      entity_id: binary_sensor.XXXX #<- This should be your motion sensor
      to: 'on'
  condition:
    - condition: time
        after: '20:00:00' # <- motion will only trigger the light on between these times
        before: '05:00:00'
  action:
    - service: homeassistant.turn_on
      entity_id: light.your_light # <- this should be your light entity_id

You will need another automation to turn the lights off after 10 minutes.

- id: kids_night_lamp_off
  alias: 'Kids Night Lamp Off'
  trigger:
    - platform: state
      entity_id: binary_sensor.XXXX #<- This should be your motion sensor
      to: 'off'
      for:
        minutes: 10
  action:
    - service: light.turn_off
      entity_id: light.your_light # <- this should be your light entity_id
1 Like

@mp3lllll

a general advice - when you post a piece of code, on a new string put 4 single backquotes, then new line, your code, newline and another 4 single backquotes, that’s to preserve formatting which is essential in HA

he’ll also need to think what to do if his “on” automation triggers more than once within 10 mins
 :wink:

His code was actually block quoted. Just had bad indentation. :upside_down_face: Also it’s 3x backquotes.

See my edited post. It does not matter if a light that is already on gets turned on again though a condition would remove this extraneous network traffic.

My off automation is triggered by no movement for 10 minutes, not the light being on for 10 minutes.

wow Tx!

i will test it!

It’s might be me, but I cannot call it a HA code without indentation like that i.e it won’t work.
And 4 backquotes work as well, try it.

Agree that your code is a good one :wink:

Done add it to my HA and all Green :slight_smile: i will test it today.

TX you agein! :heart_eyes:

@tom_l how he know to Connect them?
On if there is a motion.
Off if there is no motion for 10 min?

I don’t understand. Connect what?
Did you mean ‘How to test them?’

Correct.

Correct.

Tom. Thank for your time and your help.

i will test it today.

Hey Tom

You’re sure it should be in 2 separate automata?
No that just trun on at 20:00 and The second automation does nothing.

Yes it should be in 2 separate codes. One to turn light on after movement and between the hours you want. The second to turn off the light if no movement has been detected for 10 minutes.

I have very similar code for my lights working perfectly.

So what i miss here? i come home at 20:10 and the light is on without any movement .

until now is on and never off.

i change the service: homeassistant.turn_off i will test now

Ok, so if the above is your code, why turn on with homeassistant.turn_on service, but turn off with light service?

Have you tried using the right service through the developer section? If homesasstant.turn_on works, try homeassistant.turn_off to turn it off.

i change to homeassistant.turn_off from light.turn_off this resolve the issue :slight_smile:

1 Like

The reason light.turn_off won’t work is because you aren’t turning off a light. You are turning off a switch.

homeassistant.turn_xx works on any domain so that’s why it worked in the first automation. You should be using switch.turn_on/switch.turn_off for you service calls.

2 Likes

Yeah I assumed it was going to be a light not a light switch.

1 Like

Thank you very much, I learned something new


If you want your switch to behave as a light you can use this:

Not a huge advantage - light groups and lovelace light cards mainly.

1 Like

No need, this is just a lamp of my san room


big big TX tom.

1 Like