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!
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
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.
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! )
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.