Only allow automation to trigger once a day?

Have you tried this?

condition:
  - condition: template
    value_template: >-
       "{{ (utcnow() | as_timestamp - state_attr('automation.teplota_bojler_malo_vody_02', 'last_triggered')  | as_timestamp) | default(0) | int > 21600 }}"

You need a comparison to get true/false

I have the same basic automation going. Its trigged by my bedroom door opening with the condition that my phone is off the charger.
I use a helper. The automation toggles the helper on. Then I have a condition that will only allow the automation to run if the toggle is off.
I have a separate automation that turns that toggle back off at midnight, along with some others I only want to run once a day.
I am always the first out of bedā€¦
I also have seperate automations that take place after our bedroom door has been opened for 15 minutes. Like the bedroom lights become active upon motion. I have a failsafe built in in case I forget to close the bedroom door all the way or if the sensor doesnt register the door closing. Wouldnā€™t want the lights to come on and wake up the wife.

description: ''
trigger:
  - type: opened
    platform: device
    device_id: e71c134acb81c8be7a35797a38ab10dd
    entity_id: binary_sensor.master_bedroom_door_home_security_intrusion
    domain: binary_sensor
condition:
  - type: is_not_plugged_in
    condition: device
    device_id: c3c350836d270a6ab7f59a03403e1d18
    entity_id: binary_sensor.sm_g973u_is_charging
    domain: binary_sensor
  - condition: time
    after: '03:00:00'
    before: '08:00:00'
  - condition: state
    entity_id: input_boolean.wake_up_has_run
    state: 'off'
action:
  - device_id: 065681e3bdecd9525368eb4113591cce
    domain: select
    entity_id: select.harmony_hub_activities
    type: select_option
    option: Watch TV
  - service: light.turn_on
    data:
      transition: 30
    target:
      entity_id:
        - light.living_room_lamps_zha_group_0x0003
  - type: turn_on
    device_id: f407b33553078388d708da4afdd6c97b
    entity_id: switch.kitchen_lights
    domain: switch
  - type: turn_on
    device_id: 456a40fec7df0c228a9a583bad003788
    entity_id: switch.hallway_lamps
    domain: switch
  - type: turn_on
    device_id: 01a28cc6d329ae1a9160a02b69070c09
    entity_id: switch.under_cabinet_lights_2
    domain: switch
  - service: scene.turn_on
    target:
      entity_id: scene.wake_up_off_on
    metadata: {}
  - delay:
      hours: 0
      minutes: 10
      seconds: 0
      milliseconds: 0
  - choose:
      - conditions:
          - type: is_open
            condition: device
            device_id: e71c134acb81c8be7a35797a38ab10dd
            entity_id: binary_sensor.master_bedroom_door_home_security_intrusion
            domain: binary_sensor
        sequence:
          - service: notify.alexa_media_living_room_dot
            data:
              message: >-
                Attention, the master bedroom door has been left open. Please
                close it before the lights come on.
              data:
                type: announce
                method: all
    default: []
mode: single

I tried to adjust it to your liking. Iā€™ve worked with it before. Thatā€™s probably more than 30 versions.
Does it register for the first time > 21600 seconds? it does not respond yet ā€¦
These code writes are currently incomprehensible to me ā€¦ Iā€™m used to: If () else etc ā€¦ :smiley:

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

Do you know how to use developer tools ā†’ templates?

So I will explain what this does.

(utcnow() | as_timestamp

get the UTC time and format as unix timestamp i.e. seconds from 1-1-1970.

state_attr('automation.teplota_bojler_malo_vody_02', 'last_triggered') | as_timestamp)

give last triggered attribute as unix timestamp

pipe (filter) it to default with default of 0 when it is none or unknown

pipe (filter) it to int to remove fractions of a second

If you paste the following into developer tools ā†’ templates you should get an integer

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

seconds from (now - last triggered) > 21600 or have six hours or more elapsed.

so this pasted into developer tools ā†’ template

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

should give you true or false. If it is true the action will execute.

Does this help your understanding?

1 Like

So I tested it. The first time it is turned on, it will sound after int> 60 seconds and then all the time. there is a ratio between time_pattern and int> 60
This will happen first and then time_pattern * int> 60

alias: Teplota bojler malo vody 02
description: ''
trigger:
  - platform: time_pattern
    seconds: /1
condition:
  - condition: numeric_state
    entity_id: sensor.bojler
    below: '70'
  - condition: template
    value_template: >-
      {{ (utcnow() | as_timestamp -
      state_attr('automation.teplota_bojler_malo_vody_02', 'last_triggered')  |
      as_timestamp) | default(0) | int > 60 }}
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

so the testing automation will trigger every second

the actions will occur if:

sensor.bojler < 70 AND time now - last triggered is > 60

