I am looking to setup an automation that will basically turn on my tv (with a google tv) attached and open an iptv app linked to my tv tuner (which will start a channel). The trigger is a hue motion sensor. I have the automation working but i only want this to run in the morning only when i go into my kitchen before work. Whats the best way to only allow this automation run once a day?
cheers
id: '1643570261869'
alias: play tv in the kitchen in the morning when sensor sees me
description: ''
trigger:
- type: motion
platform: device
device_id: 7a19b2c869a28edaf40ba6c0d0d1a925
entity_id: binary_sensor.kitchen_sensor_motion
domain: binary_sensor
condition:
- condition: time
after: '05:00:00'
before: '09:30:00'
action:
- service: remote.send_command
data:
device: kitchen tv
command: power
target:
device_id: 1276592bc45474738b30066b2516c4f0
- service: androidtv.adb_command
data:
command: am force-stop de.cyberdream.iptv.tv.player
target:
device_id: 1276592bc45474738b30066b2516c4f0
- delay:
hours: 0
minutes: 0
seconds: 0
milliseconds: 500
- service: media_player.select_source
data:
source: de.cyberdream.iptv.tv.player
target:
entity_id: media_player.kitchen_gtv
mode: restart
There will be a better answer than this, but I think you could set the mode to āsingleā and then add a 5 hour delay at the end. This would prevent the automation being able to run again until the next day.
Here is a condition I use to prevent an specific automation from running if itās been run in the last 9 hours, should be able to adapt to your use case.
- condition: template
value_template: >-
{% if as_timestamp(now()) | int -
as_timestamp(states.automation.YOUR_AUTOMATION.attributes.last_triggered)
| int > 32400 %} true {% else %} false {% endif %}
Easiest way Iāve found is to use the last triggered attribute from the automation in a condition template, like this:
- condition: template
# Only run if more than 6 hours (21,600 sec) since it last ran
value_template: '{{(as_timestamp(now()) - as_timestamp(state_attr("automation.<automation ID>", "last_triggered") | default(0)) | int > 21600 )}}'
Full example:
id: '1643570261869'
alias: play tv in the kitchen in the morning when sensor sees me
description: ''
trigger:
- type: motion
platform: device
device_id: 7a19b2c869a28edaf40ba6c0d0d1a925
entity_id: binary_sensor.kitchen_sensor_motion
domain: binary_sensor
condition:
- condition: time
after: '05:00:00'
before: '09:30:00'
- condition: template
# Only run if more than 6 hours (21,600 sec) since it last ran
value_template: '{{(as_timestamp(now()) - as_timestamp(state_attr("automation.1643570261869", "last_triggered") | default(0)) | int > 21600 )}}'
action:
You can select any duration (in seconds) that fits your needs. I just grabbed this example using 6 hours. Based on the first condition, the second condition should limit this automation to one time per day.
Hope this helps.
Basically, you want it to execute its actions only if the value of the automationās last_triggered is before 05:00 today. If the value is after 05:00 that means it has already triggered once today.
In fact it can be even simpler but itās designed to also work with an automation that has never been triggered (the value of last_triggered would be None).
This is the simpler version but I suggest using the more robust version that employs the default filter:
Have actually triggered the automation todayā¦ using the Run Action button doesnāt count.
There is only one actual entity ID. If youāre not sure what it is, look for it in the Developer Tools>States or Configuration>Entities menu. If you are going to enter it yourself in your template, you need to use whichever one is the actual entity ID.
You can instead use the this.entity_id variable as recommended by Taras above. Note that it does not need quotes because it is a variable, it will render the correct information in the correct format:
Hi
Iām doing it for the first time.
Somehow it doesnāt work for me. Iāve been trying for two days, but Iām still making a mistake:
Template warning: āas_timestampā got invalid input āNoneā when rendering template āā{{(as_timestamp(now()) - as_timestamp(state_attr(āautomation.1650378854495ā, ālast_triggeredā) | default(0)) | int > 21600 )}}āā but no default was specified. Currently āas_timestampā will return āNoneā, however this template will fail to render in Home Assistant core 2022.1
Template warning: āintā got invalid input āNoneā when rendering template āā{{(as_timestamp(now()) - as_timestamp(state_attr(āautomation.1650378854495ā, ālast_triggeredā) | default(0)) | int > 21600 )}}āā but no default was specified. Currently āintā will return ā0ā, however this template will fail to render in Home Assistant core 2022.1
Test source code:
alias: Teplota bojler malo vody 01 // ID:1650378854495
description: āā
trigger:
platform: time_pattern
seconds: /20
condition:
condition: time
after: ā08:00:00ā
before: ā22:00:00ā
weekday:
thanks for the piece of template. I have used it in my automation, however it doesnāt work for me and my log gets spammed up by repeating error messages which are as follows:
2022-04-20 09:38:30 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'automation' is undefined when rendering '{{ state_attr(automation.office_blinds_chair, 'last_triggered') < today_at('08:00') }}'
2022-04-20 09:38:30 WARNING (MainThread) [homeassistant.components.automation] Error evaluating condition in 'Office Blinds Chair':
In 'condition' (item 2 of 2):
In 'template' condition: UndefinedError: 'automation' is undefined
2022-04-20 09:38:30 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'automation' is undefined when rendering '{{ state_attr(automation.office_blinds_chair, 'last_triggered') < today_at('08:00') }}'
2022-04-20 09:38:30 WARNING (MainThread) [homeassistant.components.automation] Error evaluating condition in 'Office Blinds Chair':
In 'condition' (item 2 of 2):
In 'template' condition: UndefinedError: 'automation' is undefined
2022-04-20 09:38:31 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'automation' is undefined when rendering '{{ state_attr(automation.office_blinds_chair, 'last_triggered') < today_at('08:00') }}'
2022-04-20 09:38:31 WARNING (MainThread) [homeassistant.components.automation] Error evaluating condition in 'Office Blinds Chair':
In 'condition' (item 2 of 2):
In 'template' condition: UndefinedError: 'automation' is undefined
2022-04-20 09:38:33 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'automation' is undefined when rendering '{{ state_attr(automation.office_blinds_chair, 'last_triggered') < today_at('08:00') }}'
2022-04-20 09:38:33 WARNING (MainThread) [homeassistant.components.automation] Error evaluating condition in 'Office Blinds Chair':
In 'condition' (item 2 of 2):
In 'template' condition: UndefinedError: 'automation' is undefined
You didnāt actually use the template I posted. You made a critical change to it (you replaced the variable this.entity_id with automation.office_blinds_chair) and thatās what is causing it to fail.
Either restore this.entity_id or wrap automation.office_blinds_chair in single quotes like this: 'automation.office_blinds_chair'