I have a simple automation which turns on the radio when I turn on the kitchen light between 8 AM to 9.30AM
Is it possible to run this action only once a time during this time frame (8H-9H30) ?
For example
-if I turn on the light a 8.15AM, it turns on the radio
-but if I turn off the light at 8.30AM it doesn’t run the action again as it knows it has been ran at 8.15AM
I tried this :
alias: test
description: ""
trigger:
- platform: device
type: changed_states
device_id: 32abaaa538d641a31534b3a3fa15cb3d
entity_id: light.cuisine
domain: light
id: "1"
alias: KITCHEN LIGHT ON OR OFF
condition: []
action:
- if:
- condition: trigger
id: "1"
- condition: time
after: "08:00:00"
before: "09:30:00"
weekday:
- mon
- tue
- wed
- thu
- fri
- sat
- sun
- condition: template
value_template: |-
{{ (utcnow() | as_timestamp -
state_attr('automation.test', 'last_triggered') |
as_timestamp) | default(0) | int < 60}}
then:
- service: media_player.volume_set
data:
volume_level: 0.1
target:
device_id: e3a6f4ed1304a9353c32c8d4c52128f8
- service: media_player.play_media
target:
entity_id: media_player.echo_dot_salle_de_bain
data:
media_content_id: RTL2
media_content_type: TUNEIN
metadata: {}
alias: IF LIGHT ON OR OFF / RADIO ON
mode: single
The condition only passes if the last time the action was run is earlier than the start of today. If the automation hasn’t been run at all, it’ll have None for its last_triggered attribute and the test will fail.
Use this template instead:
{{ this.attributes.last_triggered is none or this.attributes.last_triggered < today_at() }}
It was my impression that your automation was not new and had already triggered at least once. In that situation, it has a last_triggered attribute and there’s no need for the extra code included in troon’s example.
However, it seems my assumption was incorrect and you had created a brand new automation that was untriggered. In that situation, it has no last_triggered attribute and the example I posted would fail. All it needed was to add a default value.
Be advised that if Home Assistant is restarted during the automation’s 25-minute delay, the automation will be cancelled and so it will never execute the last action which turns off the kettle.
So, my thinking leads me that there should be an additional condition that states if Home Assistant restarted - from the time this routine started up to the end of the time, stop the routine. It would seem this condition would be the last condition.
This novice would appreciate - how to code that into the routine?
For future reference, there are some things that are only defined when the automation is actually triggered. For example, this is one of them and so is trigger. So if your automation references trigger.to_state.state in a condition or action, it will only work if the automation was triggered.
If you use an automation’s Run command, that only executes the automation’s actions. It doesn’t trigger the automation so any references it may have to trigger will be undefined.