Only allow automation to trigger once a day?

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.

4 Likes

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 %}
5 Likes

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.

4 Likes

Thanks folks for the replies, I will give them a try.

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.

The condition to achieve this is simple:

condition:
  - condition: time
    after: '05:00:00'
    before: '09:30:00'
  - "{{ state_attr(this.entity_id, 'last_triggered') | default(today_at(), true) < today_at('05:00') }}"

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:

condition:
  - condition: time
    after: '05:00:00'
    before: '09:30:00'
  - "{{ state_attr(this.entity_id, 'last_triggered') < today_at('05:00') }}"
4 Likes

@ 123Taras
Thanks for simplified example. I will begin using this version from this point forward,

I am trying to achieve the same as OP, could someone help me troubleshoot?

I keep getting the following error message when I try to test the template condition

“template value should be a string for dictionary value @ data[‘value_template’]. Got None”

my code :

alias: HP Theme
description: ''
trigger:
  - type: motion
    platform: device
    device_id: e913ed090d59830bf80f9dc10181960a
    entity_id: binary_sensor.lumi_lumi_sensor_motion_aq2_ias_zone
    domain: binary_sensor
condition:
  - condition: time
    before: '10:00:00'
    after: '08:00:00'
    weekday:
      - sat
  - condition: template
    value_template: >-
      {{ state_attr(automation.1649509086176, 'last_triggered') |
      default(today_at(), true) < today_at('08:00') }}
action:
  - service: notify.notifier_pushover
    data:
      message: test
      title: abc test
mode: single

In your template, you need to put single quotes around automation.1649509086176.

Thank you for replying!

I have placed it in single quotes, however the error still remains

condition: template
value_template: >-
  {{ state_attr('automation.1649509086176','last_triggered') |
  default(today_at(), true) < today_at('010:00') }}

two questions:

  1. when using the new test condition button released in 2022.4, should it come back with a negative/positive result?

  2. are the identity ID’s interchangeable?
    as in can I use either the numerical value or the naming?

automation.hp_theme

vs

automation.1649509086176

You have a typo in your time… '010:00'

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:

condition: template
value_template: >-
  {{ state_attr(this.entity_id, 'last_triggered') |
  default(today_at(), true) < today_at('10:00') }}
1 Like

thank you! I have it working!

I was focused on testing via the buttons and not actually triggering the motion sensor.

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:
    • mon
    • tue
    • wed
    • thu
    • fri
    • sat
    • sun
  • condition: numeric_state
    entity_id: sensor.bojler
    below: ‘71.6’
  • condition: template
    value_template: >-
    {{(as_timestamp(now()) - as_timestamp(state_attr(
    ‘automation.1650378854495’, ‘last_triggered’) | default(0)) | int > 21600 )}}

    action:
  • service: tts.voicerss_say
    data:
    entity_id: media_player.obyvaci_pokoj_speaker
    message: >-
    Voda v bojleru má nedostatečnou teplotu {{states(‘sensor.bojler’)}}
    stupňů celsia. Šetřete teplou vodou!!
    mode: single

where is the problem?

I did this in developer → template

{{ (utcnow() | as_timestamp - state_attr('automation.control_study_lights', 'last_triggered') | as_timestamp ) | default(0) | int }}

it works. I would use utcnow() as all triggers are UTC. Notice the parens around the subtraction.

1 Like

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

Here is my automation:

- id: office_blinds_chair
  alias: Office Blinds Chair
  initial_state: true
  trigger:
  - platform: state
    entity_id: binary_sensor.office_chair_contact
    to: 'off'
  condition:
    - condition: time
      after: '08:00:00'
      before: '10:30:00'
    - "{{ state_attr(automation.office_blinds_chair, 'last_triggered') < today_at('08:00') }}"
  action:
  - service: cover.set_cover_position
    data:
      position: 100
    target:
      entity_id: cover.office_blind
  - delay: 00:00:06
  - service: light.turn_on
    data:
      entity_id: light.office_led
  - service: switch.turn_off
    data:
      entity_id: switch.office_lights
  - service: automation.turn_off
    data:
      entity_id: automation.office_door_open_lights_on

Not sure what I am doing wrong?

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'

1 Like

ah thank you that solved it :slight_smile:

Thank you
I adjusted it like this:

 {{ (utcnow() | as_timestamp - state_attr('automation.Teplota_bojler_malo_vody_01', 'last_triggered') | as_timestamp ) | default(0) | int }}

I had to put an ID in place friendly name
Errors no longer report. But again, it doesn’t do anything. I’m out of it … :smiley:

Are you still wanting > 6 hrs? if so you need to add > 21600. Also the name should be all lower case. Teplota → teplota.

It should indicate that the temperature is low only once
I try it like this:

alias: Teplota bojler malo vody 02
description: ''
trigger:
  - platform: numeric_state
    entity_id: sensor.bojler
    below: '70'
condition:
  - condition: template
    value_template: >-
      {{ (utcnow() | as_timestamp -
      state_attr('automation.Teplota_bojler_malo_vody_02', 'last_triggered') |
      as_timestamp ) | default(0) | int }}
action:
  - service: tts.voicerss_say
    data:
      entity_id: media_player.obyvaci_pokoj_speaker
      message: >-
        Voda v bojleru má nedostatečnou teplotu {{states('sensor.bojler')}}
        stupňů celsia. Šetřete teplou vodou!!
mode: single

it doesn’t do anything and there are no more mistakes