Cannot set automation based on a state

Hi everyone,

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.

how can I do ?

Here is my automation :

alias: Test
description: ""
trigger:
  - platform: time_pattern
    id: EVERYHOUR
    hours: "1"
    alias: EVERY HOUR
  - type: no_motion
    platform: device
    device_id: 0b46e8532d4579b21d7860e1469c7374
    entity_id: binary_sensor.detecteur_mouvements_sdb_motion
    domain: binary_sensor
    for:
      hours: 0
      minutes: 15
      seconds: 0
    id: NOMOTION
condition: []
action:
  - if:
      - condition: trigger
        id: EVERYHOUR
    then:
      - type: turn_on
        device_id: 3b733f8d3bc0df029ac76de9d6d07687
        entity_id: switch.diffuseur_salon
        domain: switch
      - service: notify.mobile_app_iphone_13
        data:
          message: 🏮 Diffuseur Salon Allumé 🏮
    alias: EVERY HOUR = TURN ON
  - if:
      - condition: trigger
        id: NOMOTION
    then:
      - type: turn_off
        device_id: 3b733f8d3bc0df029ac76de9d6d07687
        entity_id: switch.diffuseur_salon
        domain: switch
    alias: NO MOTION = OFF
mode: single

Thanks for your help

Not sure I understand the 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:

  1. have an automation that triggers when the light is on for 15 minutes with a condition that the motion sensor should be off
  2. have the automation wait for 15 minutes and then check (condition) if the motion sensor is of
  3. 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”.

Thanks for the every hour trigger, I’vet set it correctly now

platform: time_pattern
hours: /1

And regarding the motion sensor the problem I have if I set the trigger as you mentionned is that :

  • When the device turns on every hour :
    IF THE MOTION SENSOR STATE IS ALREADY IN “NOT DETECTED” then trigger is not working as the state is not changing

Don’t know if i’m clear ?

Yes you understood correctly, that’s what I want to do but I’m not sure to understand your first option.

should I create a new automation aside ? I do not want to turn off my motion sensor

This is what I had in mind:

description: ""
mode: single
trigger:
  - platform: time_pattern
    hours: /1
condition: []
action:
  - service: light.turn_on
    data: {}
    target:
      entity_id: light.dimmable_light_1
  - wait_for_trigger:
      - platform: state
        entity_id:
          - binary_sensor.motion_sensor
        to: "off"
    timeout:
      hours: 0
      minutes: 15
      seconds: 0
      milliseconds: 0
  - service: light.turn_off
    data: {}
    target:
      entity_id: light.dimmable_light_1

The only downside in this is that when you meanwhile edit the automation or reboot (during the wait), it will fail to turn off the light.

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.

I think that does what you want.

Yes I could add the trigger for the diffuseur being on for 15mns and it will work

But it doesn’t resolve the NO MOTION Trigger or Condition which will still not work as the state is not changing from “SOMETHING” to “Not Detected”

Why do you care about that? Please explain a situation starting at the 10:00 trigger where my automation above does not do what you want.

I will fully explain what I want to do :

  • I want to turn on my Air Diffuser every hour
  • 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

More clear for you ? :slight_smile:

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.

1 Like

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:
...

Hey mate,

You’re right, this morning I left home at 09:50 and the diffuser stopped at 10:05


I’ve modified as you described above with a “AND” instead of “OR” in the condition. I’ll let you know if it’s working fine so thanks for that.

Now I would like to considere another scenario :

  • Diffuser sarts at 1PM
  • At 1.18PM let’s say I want to stop it
  • I want the next diffuser to start one hour after I stopped it manually (2.18PM instead of 2PM)

How can I do so ? I guess it’s a completely different trigger

Trigger on switch.diffuseur_salon being off for an hour instead of the time pattern trigger.

platform: state
entity_id:
  - switch.diffuseur_salon
to: "off"
for:
  hours: 1
  minutes: 
  seconds: 0
id: "1"

Like this ?

Yes: don’t leave minutes blank though.

I tried with the following but it’s not triggering after 1mns off (I modified the times for fast testing) :

alias: Test
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.detecteur_mouvements_sdb_motion
    to: "off"
    for:
      hours: 0
      minutes: 0
      seconds: 30
    id: NOMOTION
  - platform: state
    entity_id:
      - switch.diffuseur_salon
    to: "on"
    for:
      hours: 0
      minutes: 0
      seconds: 30
    id: NOMOTION
  - platform: state
    entity_id:
      - switch.diffuseur_salon
    to: "off"
    for:
      hours: 0
      minutes: 1
      seconds: 0
    id: "1"
condition:
  - condition: and
    conditions:
      - condition: state
        entity_id: binary_sensor.detecteur_mouvements_sdb_motion
        state: "off"
        for:
          hours: 0
          minutes: 15
          seconds: 0
      - condition: trigger
        id: "1"
action:
  - if:
      - condition: trigger
        id: "1"
    then:
      - type: turn_on
        device_id: 3b733f8d3bc0df029ac76de9d6d07687
        entity_id: switch.diffuseur_salon
        domain: switch
    alias: EVERY HOUR = ON
  - if:
      - condition: trigger
        id: NOMOTION
    then:
      - type: turn_off
        device_id: 3b733f8d3bc0df029ac76de9d6d07687
        entity_id: switch.diffuseur_salon
        domain: switch
    alias: NO MOTION = OFF
mode: single

Did i make a mistake ?

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.

Is it normal i have this yellow error ? It says the model is not supported but only in YAML