Variable not occupied time threshold

Hi,

I want to set different non occupancy time threshold depending on a helper variable setting for a Hue motion sensor. The time out is in a range of 30s to 5min, depending on condition. I am happy to use yaml code for the “for” variable to set it, e.g. via a text helper. But I don’t know how to read the time into the correct format for the for variable. I am happy to explore other ideas as well, as long it uses native HA code, without plugins.

So far I tried using a 30s time out and setting a variable non occupancy trigger in the action via choices. This does not work, it gets always triggered at 30s and if all condition fails it won’t get triggered again. I need the variable time in the trigger section of the automation. Is that possible to do?

That is the code I am using so far:

alias: Kitchen Off
description: ""
trigger:
  - type: not_occupied
    platform: device
    device_id: cd6a9e4201e53de67c994b93881c75cb
    entity_id: binary_sensor.kitchen_motion_sensor_occupancy
    domain: binary_sensor
    for:
      hours: 0
      minutes: 0
      seconds: 30
condition: []
action:
  - choose:
      - conditions:
          - condition: state
            entity_id: input_select.ground_floor_mode
            state: Night
        sequence:
          - service: script.kitchen_off
            data: {}
      - conditions:
          - condition: state
            entity_id: input_select.ground_floor_mode
            state: Emergency
        sequence:
          - stop: ""
      - conditions:
          - condition: and
            conditions:
              - condition: state
                entity_id: input_select.ground_floor_mode
                state: Bright
              - condition: state
                entity_id: binary_sensor.kitchen_motion_sensor_occupancy
                state: "off"
                for:
                  hours: 0
                  minutes: 5
                  seconds: 0
        sequence:
          - service: script.kitchen_off
            data: {}
      - conditions:
          - condition: and
            conditions:
              - condition: time
                after: "06:30:00"
                before: "17:00:00"
              - condition: state
                entity_id: binary_sensor.kitchen_motion_sensor_occupancy
                state: "off"
                for:
                  hours: 0
                  minutes: 2
                  seconds: 0
        sequence:
          - service: script.kitchen_off
            data: {}
      - conditions:
          - condition: and
            conditions:
              - condition: time
                after: "17:00:00"
                before: "20:30:00"
              - condition: state
                entity_id: binary_sensor.kitchen_motion_sensor_occupancy
                state: "off"
                for:
                  hours: 0
                  minutes: 5
                  seconds: 0
        sequence:
          - service: script.kitchen_off
            data: {}
      - conditions:
          - condition: and
            conditions:
              - condition: time
                after: "20:30:00"
                before: "06:30:00"
              - condition: state
                entity_id: binary_sensor.kitchen_motion_sensor_occupancy
                state: "off"
                for:
                  hours: 0
                  minutes: 1
                  seconds: 0
        sequence:
          - service: script.kitchen_off
            data: {}
    default: []
mode: queued
max: 3

Welcome to the forums!

Please post your automation.

Instructions here under No. 11: How to help us help you - or How to ask a good question

Sure. I edited the my post.

I would solve it this way:


  trigger:
    - platform: state
      entity_id: input_select.ground_floor_mode

  action:
    - variables:
        door: "{{ states('binary_sensor.kitchen_motion_sensor_occupancy') == 'off' }}"
        duration: |-
           {% set lc = states.kitchen_motion_sensor_occupancy.last_changed %}
           {{ (now() - lc).total_seconds() }}

    - if:
        - or:
            - "{{ trigger.to_state.state == 'Night' }}"
  
            - and:
                - "{{ trigger.to_state.state == 'Bright' }}"
                - "{{ door and duration >= 300 }}"
  
            - and:
                - condition: time
                  after: "06:30:00"
                  before: "17:00:00"
                - "{{ door and duration >= 120 }}"
  
            - and:
                - condition: time
                  after: "17:00:00"
                  before: "20:30:00"
                - "{{ door and duration >= 300 }}"
  
            - and:
                - condition: time
                  after: "20:30:00"
                  before: "06:30:00"
                - "{{ door and duration >= 60 }}"
  
      then:
        - service: script.kitchen_off

Maybe I’m ignorant, but I don’t see a way of using the binary sensor as trigger and choose between different durations (except of using multiple wait for trigger templates).

I dropped the Emergency mode because I didn’t understand its function in regard of the automation flow.

Hi,

I think, I figured it out by myself and recreated all kitchen related settings. This is my solution:

Goal: non occupancy time of a Hue motion sensor in the kitchen depending on condition of drop down helpers

First, a drop down menu helper defines the mode of the kitchen, set automatically by time or button presses. The modes are

Day
Evening
Late Evening
Night
Sleep
Cooking

In addition, another drop down menu helper for the ground floor, where the kitchen is locate can also define non occupancy time. The relevant modes for the kitchen are:

