I have been searching high and low for a way to get this automation working properly. Here’s the automation, I walk into my bedroom, a zigbee motion sensor will trigger between after 19:00 and before 23:00. From there it will turn off the downstairs lights, and turn on TV and bedroom lights, the lights are on a 5 minute delay and then shut off. However everything works with the exception of if the sensor detects anymore motion within that set timeframe it will trigger the routine again, turning on all the lights and TV. I am wanting a way to tell HA that if this routine has already been performed for the day, to not trigger it again. I have tried using a timestamp template, but it breaks the automation everytime. It may be not written correctly, if someone would be able to look this over and tell me what I’m missing.
alias: Bedtime Routine 2
description: ""
trigger:
- type: motion
platform: device
device_id: ece251e15ca50919578152d69cd57207
entity_id: binary_sensor.bedroom_sensor_iaszone
domain: binary_sensor
condition:
- condition: time
after: "19:00:00"
before: "23:00:00"
weekday:
- sun
- mon
- tue
- wed
- thu
- fri
- sat
- condition: template
value_template: |2-
{{as_timestamp(state_attr('automation.1677620726394','last_triggered'))|timestamp_custom('%-d') != as_timestamp(now())|timestamp_custom('%-d')}}
enabled: false
action:
- service: notify.alexa_media_scott_s_bedroom
data:
message: starting bedtime routine, turning on nightlights and arming system.
title: Bedtime Routine 1
- service: media_player.turn_on
data: {}
target:
device_id: c4aad01296e3f597bf552199b5da2e6e
- delay:
hours: 0
minutes: 0
seconds: 10
milliseconds: 0
- service: media_player.play_media
target:
entity_id: media_player.32_tcl_roku_tv
data:
media_content_id: "13535"
media_content_type: app
metadata:
title: Plex - Free Movies & TV
thumbnail: http://10.0.20.109:8060/query/icon/13535
media_class: app
children_media_class: null
navigateIds:
- {}
- media_content_type: apps
media_content_id: ""
- type: turn_off
device_id: 82cf6ec14229fc34929fc5d0e786af47
entity_id: light.cellar_light_1
domain: light
- type: turn_off
device_id: a2d704efb13d03c88d77fc5ae4bcba23
entity_id: light.cellar_light_2
domain: light
- type: turn_off
device_id: 8380d388602dc1df92ef9f52de992b8f
entity_id: light.cellar_light_3
domain: light
- type: turn_off
device_id: 3be37dbfe8e0435669d9c213b6a0ae15
entity_id: light.studio_light_1
domain: light
- type: turn_off
device_id: 0b8970f7f818d55a8715acfa7a6e10aa
entity_id: light.studio_light_2
domain: light
- type: turn_off
device_id: 9d68b809e083e5faa303841f2d6c2197
entity_id: light.cellar_stairs
domain: light
- type: turn_off
device_id: e399345b95646c92fb635938187ef0b0
entity_id: light.office_light
domain: light
- type: turn_off
device_id: afc42a00a7c06fe7446e27ca8bb6adca
entity_id: switch.workbench_light_switch
domain: switch
- service: media_player.turn_off
data: {}
target:
device_id: 3b6b19371664357a29331ff7cbacc160
- type: turn_on
device_id: 849841a7b77c8888fe3e1590915b2f5d
entity_id: light.bed_leds_light
domain: light
- delay:
hours: 0
minutes: 5
seconds: 0
milliseconds: 0
- type: turn_off
device_id: 849841a7b77c8888fe3e1590915b2f5d
entity_id: light.bed_leds_light
domain: light
- service: notify.alexa_media_scott_s_bedroom
data:
message: bedtime routine completed, pleasant dreams rice family.
title: Bedtime Routine 2
mode: single
itnassol
(Andrew)
March 1, 2023, 11:32am
2
I have sorted this on my system by creating a toggle helper, the toggle is off by default, set the automation to only run if the toggle is off, then as part of the automation tun the toggle on, it will then not run again until the toggle is off. I then have a separate automation that at 2am resets all my toggle helpers to off.
So it would look like this…
alias: Bedtime Routine 2
description: ""
trigger:
- type: motion
platform: device
device_id: ece251e15ca50919578152d69cd57207
entity_id: binary_sensor.bedroom_sensor_iaszone
domain: binary_sensor
condition:
- condition: time
after: "19:00:00"
before: "23:00:00"
weekday:
- sun
- mon
- tue
- wed
- thu
- fri
- sat
- condition: template
value_template: |2-
{{as_timestamp(state_attr('automation.1677620726394','last_triggered'))|timestamp_custom('%-d') != as_timestamp(now())|timestamp_custom('%-d')}}
enabled: false
- condition: and
conditions:
- condition: state
entity_id: input_boolean.HERES YOUR INPUT BOOLEN OR TOGGLE HELPER
state: "off"
action:
- service: notify.alexa_media_scott_s_bedroom
data:
message: starting bedtime routine, turning on nightlights and arming system.
title: Bedtime Routine 1
- service: media_player.turn_on
data: {}
target:
device_id: c4aad01296e3f597bf552199b5da2e6e
- delay:
hours: 0
minutes: 0
seconds: 10
milliseconds: 0
- service: media_player.play_media
target:
entity_id: media_player.32_tcl_roku_tv
data:
media_content_id: "13535"
media_content_type: app
metadata:
title: Plex - Free Movies & TV
thumbnail: http://10.0.20.109:8060/query/icon/13535
media_class: app
children_media_class: null
navigateIds:
- {}
- media_content_type: apps
media_content_id: ""
- type: turn_off
device_id: 82cf6ec14229fc34929fc5d0e786af47
entity_id: light.cellar_light_1
domain: light
- type: turn_off
device_id: a2d704efb13d03c88d77fc5ae4bcba23
entity_id: light.cellar_light_2
domain: light
- type: turn_off
device_id: 8380d388602dc1df92ef9f52de992b8f
entity_id: light.cellar_light_3
domain: light
- type: turn_off
device_id: 3be37dbfe8e0435669d9c213b6a0ae15
entity_id: light.studio_light_1
domain: light
- type: turn_off
device_id: 0b8970f7f818d55a8715acfa7a6e10aa
entity_id: light.studio_light_2
domain: light
- type: turn_off
device_id: 9d68b809e083e5faa303841f2d6c2197
entity_id: light.cellar_stairs
domain: light
- type: turn_off
device_id: e399345b95646c92fb635938187ef0b0
entity_id: light.office_light
domain: light
- type: turn_off
device_id: afc42a00a7c06fe7446e27ca8bb6adca
entity_id: switch.workbench_light_switch
domain: switch
- service: media_player.turn_off
data: {}
target:
device_id: 3b6b19371664357a29331ff7cbacc160
- type: turn_on
device_id: 849841a7b77c8888fe3e1590915b2f5d
entity_id: light.bed_leds_light
domain: light
- delay:
hours: 0
minutes: 5
seconds: 0
milliseconds: 0
- type: turn_off
device_id: 849841a7b77c8888fe3e1590915b2f5d
entity_id: light.bed_leds_light
domain: light
- service: notify.alexa_media_scott_s_bedroom
data:
message: bedtime routine completed, pleasant dreams rice family.
title: Bedtime Routine 2
mode: single
1 Like
123
(Taras)
March 1, 2023, 2:14pm
3
Simply replace this:
- condition: template
value_template: |2-
{{as_timestamp(state_attr('automation.1677620726394','last_triggered'))|timestamp_custom('%-d') != as_timestamp(now())|timestamp_custom('%-d')}}
enabled: false
with this:
- condition: template
value_template: "{{ this.attributes.last_triggered.date() != now().date() }}"
It’s a common technique to restrict an automation to execute its actions just once a day.
1 Like
Excellent! Most helpful, thank you again for the prompt solution. I will attempt the routine tonight at bedtime and hopefully everything performs without a hitch. =)
1mfaasj
(Bowa)
January 8, 2024, 8:14pm
5
123:
last_triggered
Hm it feels like I’m missing something. I’m trying the same thing but the automation fails with this error:
‘This’ is undefined
alias: Knmi
description: ""
trigger:
- platform: state
entity_id:
- sensor.knmi_code
to: red
condition:
- condition: template
value_template: "{{ this.attributes.last_triggered.date() != now().date() }}"
action:
- service: notify.john
data:
title: KNMI warning
message: "\"{{ states('sensor.knmi_warning') }}\" "
mode: single
Are you willing to help?
123
(Taras)
January 8, 2024, 11:52pm
6
1mfaasj:
This’ is undefined
The this
variable is defined only when the automation is triggered. Did you test the automation by waiting for the value of sensor.knmi_code
to change to red
?
Testing your automation
1mfaasj
(Bowa)
January 9, 2024, 12:41pm
7
It was a new automation. after I triggered it manually it worked.
123
(Taras)
January 9, 2024, 1:32pm
8
If you used either its Run command or via automation.trigger
(whose skip_condition
option is true
by default) to “trigger the automation manually” then you never exercised its Template Condition.
Triggering an automation manually only executes its action
. It doesn’t exercise the automation’s trigger
or its condition
.
Therefore it stands to reason that there was no error message when you triggered it manually because it did not exercise the Template Condition containing this
.
1mfaasj
(Bowa)
January 15, 2024, 6:49am
9
Hmm its still executing more than once a day?! I thought the condition was preventing it?
alias: Gps zone
description: ""
trigger:
- platform: zone
entity_id: person.admin
zone: zone.myzone
event: enter
condition:
- condition: template
value_template: >-
{{ (this.last_triggered | default(now() - timedelta(days=1), 1)).date !=
now().date() }}
action:
- service: google.create_event
data:
in:
days: 0
summary: zone
location: mylocation
target:
entity_id: calendar.mymail_gmail_com
mode: single
123
(Taras)
January 15, 2024, 12:29pm
10
Not that condition. Try using the one shown in the example posted 10 months ago.