Trigger event on sensor activation, but only if it hasn't activated for a period of time

I have a Xiaomi Gateway with a window/door sensor. The sensor is on my front door, and I am attempting to trigger a light to go on when the door is opened, but only if the light level is below a certain value (using another Xiaomi sensor) and only if the sensor has not been activated for several minutes (to eliminate cases when I’ve closed the door and reopened it shortly afterwards).

The YAML I currently have looks like this:

- id: front_door_open
  alias: Turn on living room light when front door opens at night
  trigger:
    platform: state
    entity_id: binary_sensor.door_window_sensor_158d000149abe5
    from: 'off'
    to: 'on'
  condition:
    - condition: numeric_state
      entity_id: sensor.illumination_286c07891c0c
      below: 75
    - condition: state
      entity_id: light.living_room
      state: 'off'
    - condition: state
      entity_id: binary_sensor.door_window_sensor_158d000149abe5
      to: 'off'
      for: 
        minutes: 3
  action:
    service: light.turn_on
    data: 
      brightness: 254
      entity_id: light.living_room

This was working until I added the condition that the sensor hadn’t been activated for 3 minutes:

- condition: state
  entity_id: binary_sensor.door_window_sensor_158d000149abe5
  to: 'off'
  for: 
    minutes: 3

When I add this, I get the following when checking the config:

Invalid config for [automation]: extra keys not allowed @ data['condition'][2]['for']. Got None
extra keys not allowed @ data['condition'][2]['to']. Got None
not a valid value for dictionary value @ data['condition'][2]['condition']. Got None. (See /config/configuration.yaml, line 256). Please check the docs at https://home-assistant.io/components/automation/

I suspect this is due to using the same sensor as the trigger and as a condition. Is this the case? How can I achieve what I’m trying to do here?

I’ve searched around and have not found an answer, but may be searching for the wrong thing.

Thanks

It’s “state”, not “to”:

Sebastian

2 Likes
- condition: state
  entity_id: binary_sensor.door_window_sensor_158d000149abe5
  state: 'off' # WRONG ATTRIBUTE LABEL
  for: 
    minutes: 3
1 Like

That’s what happens when you copy from another part of your config.

It is, however, still not working. I’m wondering if as soon as it triggers, it means the state changes to “on”, so this condition will never be met. Is there a way to check the previous state of the sensor?

Excellent point!

I just recently implemented something close to what you want. Maybe it will work for you, too.

Basically, I wanted an automation to run based on certain triggers and conditions, but only if it hadn’t run for a certain amount of time. I came up with this condition:

- condition: template
  value_template: >
    {% set last_run = as_timestamp(
             state_attr('automation.OBJECT_ID_OF_AUTOMATION', 'last_triggered')) %}
    {{ (last_run is none or (now().timestamp() - last_run) > 3*60 }}

This works because when this condition is being evaluated, the automation’s state has not yet been updated for this “run”, so last_triggered is the date & time of the last time it actually ran.

EDIT: Alternatively, I suppose you could use trigger.from_state.last_changed, which is the time binary_sensor.door_window_sensor_158d000149abe5 changed to ‘off’ just before it changed to ‘on’ (which, would be it’s current last_changed, and trigger.to_state.last_changed.) Does that make sense?

5 Likes

O My Idol !

That is amazing !

Bookmarked.

You could also start (or reset) a 3min-timer every time you turn off the light and then check its state when the door sensor triggers.
But @pnbruckner’s solution seems more elegant :wink:

Sebastian

Nut just noticed that I’ve not got any IDs on my automatons, Another thing to add to the list.