Automation setup for knx , almost there

I’m creating an automation for my KNX system to disable my alarm when motion is detected in the hallway between 6hr-11hr AM

Trigger is a motion sensor which goes from off>on.

As conditions i have an “and” condition with :
1/ Motion has to be “off” state during at least 4 hours
2/ The time has to be between 6 hr and 11 hr.

Action is “1” “send value to the knx bus”

alias: Auto Alarm UIT
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.lasersensors_trap
    from: "off"
    to: "on"
    for:
      hours: 0
      minutes: 0
      seconds: 0
condition:
  - condition: and
    conditions:
      - condition: state
        entity_id: binary_sensor.lasersensors_trap
        state: "off"
        for:
          hours: 4
          minutes: 0
          seconds: 0
      - condition: time
        after: "06:00:00"
        before: "11:00:00"
action:
  - service: knx.send
    data:
      address: 7/1/1
      response: false
      payload: 1
mode: single

When checking traces, I think the problem is with the 1st condition. (Also, if I remove the 1st condition, the automation works)

The conditions pass when I test, but when triggered the state of the sensor changes to “on” and therefore doesn’t pass the AND test ? I think ?

Then I thought I had to setup an “AND” logic for 2 triggers, but that’s not possible I think ?

I’d also like to expand with additional sensors that have to be on, so motion is detected in a path from the bedroom to downstairs. This way the alarm can go off in this scenario.

You could split it to two automations. One enabling/disabling the alarm → turning on/off the second automation. By daytime and/or the 4-hour-no-motion triggger.
The second is the alarm itself triggered by whatever you have in mind.

Thank about what you designed your automation to do:

  1. Triggers when the binary_sensor’s state changes to on.
  2. Checks if the binary_sensor current state has been off for the last 4 hours.

The condition in #2 can never be true because the binary_sensor is no longer off and is now on.

That’s why the automation works when you remove the condition because it’s impossible to fulfill.

I believe what you want the condition to check is if the binary_sensor’s previous state was off for at least 4 hours. Is that correct?

If so, then you’ll need to compute the time difference between now and the last time the binary_sensor’s state changed.

condition:
  - condition: template
    value_template: "{{ now() - trigger.from_state.last_changed > timedelta(hours=4) }}"
  - condition: time
    after: "06:00:00"
    before: "11:00:00"
2 Likes

Nice, this works!
Thank you so much! :sunglasses:

1 Like

I’d like try this as well, but how would I set this up ?

So for example I add a trigger for the time passed from ‘on’ to ‘off’ for 4hr, than what “action” do I take to input this into the second automation?

trigger:
  - platform: state
    entity_id:
      - binary_sensor.lasersensors_trap
    for:
      hours: 4
      minutes: 0
      seconds: 0
    from: "on"
    to: "off"

Have a look at Open your Home Assistant instance and show your state developer tools.
Every automation is an entity, thus has an entity_id.
It provides, amongst others, services automation.turn_on and automation.turn_off.
So you don’t need to pass anything to the second automation, but call the service to turn on/off the whole automation in the first.
All you need is to know the entity_id of the second automation in the first one.

1 Like