This is for light in the bathroom, it is controlled by a motion sensor. I am using the motion sensor to turn on the light when walking into the room. But I did not find any good way to turn it off again when there is no movements. The motion sensor can’t see trough glass so it would switch off the lights while showering. I was then thinking it could start a timer for like 10 minutes or more when it does not detect any movementents.
It should start a timer when there is no movement, but I also want it to be cancelled and restart if there is movement within the set time. When the time is reached without any interruption it will then switch off the lights. How could I proceed with this? I was able to make a 10 minutes timer but missing the possibilites to restart the timer and so on. I would be very happy if someone could look into the code for me. Thanks
Whenever motion is sensed the timer will be started/restarted and the light will be turned on (which does nothing if it is already on.) Then when the timer finally finishes the light will be turned off.
Whenever motion is sensed the light will be turned on and the timer will be canceled (in case it was running.) When motion detection stops the timer will be started. When the timer expires the light will be turned off.
Well, I don’t see where you’re turning the light on, but the automation that watches for the timer to finish does look right.
It does also make me think that in my solution I should have had to: 'off' for the trigger in the first automation. (I.e., start the timer after motion is no longer detected, not when motion is first detected.) I’ll edit it.
EDIT: Oh, wait. You probably want three automations altogether. One to turn the light on when motion is detected, another that starts the timer when motion detection stops, and the third to turn the light off when the timer expires. Sorry about that. Editing the original suggestion again…
I think so. (It was only harder than it should be because I started by saying, “Easy peasy!” ) Just substitute the real entity_id’s. Glad to help. If you have any issues with it, don’t hesitate to say so.
- alias: Turn on lights after sunset
trigger:
- platform: sun
event: sunset
- platform: homeassistant
event: start
condition:
condition:
- condition: or
conditions:
- condition: sun
after: sunset
- condition: sun
before: sunrise
- condition: state
entity_id: light.LIVING_ROOM
state: 'on'
action:
service: light.turn_on
entity_id: light.HUE1, light.HUE2
EDIT: For those that might come across this later on without reading the rest of the topic, I’ve modified the automation above with a fix discussed below. (Basically I changed a condition for the state of sun.sun being below_horizon to one that uses sun conditions.)
Have you already adjusted your time zone and Latitude and longitude in your configuration.yaml?
If you don’t, it is set for the UTC time zone by default.
Yes, I it has correct longitude and latitude there.
latitude: 59.7505520
longitude: 10.8340330
and
time_zone: Europe/Oslo
But I can see that the Sun component is using 12 hour instead of 24 hour, does that matter?
Maybe it is the code. I think it should be
- condition: sun
after: sunset
Instead of:
- platform: sun
event: sunset
Is that correct?
It should not, as you are calling sunset, and not a specific time.
It might be trying to meet too many requirements.
Try using the living room light as the trigger, and after sunset as the condition.
this way, it will check for the sun set every time you turn on the living room light, rather than checking the living room light only once at sunset.
The automation I wrote is triggered either at sunset or when HA starts. I then checks to make sure it is between sunset and sunrise (i.e., below_horizon.) That condition is for when the automation is triggered by HA starting. You originally had the condition for the living room light being on; that’s why that condition is there.
If it did not run at last sunset then that must be because the living room light was not on.
As far as 12hr vs 24hr, that’s only how it’s displayed in the frontend. Internally all times are in UTC.
I still think you will have best results if you just use the light as the trigger, and below horizon as the condition.
You can also use it as a wait template with a long timeout, waiting for sunset after the light is triggered