I’m trying to setup a simple automation which turn on a device every hour and if no motion is detected for 15mns then turn off that device.
The problem is if the state of the motion sensor is not changing from “Detected” to “Not Detected” then it’s not turning off the device. I didn’t find a way to resolve this issue.
First of all, that time_pattern trigger will only fire once per day at 01:00. You want hours: "/1" or alternatively minutes: "0". Automation Trigger - Home Assistant
For the motion sensor, a simpler trigger would be:
- platform: state
entity_id: binary_sensor.detecteur_mouvements_sdb_motion
to: 'off'
for: '00:15:00'
id: NOMOTION
but if the motion sensor isn’t turning off when no motion is detected, I don’t know how you think HA is going to resolve that.
So if I understand you correctly you want to turn on the light, without the motion sensor being triggered, and then turn it off after 15 minutes if the motion sensor does not have the state on?
There are a lot of solutions for that, for example:"
Toggle the light to on and then:
have an automation that triggers when the light is on for 15 minutes with a condition that the motion sensor should be off
have the automation wait for 15 minutes and then check (condition) if the motion sensor is of
have the automation wait for 15 minutes with a trigger for the motion sensor to ‘off’, and then continue to turn off the light. So it will be turned off my the state of the sensor and if that never happens, it will turn off after 15m
Disclaimer, never used option 3, but I assume that if the sensor is already off, it’s not considered to be a trigger. Your every hour pattern should by the way be “/1”.
Ah, I understand now. Add a trigger for the diffuser being on for 15 minutes, and a condition that there must have been no motion for at least 15 minutes:
trigger:
- platform: time_pattern
id: EVERYHOUR
hours: "1"
alias: EVERY HOUR
- platform: state
entity_id: binary_sensor.detecteur_mouvements_sdb_motion
to: 'off'
for: '00:15:00'
id: NOMOTION
- platform: state
entity_id: switch.diffuseur_salon
to: 'on'
for: '00:15:00'
id: NOMOTION
condition:
- or:
- condition: state
entity_id: binary_sensor.detecteur_mouvements_sdb_motion
state: 'off'
for: "00:15:00"
- condition: trigger
id: EVERYHOUR
action:
...
So that will
trigger every hour to turn on
trigger after being on for 15 minutes or after 15 minutes of no motion
And the condition only lets the flow continue if it’s the turn-on trigger or if there has been no motion for 15 minutes.
If motion is detected (means i’m in the room) then leaves the Air Diffuser running until no motion is detected for 15mns
If NO motion is detected for 15mns then turn off the air diffuser
The last step above will work just fine IF i was in the room let’s say for 30mns and then leave. It will just turn off the device after 15mns and the last status of the motion will be “Not Detected”
But let’s say i’m not in the room when the Diffuser starts running at the next hour 1PM
When it will start, the state of the motion sensor will be “Not Detected” and so it won’t change it after 15mns so the diffuser will just keep running and not turn off for hours until I come back in the room
Perfectly clear, and my automation above should work as you want. The diffuser will switch off after 15 minutes (third trigger) provided there has been no motion (first condition).
The only caveat is that if you leave the room at 12:50, the diffuser will switch off at 13:05. If that’s likely to be a problem you could build in another condition to ensure that the diffuser has been on for 15 minutes.
Your solution is working great! Sorry I did it wrong before
Thanks a lot for your help and for the 12:50 scenario, it should turn on the Diffuser at 13:00 no ?
Switch off will occur then at 13:15 ? There is no problem
It’ll switch off after there has been no motion for 15 minutes — it doesn’t matter when that 15-minute period starts. There isn’t any “sequence” to the triggers: any one of them can fire.
12:50: you leave the room, motion sensor reports off
13:00: Time pattern trigger fires, diffuser goes on
13:05: No motion for 15 minutes trigger fires, diffuser goes off
This will fix it: it’ll only switch off if there’s been no motion for 15 minutes AND the diffuser has been on for 15 minutes - it now won’t run for less than 15 minutes:
trigger:
- platform: time_pattern
id: EVERYHOUR
hours: "1"
alias: EVERY HOUR
- platform: state
entity_id: binary_sensor.detecteur_mouvements_sdb_motion
to: 'off'
for: '00:15:00'
id: NOMOTION
- platform: state
entity_id: switch.diffuseur_salon
to: 'on'
for: '00:15:00'
id: NOMOTION
condition:
- or:
- condition: trigger
id: EVERYHOUR
- and:
- condition: state
entity_id: binary_sensor.detecteur_mouvements_sdb_motion
state: 'off'
for: "00:15:00"
- condition: state
entity_id: switch.diffuseur_salon
state: 'on'
for: "00:15:00"
action:
...
Two mistakes, I think. Don’t use numbers for trigger IDs, as they can get confused with index referencing:
…and your condition will only let the flow get to the action if there has been no motion for 15 minutes AND the trigger is your new one.
Try this:
alias: Diffuser manager
trigger:
- alias: "no motion for 15 mins"
platform: state
entity_id: binary_sensor.detecteur_mouvements_sdb_motion
to: "off"
for: "00:15:00"
id: "off"
- alias: "diffuser has been on for 15 mins"
platform: state
entity_id: switch.diffuseur_salon
to: "on"
for: "00:15:00"
id: "off"
- alias: "diffuser has been off for an hour"
platform: state
entity_id: switch.diffuseur_salon
to: "off"
for: "01:00:00"
id: "on"
condition:
- or:
- alias: "want to switch diffuser on"
condition: trigger
id: "on"
- and:
- alias: "no motion for 15 mins"
condition: state
entity_id: binary_sensor.detecteur_mouvements_sdb_motion
state: "off"
for: "00:15:00"
- alias: "diffuser on for 15 mins"
condition: state
entity_id: switch.diffuseur_salon
state: "on"
for: "00:15:00"
action:
- service: switch.turn_{{ trigger.id }}
entity_id: switch.diffuseur_salon
Switches the diffuser ON when it has been OFF for an hour; and OFF when it has been ON for 15 minutes AND there has been no motion for 15 minutes.