I’m having trouble with an automation in Home Assistant and I hope you can help me figure out what’s wrong. Here’s what I’m trying to achieve:
Between a specific time range (let’s say 18:00 to 23:00), I want certain lights to turn on when a presence sensor detects that I’ve arrived home.
This should only happen once per day. If I turn off the lights and pass by the sensor again, it shouldn’t trigger the lights until the next day.
I have two YAML automations set up for this, but they’re not working as expected. When I force the action, the lights turn on, but it doesn’t work automatically when the specified time arrives.
Here are my current YAML automations:
1. alias: Encender LUZ SALON movimiento
2. description: >
3. Al detectar movimiento entre las 8:45 p.m. y la 1:00 a.m., enciende las luces
4. del salón, solo una vez al día.
5. trigger:
6. - type: motion
7. platform: device
8. device_id: 3bbac7334aa617dd6457474922a10a00
9. entity_id: 42d70ee7b5e8e903e88d10cf1fedd241
10. domain: binary_sensor
11. for:
12. hours: 0
13. minutes: 0
14. seconds: 0
15. condition:
16. - condition: and
17. conditions:
18. - condition: time
19. after: "20:28:00"
20. before: "23:59:59"
21. - condition: state
22. entity_id: input_boolean.time_interval_toggle_helper
23. state: "on"
24. action:
25. - service: light.turn_on
26. data: {}
27. target:
28. device_id:
29. - 5912847c2140a8f0c2173e32b2d18381
30. - c32c52537c47a619773d13fcbe3b5d2b
31. - service: input_boolean.turn_off
32. target:
33. entity_id: input_boolean.time_interval_toggle_helper
34. mode: single
and boleean:
1. alias: Reset Time Interval Toggle Helper
2. description: "Resets the Time Interval Toggle Helper input boolean every day at 7:00 a.m."
3. trigger:
4. - platform: time
5. at: "07:00:00"
6. action:
7. - service: input_boolean.turn_on
8. target:
9. entity_id: input_boolean.time_interval_toggle_helper
10. mode: single
Please don’t post code with line numbers like that.
Try this:
I’ve replaced your device trigger with an entity state trigger, which are better. You need to fill in the entity ID of your motion detector.
I’ve removed the references to your toggle helper and added a template condition that will only pass if the automation hasn’t been executed in the last ten hours.
I’ve replaced the service action with a new action action (2024.8 change), and I’d recommend updating the target device_id to entity_id instead, listing your two light.??? entity IDs.
The times in your condition do not match the times in your description. I’ve left the times as per your condition.
alias: Encender LUZ SALON movimiento
description: >
Al detectar movimiento entre las 8:45 p.m. y la 1:00 a.m., enciende las luces
del salón, solo una vez al día.
trigger:
- platform: state
entity_id: binary_sensor.YOUR_MOTION_DETECTOR
to: 'on'
condition:
- condition: time
after: "20:28:00"
before: "23:59:59"
- condition: template
value_template: |-
{{ now()|as_timestamp -
state_attr(this.entity_id, 'last_triggered')|as_timestamp(0)
> 10 * 3600 }}
action:
- action: light.turn_on
target:
device_id:
- 5912847c2140a8f0c2173e32b2d18381
- c32c52537c47a619773d13fcbe3b5d2b
mode: single
First of all, thank you for your comments and the help you are giving me. I have followed the steps you indicated, but unfortunately it has not worked for me. I did not think that something so simple could end up costing me so much… What could be wrong with me?
alias: Encender LUZ SALON movimiento
description: >
Al detectar movimiento entre las 8:45 p.m. y la 1:00 a.m., enciende las luces
del salón, solo una vez al día.
trigger:
- platform: state
entity_id:
- binary_sensor.occupancysensor_ocupacion
to: "on"
condition:
- condition: time
after: "20:28:00"
before: "23:59:59"
- condition: template
value_template: |-
{{ now()|as_timestamp -
state_attr(this.entity_id, 'last_triggered')|as_timestamp(0)
> 10 * 3600 }}
action:
- action: light.turn_on
target:
device_id:
- 5912847c2140a8f0c2173e32b2d18381
- c32c52537c47a619773d13fcbe3b5d2b
data: {}
mode: single
I have re-created an automation from scratch but without time to see if it works. And yes, it does indeed work correctly. Do you think I can work on this one that is clean and that we know for sure works?
alias: Llegar a Casa
description: Luz al llegar a casa
trigger:
- type: occupied
platform: device
device_id: 719cedc3771fd55ec0e8dad424c414e2
entity_id: 3a9718042b0a693634932b2637141592
domain: binary_sensor
condition: []
action:
- action: light.turn_on
metadata: {}
data: {}
target:
device_id:
- 5912847c2140a8f0c2173e32b2d18381
- c32c52537c47a619773d13fcbe3b5d2b
mode: single
So I understand that if I take the new YAML code it should look like this: Correct?
alias: Llegar a Casa
description: Luz al llegar a casa
trigger:
- type: occupied
platform: device
device_id: 719cedc3771fd55ec0e8dad424c414e2
entity_id: 3a9718042b0a693634932b2637141592
domain: binary_sensor
condition:
- condition: time
after: "20:28:00"
before: "23:59:00"
- condition: template
value_template: |-
{{ now()|as_timestamp -
state_attr(this.entity_id, 'last_triggered')|as_timestamp(0)
> 10 * 3600 }}
action:
- action: light.turn_on
metadata: {}
data: {}
target:
device_id:
- 5912847c2140a8f0c2173e32b2d18381
- c32c52537c47a619773d13fcbe3b5d2b
mode: single
I want to tell you that it finally works. Thank you for the help you have given me. I am very grateful that you have shared your knowledge with me. Greetings. (SOLVED)