Only allow automation to trigger once a day?

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

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)?