Run automation only once a day

Hello,

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

Any idea what should I do ?

Thanks!

Use the following Template Condition:

{{ this.attributes.last_triggered < today_at() }}

Example

1 Like

I tried the condition but it’s not working for me, maybe i’m doing something wrong

I created a new simpler automation just for testing (don’t pay attention to what it does)

alias: Nouvelle automatisation34214
description: ""
trigger:
  - platform: device
    type: changed_states
    device_id: 32abaaa538d641a31534b3a3fa15cb3d
    entity_id: light.cuisine
    domain: light
    id: "1"
condition:
  - condition: template
    value_template: "{{ this.attributes.last_triggered < today_at() }}"
action:
  - if:
      - condition: trigger
        id: "1"
    then:
      - type: turn_on
        device_id: 1f89fc8902a47df3482a497e09595c09
        entity_id: light.canape
        domain: light
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() }}

Woww thanks!! It works like a charm

:smiley:

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.

{{ this.attributes.last_triggered | default(as_datetime(0)) < today_at() }}
2 Likes

Hey mate, I was wondering if I could use this rule you gave me but not for the entire automation

{{ this.attributes.last_triggered is none or this.attributes.last_triggered < today_at() }}

I have multiple “IF” in actions and would like to apply the rule only for the one in red :

Using your code in a similar automation:

alias: Coffee Maker On
description: ""
trigger:
  - type: motion
    platform: device
    device_id: cefb551b7880f6d6544b2e6eb2915b70
    entity_id: binary_sensor.master_bathroom_ceiling_motion_sensor_occupancy
    domain: binary_sensor
  - type: motion
    platform: device
    device_id: 14aeb7b6b44d5c1f73e22747e1497a08
    entity_id: binary_sensor.master_bathroom_floor_motion_sensor_occupancy
    domain: binary_sensor
  - type: motion
    platform: device
    device_id: ec26722c66df29d24454276279a73a9d
    entity_id: binary_sensor.master_closet_motion_sensor_occupancy
    domain: binary_sensor
condition:
  - condition: time
    after: "06:30:00"
    before: "10:00:00"
    weekday:
      - mon
      - tue
      - wed
      - thu
      - fri
  - condition: template
    value_template: "{{ this.attributes.last_triggered | default(as_datetime(0)) < today_at() }}"
action:
  - type: turn_on
    device_id: d8ca29c667575a473ba863a51f31ae5d
    entity_id: switch.coffee_kp2
    domain: switch
  - delay:
      hours: 0
      minutes: 25
      seconds: 0
      milliseconds: 0
  - type: turn_off
    device_id: d8ca29c667575a473ba863a51f31ae5d
    entity_id: switch.coffee_kp2
    domain: switch
mode: single

when I “test” the template in the YAML editor, I get an error: template condition: UndefinedError: ‘this’ is undefined.

What have I done wrong - your help pls.
Thanks.

You can’t use that for testing a template containing the this or trigger objects.

ok - so the error is false - as I can not use the “test” routine on this element.

do I have the coding correct? The logic should work - yes?

Thanks!

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.

Hmmm - very good point - thank you.

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?

Unfortunately I don’t have free time available to help you code that. Perhaps someone else can help you.

Hello,

I’m trying to implement the following condition but it does not work. ‘Condition did not pas’ with grey background when I use this:

{{ this.attributes.last_triggered < today_at() }}

or this:

{{ this.attributes.last_triggered is none or this.attributes.last_triggered < today_at() }}

When I write this it works:

{{state_attr('automation.presence_leaving_home_zone_controlled', 'last_triggered')< today_at()}}

Why can’t I use “This” in a condition?

kind regards,
Nick

If you’re trying to test those conditions in the Automation Editor, it won’t work because it’s incapable of evaluating the this object.

The use of this is valid but it can’t be tested like that. Not everything can be properly tested within the Automation Editor.

ok,
I thought it didn’t work when you used it at the ‘then’ section.

But now I understand.

I’ll leave it like this and see if it runs tomorrow.

Thank you for you quick reply.
Nick

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.

For more information:
Troubleshooting your automation
Automation trigger variables

1 Like

Noted!

Last question, is there a way to overwrite the attributes of the ‘this’ object?

I mean, is it possible to write a template action like this?

{{this.attributes.last_triggered = none}}

kind regards,
Nick

No.

You can’t use a template to assign a value to an entity’s state or its attributes.

1 Like