Hi, I have an automation that looks at the lux level in the room and once it hits the threshold, it turns on lights. To stop it from running over and over again (when the lights are turned off in the evening near where the sensor is,) I was thinking of using a virtual device that has to be “on” in order for the automation to run. At the end of the automation, I was going to put a command that turns that virtual device off, thus stopping the automation from running until the next day after it’s light out (I have an “if” part that says to only run after 12:00 PM.) Rudimentary, but it’s worked on the old setup I had on another system. If anyone can recommend the easier way to do this, I’d appreciate it.
You can use a condition that says the light has to have been off for some time duration, like 12 hours. I think that would work for what you’re trying to do.
Just add a Template Condition that confirms the automation hasn’t yet triggered today.
conditions:
- condition: template
value_template: "{{ state_attr(this.entity_id, 'last_triggered') < today_at() }}"
If it has already triggered today, the condition prevents the automation from executing its actions.
If you want it to run only after a specific time (once a day), specify a time in today_at ()
.
value_template: "{{ state_attr(this.entity_id, 'last_triggered') < today_at('12:00') }}"
I am pretty new to the automations using using Yaml. Can i just paste this within the code I already have?
The tripple dot menu at top right, while editing the automation, gives you an option to edit in yaml. He’s an example of what present in an automation I have:
alias: STB off with tv
description: ""
triggers:
- device_id: 20e515fe4abce66651e64075854add39
domain: media_player
entity_id: db06a491e20bd5e3287b93a17c86174f
type: turned_off
trigger: device
conditions: []
actions:
- type: turn_off
device_id: 7ed95f4519020358358f9ad6057e981d
entity_id: a9928ffd4400db9480d593e47ccb9b00
domain: switch
mode: single
You would just cut and past the condition line provided previously to replace the “conditions: ” line in your yaml automation.
Thank you, I appreciate it. Here’s my code. Just wanted to make sure the way I have it will do what I need. If the lux level is below 24, it’s at least 1:00 PM and the the automation hasn’t run today, then turn on the lights…One other question: I tested this this morning by hitting ‘run’ - will this stop the automation from running tonight since it has already run today? Is there somewhere I can see whether the ‘today’ variable is yes or no?
alias: Lux
description: ""
triggers:
- type: illuminance
device_id: 8d09d65c7a7695745233c627c327d99d
entity_id: 42e2780368e94f24bbef99a93ed9c7d5
domain: sensor
trigger: device
below: 24
conditions:
- condition: time
after: "13:00:00"
weekday:
- sun
- mon
- tue
- wed
- thu
- fri
- sat
- condition: template
value_template: "{{ state_attr(this.entity_id, 'last_triggered') < today_at() }}"
actions:
- type: turn_on
device_id: 804707fe4229043c206e8c90aa1388b2
entity_id: 06867a36df0f06ada2f5bec287a0059e
domain: light
brightness_pct: 10
- type: turn_on
device_id: 2d47b5d5d74387f9854fa49c9c9114c9
entity_id: 2eae9aac8ec358a859b81d3ffe97a5d2
domain: light
brightness_pct: 84
- type: turn_on
device_id: a0304bca9c109ace84dfccffd654ea55
entity_id: 9ff26b63fcea00a8ec1af7b6e9630cb3
domain: light
brightness_pct: 30
- type: turn_on
device_id: bc9b9ef7284e75ad03782f637cac95bf
entity_id: 2fe011e6ed1277fe02eebf03ed6a16fa
domain: light
- type: turn_on
device_id: aa97a105fd6cea7b78c8593de36e7026
entity_id: dcbb9aac416deba556bb7cc80db5da8b
domain: light
brightness_pct: 100
- type: turn_on
device_id: 12d2ada6111d40f1fa9eb157ea0adc54
entity_id: d18855d40bf9ed01de613decd7eb2acd
domain: light
- type: turn_on
device_id: 33f62eae58d357edfd95ed8416cfc30a
entity_id: ee0928883be95296312b1ba28cd758fe
domain: light
mode: single
You can consolidate the Time Condition and Template Condition into a single Template Condition. Simply follow the instructions I provided in my previous post and supply today_at()
with 13:00
conditions:
- condition: template
value_template: "{{ state_attr(this.entity_id, 'last_triggered') < today_at('13:00') }}"
Every automation has a last_triggered
attribute that shows the date and time when the automation was triggered and executed its actions. The attribute is displayed in Developer Tools → States in the Attributes column (rightmost column; visible on a widescreen like a laptop or tablet).
It’s also displayed in Settings → Automations either under or beside the automation’s name. That’s how you can check the last time the automation was triggered and executed its actions.
Thanks for the explanation. Here’s what it says for last triggered:
last_triggered: 2024-12-13T13:39:12.600907+00:00
mode: single
current: 0
By the way, I ran the below code and the automation triggered, even thought it’s before 1:00 PM. It continues to trigger, so there’s something I am missing from your explanation…
If I’m understanding this right, the below code will trigger if a) lux under 24, b)1:00 PM or later c)it won’t re-run the automation later in the evening if the lights go out and the lux sensor goes below 24 d)the automation can’t run until the next day at 1:00 PM
-Please let me know if I’m misinterpreting any of the above or if my code doesn’t represent the above:
alias: Lux
description: ""
triggers:
- type: illuminance
device_id: 8d09d65c7a7695745233c627c327d99d
entity_id: 42e2780368e94f24bbef99a93ed9c7d5
domain: sensor
trigger: device
below: 24
conditions:
- condition: template
value_template: "{{ state_attr(this.entity_id, 'last_triggered') < today_at('13:00') }}"
actions:
- type: turn_on
device_id: 804707fe4229043c206e8c90aa1388b2
entity_id: 06867a36df0f06ada2f5bec287a0059e
domain: light
brightness_pct: 10
- type: turn_on
device_id: 2d47b5d5d74387f9854fa49c9c9114c9
entity_id: 2eae9aac8ec358a859b81d3ffe97a5d2
domain: light
brightness_pct: 84
- type: turn_on
device_id: a0304bca9c109ace84dfccffd654ea55
entity_id: 9ff26b63fcea00a8ec1af7b6e9630cb3
domain: light
brightness_pct: 30
- type: turn_on
device_id: bc9b9ef7284e75ad03782f637cac95bf
entity_id: 2fe011e6ed1277fe02eebf03ed6a16fa
domain: light
- type: turn_on
device_id: aa97a105fd6cea7b78c8593de36e7026
entity_id: dcbb9aac416deba556bb7cc80db5da8b
domain: light
brightness_pct: 100
- type: turn_on
device_id: 12d2ada6111d40f1fa9eb157ea0adc54
entity_id: d18855d40bf9ed01de613decd7eb2acd
domain: light
- type: turn_on
device_id: 33f62eae58d357edfd95ed8416cfc30a
entity_id: ee0928883be95296312b1ba28cd758fe
domain: light
mode: single
How did you “run” it? In other words, how did you test the automation?
-
Did you darken the room, or cover the sensor, so that the illuminance sensor’s value decreased from above 24 to below 24? That will serve to trigger the automation and cause it to evaluate its Template Condition.
-
Did you execute the automation’s Run command? That skips the automation’s trigger and condition and simply executes its actions.
Yes, exactly, I covered it. What I ended up doing, which I know is not probably the easiest way to do it, is used the input boolean method. The automation checks to see if the switch is on and if it isn’t, it can’t continue with the flow. I have a separate automation that turns on this switch in the late morning. At the end of the automation, it turns off that switch so it (the automation) can’t run until the next day…
I don’t know what you’re doing wrong but you’re doing something wrong.
The reason why I say that is because what I suggested above is what I (and others) have suggested to countless others who had the same need to execute an automation just once a day. It worked for them; no additional helper required.