Emergency
Bright

To allow variable non occupancy times for the kitchen. All possible non occupancy times must be set as triggers in the kitchen light off automation. The action is defined as choises, using the states of the drop down helpers and the duration of the off-state of the non occupancy variable of the motion sensor. The duration must match on of the triggers.

alias: Kitchen Off
description: ""
trigger:
  - type: not_occupied
    platform: device
    device_id: cd6a9e4201e53de67c994b93881c75cb
    entity_id: binary_sensor.kitchen_motion_sensor_occupancy
    domain: binary_sensor
    for:
      hours: 0
      minutes: 0
      seconds: 30
  - type: not_occupied
    platform: device
    device_id: cd6a9e4201e53de67c994b93881c75cb
    entity_id: binary_sensor.kitchen_motion_sensor_occupancy
    domain: binary_sensor
    for:
      hours: 0
      minutes: 1
      seconds: 0
  - type: not_occupied
    platform: device
    device_id: cd6a9e4201e53de67c994b93881c75cb
    entity_id: binary_sensor.kitchen_motion_sensor_occupancy
    domain: binary_sensor
    for:
      hours: 0
      minutes: 2
      seconds: 0
  - type: not_occupied
    platform: device
    device_id: cd6a9e4201e53de67c994b93881c75cb
    entity_id: binary_sensor.kitchen_motion_sensor_occupancy
    domain: binary_sensor
    for:
      hours: 0
      minutes: 5
      seconds: 0
  - type: not_occupied
    platform: device
    device_id: cd6a9e4201e53de67c994b93881c75cb
    entity_id: binary_sensor.kitchen_motion_sensor_occupancy
    domain: binary_sensor
    for:
      hours: 0
      minutes: 10
      seconds: 0
condition: []
action:
  - choose:
      - conditions:
          - condition: state
            entity_id: input_select.kitchen_mode
            state: Sleep
        sequence:
          - service: light.turn_off
            data: {}
            target:
              area_id: kitchen
      - conditions:
          - condition: state
            entity_id: input_select.kitchen_mode
            state: Day
          - condition: state
            entity_id: binary_sensor.kitchen_motion_sensor_occupancy
            for:
              hours: 0
              minutes: 2
              seconds: 0
            state: "off"
        sequence:
          - service: light.turn_off
            data: {}
            target:
              area_id: kitchen
      - conditions:
          - condition: state
            entity_id: input_select.kitchen_mode
            state: Evening
            for:
              hours: 0
              minutes: 0
              seconds: 0
          - condition: state
            entity_id: binary_sensor.kitchen_motion_sensor_occupancy
            for:
              hours: 0
              minutes: 2
              seconds: 0
            state: "off"
        sequence:
          - service: light.turn_off
            data: {}
            target:
              area_id: kitchen
      - conditions:
          - condition: state
            entity_id: input_select.kitchen_mode
            state: Late Evening
          - condition: state
            entity_id: binary_sensor.kitchen_motion_sensor_occupancy
            for:
              hours: 0
              minutes: 1
              seconds: 0
            state: "off"
        sequence:
          - service: light.turn_off
            data: {}
            target:
              area_id: kitchen
      - conditions:
          - condition: state
            entity_id: input_select.kitchen_mode
            state: Night
          - condition: state
            entity_id: binary_sensor.kitchen_motion_sensor_occupancy
            for:
              hours: 0
              minutes: 0
              seconds: 30
            state: "off"
        sequence:
          - service: light.turn_off
            data: {}
            target:
              area_id: kitchen
      - conditions:
          - condition: state
            entity_id: input_select.ground_floor_mode
            state: Bright
          - condition: state
            entity_id: binary_sensor.kitchen_motion_sensor_occupancy
            for:
              hours: 0
              minutes: 10
              seconds: 0
            state: "off"
        sequence:
          - service: light.turn_off
            data: {}
            target:
              area_id: kitchen
      - conditions:
          - condition: state
            entity_id: input_select.ground_floor_mode
            state: Emergency
        sequence:
          - stop: Light must stay on in an emergency
      - conditions:
          - condition: state
            entity_id: input_select.kitchen_mode
            state: Cooking
          - condition: state
            entity_id: binary_sensor.kitchen_motion_sensor_occupancy
            state: "off"
            for:
              hours: 0
              minutes: 5
              seconds: 0
        sequence:
          - service: light.turn_off
            data: {}
            target:
              area_id: kitchen
    default:
      - condition: state
        entity_id: binary_sensor.kitchen_motion_sensor_occupancy
        for:
          hours: 0
          minutes: 10
          seconds: 0
        state: "off"
      - service: light.turn_off
        data: {}
mode: queued
max: 3

As a safe guard I added a default condition to switch off after 10 minutes if everything else fails for whatever reason.

BW J