Hi, sometimes my motion sensor turns on or off my light, but I would prefer it not to. So, I created a blueprint that turns off my motion sensor automation for an hour when I manually change the light after a specific automation trigger. However, it’s not perfect. Sometimes it disables automation even if I did not manually change the status of the light. I know I could use a timer too, but I don’t want to have to create a timer for every automation.
Here is my code. Any advice would be appreciated.
blueprint:
name: Disable Automation After Light Change
description: Disable automation for a period after a light change.
domain: automation
input:
automation_entity:
name: Automation Entity
description: The automation entity to control.
selector:
entity:
domain: automation
light_entity:
name: Light Entity
description: The light entity to monitor for changes.
selector:
entity:
domain:
- light
- switch
- group
disable_duration:
name: Disable Duration
description: Duration to disable the automation after the light change (in minutes).
default: 5
selector:
number:
min: 1
max: 60
unit_of_measurement: minutes
trigger:
- platform: state
entity_id: !input "light_entity"
variables:
disable_duration: !input "disable_duration"
automation_entity: !input "automation_entity"
condition:
- condition: or
conditions:
- condition: and
conditions:
- condition: template
value_template: "{{ trigger.to_state.context.parent_id == none }}"
- condition: template
value_template: "{{ trigger.to_state.context.user_id == none }}"
- condition: template
value_template: "{{ trigger.to_state.context.id != none }}"
- condition: template
value_template: >-
{{ state_attr(automation_entity, 'last_triggered') is defined and now() - state_attr(automation_entity, 'last_triggered') < timedelta(minutes=disable_duration) }}
- condition: or
conditions:
- condition: and
conditions:
- condition: template
value_template: "{{ trigger.to_state.context.parent_id == none }}"
- condition: template
value_template: "{{ trigger.to_state.context.user_id != none }}"
- condition: template
value_template: "{{ trigger.to_state.context.id != none }}"
- condition: template
value_template: >-
{{ state_attr(automation_entity, 'last_triggered') is defined and now() - state_attr(automation_entity, 'last_triggered') < timedelta(minutes=disable_duration) }}
action:
- service: automation.turn_off
target:
entity_id: !input "automation_entity"
data:
stop_actions: true
- delay: "00:{{ '{:02}'.format(disable_duration) }}:00"
- service: automation.turn_on
target:
entity_id: !input "automation_entity"
mode: restart
For info this is an exemple of an automation that I “kill”
alias: Mudroom - Light - Motion
description: ""
trigger:
- platform: state
entity_id:
- binary_sensor.presence_sensor_fp2_46ca_presence_sensor_3
to: null
condition:
- condition: state
entity_id: group.couple
state: home
action:
- choose:
- conditions:
- condition: state
entity_id: binary_sensor.presence_sensor_fp2_46ca_presence_sensor_3
state: "off"
for:
hours: 0
minutes: 5
seconds: 0
sequence:
- service: light.turn_off
target:
entity_id: light.60357018dc4f2261b9b1
data: {}
- conditions:
- condition: state
entity_id: binary_sensor.presence_sensor_fp2_46ca_presence_sensor_3
state: "on"
for:
hours: 0
minutes: 0
seconds: 0
sequence:
- service: light.turn_on
target:
entity_id:
- light.60357018dc4f2261b9b1
data: {}
mode: single
Finally, here’s a reference for how I manage to determine if an entity is triggered manually or by an automation:
https://community.home-assistant.io/t/work-with-triggered-by-in-automations/400352/9