Hi, hoping that someone might be able to help me with this.
I use an automation to control the lights in my bathroom.
PART 1 OF AUTOMATION
Turns on the lights in the bathroom when motion is detected. It then keeps the light on for a set time, if no motion has been detected during that time the lights turn off. Otherwise the set time repeats. This part works fine.
PART 2 OF AUTOMATION
The lights can also be turned on by a dimmer switch which is outside of the bathroom (so this part of the automation is triggered by the lights turning on rather than by motion). The automation should then keep the light on for a set time, if no motion has been detected during that time the lights should turn off. Otherwise the set time repeats.
The two separate parts of the automation basically use the same conditions but in the second part one of the conditions always reports as false so the automation just loops endlessly. When I test the condition in Developer Tools > Template I can see that it’s true. So I’m kind of stumped.
# Bathroom motion lights
- id: '7591950598'
alias: bathroom_motion_lights
mode: single
trigger:
- platform: homeassistant
event: start
- platform: state
entity_id: binary_sensor.bathroom_multisensor_motion
to: 'on'
- platform: state
entity_id: light.bathroom_pendant
to: 'on'
condition:
- '{{ is_state("input_boolean.bath_time", "off") }}'
action:
- wait_template: >-
{% set ignore = ["unknown", "unavailable"] %}
{{ states("light.bathroom_pendant_zha") not in ignore
and states("light.bathroom_pendant") not in ignore
and states("binary_sensor.bathroom_multisensor_motion") not in ignore }}
- variables:
motion_clear_1: >
{{ is_state("binary_sensor.bathroom_multisensor_motion", "off")
and (as_timestamp(now()) - as_timestamp(states.binary_sensor.bathroom_multisensor_motion.last_changed) | default(0) | int > 1*60) }}
motion_clear_10: >
{{ is_state("binary_sensor.bathroom_multisensor_motion", "off")
and (as_timestamp(now()) - as_timestamp(states.binary_sensor.bathroom_multisensor_motion.last_changed) | default(0) | int >= 10*60) }}
motion_clear_20: >
{{ is_state("binary_sensor.bathroom_multisensor_motion", "off")
and (as_timestamp(now()) - as_timestamp(states.binary_sensor.bathroom_multisensor_motion.last_changed) | default(0) | int >= 20*60) }}
motion_clear_30: >
{{ is_state("binary_sensor.bathroom_multisensor_motion", "off")
and (as_timestamp(now()) - as_timestamp(states.binary_sensor.bathroom_multisensor_motion.last_changed) | default(0) | int >= 30*60) }}
- choose:
# IF trigger is motion detected
- conditions:
- '{{ trigger.entity_id == "binary_sensor.bathroom_multisensor_motion" }}'
- '{{ is_state("light.bathroom_pendant", "off") }}'
sequence:
- choose:
# IF sun is above horizon and bathroom pendant is off
- conditions: '{{ is_state("sun.sun", "above_horizon") }}'
sequence:
- service: light.turn_on
target:
entity_id: light.bathroom_pendant
data:
transition: 2
brightness_pct: 100
- repeat:
sequence:
- delay: '{{ "00:30:00" if is_state("binary_sensor.bathroom_humidity_bayesian", "on") else "00:20:00"}}'
until:
condition: or
conditions:
# PROBLEM: Condition works here.
- '{{ motion_clear_30 if is_state("binary_sensor.bathroom_humidity_bayesian", "on") else motion_clear_20 }}'
- '{{ is_state("light.bathroom_pendant", "off") }}'
- wait_for_trigger:
- platform: template
value_template: '{{ is_state("light.bathroom_pendant", "off") }}'
timeout: '00:00:10'
continue_on_timeout: true
- choose:
- conditions: '{{ not wait.trigger }}'
sequence:
- service: light.turn_off
target:
entity_id: light.bathroom_pendant
data:
transition: 2
# IF sun is below horizon and bathroom pendant is off
# Bathroom nightlight
- conditions: '{{ is_state("sun.sun", "below_horizon") }}'
sequence:
- service: light.turn_on
target:
entity_id: light.bathroom_led_strip
data:
transition: 2
brightness_pct: 10
- repeat:
sequence:
- delay: '00:10:00'
until:
condition: or
conditions:
- '{{ motion_clear_10 }}'
- '{{ is_state("light.bathroom_pendant", "on") }}'
- wait_for_trigger:
- platform: template
value_template: '{{ is_state("light.bathroom_pendant", "on") }}'
timeout: '00:00:10'
continue_on_timeout: true
- choose:
- conditions: '{{ not wait.trigger }}'
sequence:
- service: light.turn_off
target:
entity_id: light.bathroom_led_strip
data:
transition: 2
# IF trigger is light.bathroom_pendant and not motion
- conditions:
- '{{ is_state("light.bathroom_pendant", "on") if trigger.event == "start" else trigger.entity_id == "light.bathroom_pendant" }}'
- '{{ is_state("binary_sensor.bathroom_multisensor_motion", "off") if trigger.event == "start" else motion_clear_1 }}'
sequence:
- choose:
- conditions: '{{ is_state("sun.sun", "above_horizon") }}'
sequence:
- repeat:
sequence:
- delay: '{{ "00:30:00" if is_state("binary_sensor.bathroom_humidity_bayesian", "on") else "00:20:00"}}'
until:
condition: or
conditions:
# PROBLEM: Condition always reports false
- '{{ motion_clear_30 if is_state("binary_sensor.bathroom_humidity_bayesian", "on") else motion_clear_20 }}'
- '{{ is_state("light.bathroom_pendant", "off") }}'
- wait_for_trigger:
- platform: template
value_template: '{{ is_state("light.bathroom_pendant", "off") }}'
timeout: '00:00:10'
continue_on_timeout: true
- choose:
- conditions: '{{ not wait.trigger }}'
sequence:
- service: light.turn_off
target:
entity_id: light.bathroom_pendant
data:
transition: 2
- conditions: '{{ is_state("sun.sun", "below_horizon") }}'
sequence:
- repeat:
sequence:
- delay: '{{ "00:30:00" if is_state("binary_sensor.bathroom_humidity_bayesian", "on") else "00:10:00"}}'
until:
condition: or
conditions:
# PROBLEM: Condition always reports false
- '{{ motion_clear_30 if is_state("binary_sensor.bathroom_humidity_bayesian", "on") else motion_clear_10 }}'
- '{{ is_state("light.bathroom_pendant", "off") }}'
- wait_for_trigger:
- platform: template
value_template: '{{ is_state("light.bathroom_pendant", "off") }}'
timeout: '00:00:10'
continue_on_timeout: true
- choose:
- conditions: '{{ not wait.trigger }}'
sequence:
- service: light.turn_off
target:
entity_id: light.bathroom_pendant
data:
transition: 2
Here’s a trace:
{
"trace": {
"last_step": "action/2/choose/1/sequence/0/choose/0/sequence/0/repeat/until/0/conditions/1",
"run_id": "7",
"state": "running",
"script_execution": null,
"timestamp": {
"start": "2021-06-19T16:35:28.346641+00:00",
"finish": null
},
"domain": "automation",
"item_id": "7591950598",
"trigger": "Home Assistant starting",
"trace": {
"trigger/0": [
{
"path": "trigger/0",
"timestamp": "2021-06-19T16:35:28.346752+00:00",
"changed_variables": {
"trigger": {
"platform": "homeassistant",
"event": "start",
"description": "Home Assistant starting",
"id": "0"
}
}
}
],
"condition/0": [
{
"path": "condition/0",
"timestamp": "2021-06-19T16:35:28.346899+00:00",
"result": {
"result": true,
"entities": [
"input_boolean.bath_time"
]
}
}
],
"action/0": [
{
"path": "action/0",
"timestamp": "2021-06-19T16:35:28.436101+00:00",
"changed_variables": {
"context": {
"id": "270567fad461901e551e7a0742810b21",
"parent_id": null,
"user_id": null
}
},
"result": {
"wait": {
"remaining": null,
"completed": true
}
}
}
],
"action/1": [
{
"path": "action/1",
"timestamp": "2021-06-19T16:35:43.287406+00:00",
"changed_variables": {
"wait": {
"remaining": null,
"completed": true
}
}
}
],
"action/2": [
{
"path": "action/2",
"timestamp": "2021-06-19T16:35:43.292839+00:00",
"changed_variables": {
"motion_clear_1": false,
"motion_clear_10": false,
"motion_clear_20": false,
"motion_clear_30": false
},
"result": {
"choice": 1
}
}
],
"action/2/choose/0": [
{
"path": "action/2/choose/0",
"timestamp": "2021-06-19T16:35:43.295317+00:00",
"result": {
"result": false
}
}
],
"action/2/choose/0/conditions/0": [
{
"path": "action/2/choose/0/conditions/0",
"timestamp": "2021-06-19T16:35:43.295668+00:00",
"result": {
"result": false,
"entities": []
}
}
],
"action/2/choose/1": [
{
"path": "action/2/choose/1",
"timestamp": "2021-06-19T16:35:43.296274+00:00",
"result": {
"result": true
}
}
],
"action/2/choose/1/conditions/0": [
{
"path": "action/2/choose/1/conditions/0",
"timestamp": "2021-06-19T16:35:43.296380+00:00",
"result": {
"result": true,
"entities": [
"light.bathroom_pendant"
]
}
}
],
"action/2/choose/1/conditions/1": [
{
"path": "action/2/choose/1/conditions/1",
"timestamp": "2021-06-19T16:35:43.297149+00:00",
"result": {
"result": true,
"entities": [
"binary_sensor.bathroom_multisensor_motion"
]
}
}
],
"action/2/choose/1/sequence/0": [
{
"path": "action/2/choose/1/sequence/0",
"timestamp": "2021-06-19T16:35:43.345308+00:00",
"result": {
"choice": 0
}
}
],
"action/2/choose/1/sequence/0/choose/0": [
{
"path": "action/2/choose/1/sequence/0/choose/0",
"timestamp": "2021-06-19T16:35:43.346675+00:00",
"result": {
"result": true
}
}
],
"action/2/choose/1/sequence/0/choose/0/conditions/0": [
{
"path": "action/2/choose/1/sequence/0/choose/0/conditions/0",
"timestamp": "2021-06-19T16:35:43.346809+00:00",
"result": {
"result": true,
"entities": [
"sun.sun"
]
}
}
],
"action/2/choose/1/sequence/0/choose/0/sequence/0": [
{
"path": "action/2/choose/1/sequence/0/choose/0/sequence/0",
"timestamp": "2021-06-19T16:35:43.363886+00:00"
}
],
"action/2/choose/1/sequence/0/choose/0/sequence/0/repeat/sequence/0": [
{
"path": "action/2/choose/1/sequence/0/choose/0/sequence/0/repeat/sequence/0",
"timestamp": "2021-06-19T16:35:43.377740+00:00",
"changed_variables": {
"repeat": {
"first": true,
"index": 1
}
},
"result": {
"delay": 1200,
"done": true
}
},
{
"path": "action/2/choose/1/sequence/0/choose/0/sequence/0/repeat/sequence/0",
"timestamp": "2021-06-19T16:55:43.388633+00:00",
"changed_variables": {
"repeat": {
"first": false,
"index": 2
}
},
"result": {
"delay": 1200,
"done": false
}
}
],
"action/2/choose/1/sequence/0/choose/0/sequence/0/repeat": [
{
"path": "action/2/choose/1/sequence/0/choose/0/sequence/0/repeat",
"timestamp": "2021-06-19T16:55:43.383277+00:00",
"changed_variables": {
"repeat": {
"first": true,
"index": 1
}
},
"result": {
"result": false
}
}
],
"action/2/choose/1/sequence/0/choose/0/sequence/0/repeat/until/0": [
{
"path": "action/2/choose/1/sequence/0/choose/0/sequence/0/repeat/until/0",
"timestamp": "2021-06-19T16:55:43.383685+00:00",
"result": {
"result": false
}
}
],
"action/2/choose/1/sequence/0/choose/0/sequence/0/repeat/until/0/conditions/0": [
{
"path": "action/2/choose/1/sequence/0/choose/0/sequence/0/repeat/until/0/conditions/0",
"timestamp": "2021-06-19T16:55:43.383869+00:00",
"result": {
"result": false,
"entities": [
"binary_sensor.bathroom_humidity_bayesian"
]
}
}
],
"action/2/choose/1/sequence/0/choose/0/sequence/0/repeat/until/0/conditions/1": [
{
"path": "action/2/choose/1/sequence/0/choose/0/sequence/0/repeat/until/0/conditions/1",
"timestamp": "2021-06-19T16:55:43.385220+00:00",
"result": {
"result": false,
"entities": [
"light.bathroom_pendant"
]
}
}
]
},
"config": {
"id": "7591950598",
"alias": "bathroom_motion_lights",
"mode": "single",
"trigger": [
{
"platform": "homeassistant",
"event": "start"
},
{
"platform": "state",
"entity_id": "binary_sensor.bathroom_multisensor_motion",
"to": "on"
},
{
"platform": "state",
"entity_id": "light.bathroom_pendant",
"to": "on"
}
],
"condition": [
"{{ is_state(\"input_boolean.bath_time\", \"off\") }}"
],
"action": [
{
"wait_template": "{% set ignore = [\"unknown\", \"unavailable\"] %} {{ states(\"light.bathroom_pendant_zha\") not in ignore\n and states(\"light.bathroom_pendant\") not in ignore\n and states(\"binary_sensor.bathroom_multisensor_motion\") not in ignore }}"
},
{
"variables": {
"motion_clear_1": "{{ is_state(\"binary_sensor.bathroom_multisensor_motion\", \"off\")\n and (as_timestamp(now()) - as_timestamp(states.binary_sensor.bathroom_multisensor_motion.last_changed) | default(0) | int > 1*60) }} \n",
"motion_clear_10": "{{ is_state(\"binary_sensor.bathroom_multisensor_motion\", \"off\")\n and (as_timestamp(now()) - as_timestamp(states.binary_sensor.bathroom_multisensor_motion.last_changed) | default(0) | int >= 10*60) }}\n",
"motion_clear_20": "{{ is_state(\"binary_sensor.bathroom_multisensor_motion\", \"off\")\n and (as_timestamp(now()) - as_timestamp(states.binary_sensor.bathroom_multisensor_motion.last_changed) | default(0) | int >= 20*60) }}\n",
"motion_clear_30": "{{ is_state(\"binary_sensor.bathroom_multisensor_motion\", \"off\")\n and (as_timestamp(now()) - as_timestamp(states.binary_sensor.bathroom_multisensor_motion.last_changed) | default(0) | int >= 30*60) }}\n"
}
},
{
"choose": [
{
"conditions": [
"{{ trigger.entity_id == \"binary_sensor.bathroom_multisensor_motion\" }}",
"{{ is_state(\"light.bathroom_pendant\", \"off\") }}"
],
"sequence": [
{
"choose": [
{
"conditions": "{{ is_state(\"sun.sun\", \"above_horizon\") }}",
"sequence": [
{
"service": "light.turn_on",
"target": {
"entity_id": "light.bathroom_pendant"
},
"data": {
"transition": 2,
"brightness_pct": 100
}
},
{
"repeat": {
"sequence": [
{
"delay": "{{ \"00:30:00\" if is_state(\"binary_sensor.bathroom_humidity_bayesian\", \"on\") else \"00:20:00\"}}"
}
],
"until": {
"condition": "or",
"conditions": [
"{{ motion_clear_30 if is_state(\"binary_sensor.bathroom_humidity_bayesian\", \"on\") else motion_clear_20 }}",
"{{ is_state(\"light.bathroom_pendant\", \"off\") }}"
]
}
}
},
{
"wait_for_trigger": [
{
"platform": "template",
"value_template": "{{ is_state(\"light.bathroom_pendant\", \"off\") }}"
}
],
"timeout": "00:00:10",
"continue_on_timeout": true
},
{
"choose": [
{
"conditions": "{{ not wait.trigger }}",
"sequence": [
{
"service": "light.turn_off",
"target": {
"entity_id": "light.bathroom_pendant"
},
"data": {
"transition": 2
}
}
]
}
]
}
]
},
{
"conditions": "{{ is_state(\"sun.sun\", \"below_horizon\") }}",
"sequence": [
{
"service": "light.turn_on",
"target": {
"entity_id": "light.bathroom_led_strip"
},
"data": {
"transition": 2,
"brightness_pct": 10
}
},
{
"repeat": {
"sequence": [
{
"delay": "00:10:00"
}
],
"until": {
"condition": "or",
"conditions": [
"{{ motion_clear_10 }}",
"{{ is_state(\"light.bathroom_pendant\", \"on\") }}"
]
}
}
},
{
"wait_for_trigger": [
{
"platform": "template",
"value_template": "{{ is_state(\"light.bathroom_pendant\", \"on\") }}"
}
],
"timeout": "00:00:10",
"continue_on_timeout": true
},
{
"choose": [
{
"conditions": "{{ not wait.trigger }}",
"sequence": [
{
"service": "light.turn_off",
"target": {
"entity_id": "light.bathroom_led_strip"
},
"data": {
"transition": 2
}
}
]
}
]
}
]
}
]
}
]
},
{
"conditions": [
"{{ is_state(\"light.bathroom_pendant\", \"on\") if trigger.event == \"start\" else trigger.entity_id == \"light.bathroom_pendant\" }}",
"{{ is_state(\"binary_sensor.bathroom_multisensor_motion\", \"off\") if trigger.event == \"start\" else motion_clear_1 }}"
],
"sequence": [
{
"choose": [
{
"conditions": "{{ is_state(\"sun.sun\", \"above_horizon\") }}",
"sequence": [
{
"repeat": {
"sequence": [
{
"delay": "{{ \"00:30:00\" if is_state(\"binary_sensor.bathroom_humidity_bayesian\", \"on\") else \"00:20:00\"}}"
}
],
"until": {
"condition": "or",
"conditions": [
"{{ motion_clear_30 if is_state(\"binary_sensor.bathroom_humidity_bayesian\", \"on\") else motion_clear_20 }}",
"{{ is_state(\"light.bathroom_pendant\", \"off\") }}"
]
}
}
},
{
"wait_for_trigger": [
{
"platform": "template",
"value_template": "{{ is_state(\"light.bathroom_pendant\", \"off\") }}"
}
],
"timeout": "00:00:10",
"continue_on_timeout": true
},
{
"choose": [
{
"conditions": "{{ not wait.trigger }}",
"sequence": [
{
"service": "light.turn_off",
"target": {
"entity_id": "light.bathroom_pendant"
},
"data": {
"transition": 2
}
}
]
}
]
}
]
},
{
"conditions": "{{ is_state(\"sun.sun\", \"below_horizon\") }}",
"sequence": [
{
"repeat": {
"sequence": [
{
"delay": "{{ \"00:30:00\" if is_state(\"binary_sensor.bathroom_humidity_bayesian\", \"on\") else \"00:10:00\"}}"
}
],
"until": {
"condition": "or",
"conditions": [
"{{ motion_clear_30 if is_state(\"binary_sensor.bathroom_humidity_bayesian\", \"on\") else motion_clear_10 }}",
"{{ is_state(\"light.bathroom_pendant\", \"off\") }}"
]
}
}
},
{
"wait_for_trigger": [
{
"platform": "template",
"value_template": "{{ is_state(\"light.bathroom_pendant\", \"off\") }}"
}
],
"timeout": "00:00:10",
"continue_on_timeout": true
},
{
"choose": [
{
"conditions": "{{ not wait.trigger }}",
"sequence": [
{
"service": "light.turn_off",
"target": {
"entity_id": "light.bathroom_pendant"
},
"data": {
"transition": 2
}
}
]
}
]
}
]
}
]
}
]
}
]
}
]
},
"blueprint_inputs": null,
"context": {
"id": "270567fad461901e551e7a0742810b21",
"parent_id": null,
"user_id": null
}
},
"logbookEntries": [
{
"name": "bathroom_motion_lights",
"message": "has been triggered by Home Assistant starting",
"source": "Home Assistant starting",
"entity_id": "automation.bathroom_motion_lights",
"context_id": "270567fad461901e551e7a0742810b21",
"when": "2021-06-19T16:35:28.347697+00:00",
"domain": "automation"
}
]
}
Totally open to suggestions of how this could be simplified, if I’ve over complicated it.
Thanks for reading