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