I donā€™t understand what you mean by a ratio between time_pattern * int > 60???

I would not consider the temperature of the boiler now, it is a condition that I test whether it will react or not, it works. The weird thing is that when time_pattern = / 1sec and int> 60, it reports for the first time right away and repeats after int> 60 sec. If I have an int> 120, it will report after 60 seconds and then report after another 60 seconds. Iā€™ll test it again tomorrow ā€¦ Iā€™m confused :frowning:

Because you use utcnow() it updates once a minute on the template. That is why for testing it makes no sense to trigger more than once a minute.

I am getting this error and the automation doesnā€™t work:

In ā€˜conditionā€™ (item 2 of 2):
In ā€˜templateā€™ condition: TypeError: ā€˜<ā€™ not supported between instances of ā€˜NoneTypeā€™ and ā€˜datetime.datetimeā€™

First you said it was ā€œsolvedā€ now you say ā€œthe automation doesnā€™t workā€.

You havenā€™t explained what changed to transform success into failure.

right sorry to cause the confusion but basically I have tried to use the same condition on another automation as well.

This is the automation:

- id: livingroom_blinds_open_in_the_morning
  alias: Livingroom Blinds Open in the Morning
  initial_state: 'on'
  trigger:
  - platform: state
    entity_id: binary_sensor.livingroom_motion_detector
    to: 'on'
  condition:
    - condition: time
      after: '06:30:00'
      before: '12:00:00'
    - "{{ state_attr('automation.livingroom_blinds_open_in_the_morning', 'last_triggered') < today_at('06:30') }}"
  action:
  - service: cover.set_cover_position
    data:
      position: 100
    target:
      entity_id: cover.livingroom_blinds

Now this automation doesnā€™t work and throws the following error/warning:

2022-04-21 10:51:15 WARNING (MainThread) [homeassistant.components.automation] Error evaluating condition in 'Livingroom Blinds Open in the Morning':
In 'condition' (item 2 of 2):
  In 'template' condition: TypeError: '<' not supported between instances of 'NoneType' and 'datetime.datetime'

In the template condition, if the automationā€™s name is incorrect (in this case it appears to be correct) or the automation has never been triggered yet, the result of the state_attr() function will be none.

If you recall, I explained this in a previous post and described how a default filter can mitigate this situation.

ok thanks I have used the default filter and changed the condition to:

- "{{ state_attr('automation.livingroom_blinds_open_in_the_morning', 'last_triggered') | default(today_at(), true) < today_at('06:30') }}"

1 Like

so in the end I solved it completely differently. In configuration.yaml, a global boolean variable is defined. When the temperature drops once it reports. The transformation turns off. The boiler heats up above the set limit and the variable is switched on.

configuration.yaml

input_boolean:
  notify_boiler:
    name: NotifyBoiler
    initial: ON

automation on only sound:

talias: Teplota bojler 68
description: ''
trigger:
  - platform: time_pattern
    minutes: /1
condition:
  - condition: time
    after: '08:00:00'
    before: '22:00:00'
    weekday:
      - mon
      - tue
      - wed
      - thu
      - fri
      - sat
      - sun
  - condition: state
    entity_id: input_boolean.notify_boiler
    state: 'on'
  - condition: numeric_state
    entity_id: sensor.bojler
    below: '68'
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!!
  - service: input_boolean.turn_off
    target:
      entity_id: input_boolean.notify_boiler
    data: {}
mode: single


automation reset event:

alias: Teplota bojler reset stavu
description: ''
trigger:
  - platform: time_pattern
    minutes: /10
condition:
  - condition: numeric_state
    entity_id: sensor.bojler
    above: '72'
action:
  - service: input_boolean.turn_on
    target:
      entity_id: input_boolean.notify_boiler
    data: {}
mode: single

thank you for the response :slight_smile:

Why have you chosen to use Time Pattern Triggers when the automation can clearly be triggered exclusively when desired events occur (and not needlessly and repeatedly every X minutes)?

I donā€™t know why yet, but the numerical condition doesnā€™t want me to work. The timer will check it safely. I know this is not the best solution.

So I am using the default filter but the automation keeps on triggering when the binary sensor turns to on I.e. when motion is detected.

Not sure if the condition is really working.

I think its new. Now you can add the action ā€œWait for time to pass (delay)ā€!

Working, but testing on automation show error:
ā€œtemplate value should be a string for dictionary value @ data[ā€˜value_templateā€™]. Got Noneā€

Open Issue on GitHub: https://github.com/home-assistant/frontend/issues/12282

1 Like

I actually do this by creating a input_boolean helper. It turns off at midnight and turns on with the specific automation that should only be run once. Then use input_booleanā€™s state = off as a condition for it. :slight_smile: