Hello again,
Still a noob to HA. Trying to get Athom’s human presence sensor automation honed in. But, am not quite there just yet. The idea is to eventually create a blueprint which:
- allows user to input the
area_id
of interest - find all the lights, switches etc in that area
- detect all presence sensors (and others in the future e.g. door sensor)
- turn on lights as long as there is occupancy; in future, other services too.
- wait for involved sensors to clear
- turn off lights; and other services in future too.
I have seen other blueprints on github which are intended for a similar end result but via different means.
For now, following the KISS principle, i’m using a single athom sensor and hardcoding the area master_bedroom
. I’d like for lights to turn on when lights turn a combination of binary_sensors
parameters (e.g. occupancy
OR no PIR but occupancy
OR mmWave but no occupancy
etc) occurs. Lights do turn on based on these conditions. But, its not getting past wait_template
. Here’s the code. I’m sure its something stupidly simple but i’ve now several hours per day for 2 weeks trying to debug it using debug logs and developer tools with no luck.
alias: test-motion
description: test-motion
trigger:
- platform: event
event_data: {}
event_type: state_changed
condition:
- condition: template
value_template: "{{ trigger.event.data.new_state is not none }}"
- condition: template
value_template: "{{ trigger.event.data.new_state.domain == 'binary_sensor' }}"
- condition: template
value_template: "{{ area_id(trigger.event.data.entity_id) == 'master_bedroom' }}"
- condition: template
value_template: "{{ states('sensor.sensor_presence_mbr_light_sensor') | float() <35.0 }}"
- condition: or
conditions:
- condition: and
conditions:
- condition: template
value_template: "{{ trigger.event.data.entity_id.split('_')[-1] == 'occupancy' }}"
- condition: template
value_template: "{{ states(trigger.event.data.entity_id) == 'on' }}"
- condition: and
conditions:
- condition: template
value_template: "{{ trigger.event.data.entity_id.split('_')[-2] == 'mmwave' }}"
- condition: template
value_template: "{{ states(trigger.event.data.entity_id) == 'on' }}"
- condition: and
conditions:
- condition: template
value_template: "{{ trigger.event.data.entity_id.split('_')[-2] == 'pir' }}"
- condition: template
value_template: "{{ states(trigger.event.data.entity_id) == 'on' }}"
- condition: and
conditions:
- condition: or
conditions:
- condition: template
value_template: "{{ trigger.event.data.entity_id.split('_')[-2] == 'pir' }}"
- condition: template
value_template: "{{ trigger.event.data.entity_id.split('_')[-2] == 'mmwave' }}"
- condition: and
conditions:
- condition: template
value_template: >-
{{ trigger.event.data.entity_id.split('_')[-1] == 'occupancy'
}}
- condition: template
value_template: "{{ states(trigger.event.data.entity_id) == 'on' }}"
action:
- alias: Set variables to be used later
variables:
pir_name: |
"{% if trigger.event.data.entity_id.split('_')[-1] == 'occupancy' %}
{{- trigger.event.data.entity_id[:-9] + 'pir_sensor' -}}
{% elif trigger.event.data.entity_id.split('_')[-2] == 'mmwave' %}
{{- trigger.event.data.entity_id[:-13] + 'pir_sensor' -}}
{% else %}
{{- trigger.event.data.entity_id -}}
{% endif %}"
mmwave_name: |
"{% if trigger.event.data.entity_id.split('_')[-1] == 'occupancy' %}
{{- trigger.event.data.entity_id[:-9] + 'mmwave_sensor' -}}
{% elif trigger.event.data.entity_id.split('_')[-2] == 'pir' %}
{{- trigger.event.data.entity_id[:-10] + 'mmwave_sensor' -}}
{% else %}
{{- trigger.event.data.entity_id }}
{% endif %}"
occ_name: |
"{% if trigger.event.data.entity_id.split('_')[-2] == 'pir' %}
{{- trigger.event.data.entity_id[:-10] + 'occupancy' -}}
{% elif trigger.event.data.entity_id.split('_')[-2] == 'mmwave' %}
{{- trigger.event.data.entity_id[:-13] + 'occupancy' -}}
{% else %}
{{- trigger.event.data.entity_id -}}
{% endif %}"
area_name: "{{ area_id('light.master_bedroom_main') }}"
- alias: Log variable values to see whether those are formulated correctly
service: system_log.write
data_template:
message: >
PIR entity = {{ pir_name }}, mmWave entity = {{ mmwave_name}},
occupancy entity = {{ occ_name }}, area = {{area_name }}
level: warning
- alias: Turn on lights and switches
service: light.turn_on
data:
area_id: "{{ area_name }}"
- alias: Wait for all sensors to clear
wait_template: |
"{{ is_state('binary_sensor.sensor_presence_mbr_occupancy', 'off') and
is_state('binary_sensor.sensor_presence_mbr_pir_sensor', 'off') and
is_state('binary_sensor.sensor_presence_mbr_mmwave_sensor', 'off')
}}"
- alias: Turn off lights and switches
service: light.turn_off
data:
area_id: "{{ area_name }}"
mode: restart
max_exceeded: silent
syslog output shows variables being created correctly e.g.
2023-10-31 08:09:45.269 WARNING (MainThread) [homeassistant.components.system_log.external] PIR entity = "binary_sensor.sensor_presence_mbr_pir_sensor", mmWave entity = "binary_sensor.sensor_presence_mbr_mmwave_sensor", occupancy entity = "binary_sensor.sensor_presence_mbr_occupancy", area = master_bedroom
After a certain time, sensor states change to off
. But, wait_template
doesn’t proceed to light.turn_off
.
Help please.