Condition template every minute

Hi,
I have an automation that is very noisy. Ie it is continually triggering since I am monitoring if the amount of watts my air condition is using is being updated.

I want to prevent it triggering so often to only once per minute. How do I write a condition that limits execution only at one minute.

Here is my ‘noisy’ automation which keeps an input_boolean on if the wifi is still connected to my current sensor.

- id: '1661032665990'
  alias: 'VALIDATE: Check if Air Conditioner is functioning properly'
  description: ''
  trigger:
  - platform: state
    entity_id:
    - sensor.mr_slim_watts
  condition:
  - condition: or
    conditions:
    - condition: template
      value_template: '{{ trigger.to_state.state|float > trigger.from_state.state|float
        }}

        '
    - condition: template
      value_template: '{{ trigger.to_state.state|float < trigger.from_state.state|float
        }}

        '
  - condition: state
    entity_id: sensor.airconditioner_operational
    state: 'True'
  action:
  - service: input_boolean.turn_on
    data: {}
    target:
      entity_id: input_boolean.mr_slim_current
  - delay:
      hours: 0
      minutes: 5
      seconds: 0
      milliseconds: 0
  - service: input_boolean.turn_off
    data: {}
    target:
      entity_id: input_boolean.mr_slim_current
  mode: restart

How about every 5 minutes?

Just change this:

mode: restart

To this:

mode: single
max_exceeded: silent

Your delay:

  - delay:
      hours: 0
      minutes: 5
      seconds: 0
      milliseconds: 0

Will then prevent this triggering more than once per 5 minutes, as the automation will run for 5 minutes and only one instance will be allowed to run at a time. The max_exceeded: silent line prevents logging of attempts to run more than one instance filling up your log.

2 Likes

Another thing that might help is adding to: null to your state trigger so it only triggers when the state changes, and not on e.g. attribute changes (which can be often)

Thanks, although the idea is for the delay only to time out if the sensor.mr_slim_watts isn’t contactable. So having it as a single rather than reset doesn’t achieve the outcome.

I can live with it but it is noisy in the logbook as it triggers multiple times a minute.

I believe that the “last_triggered” attribute of an automation only gets set when the actions actually run so you could condition your automation on that:

condition: "{{ as_timestamp(now()) - as_timestamp(state_attr('automation.your_really_long_automation_name', 'last_triggered')) > 60}}"

Perfect, thank you!! Exactly what I was looking for, here it is.

condition: template
value_template: >
  {{ as_timestamp(now()) -
  as_timestamp(state_attr('automation.validate_check_if_air_conditioner_is_functioning_properly',
  'last_triggered')) > 240 }}

Here’s the same thing but shorter; Template Condition in shorthand notation and calculation is performed with datetime objects instead of timestamps.

- "{{ now() - state_attr('automation.validate_check_if_air_conditioner_is_functioning_properly', 'last_triggered') > timedelta(minutes=4) }}"

Even shorter:

- "{{ now() - state_attr(this.entity_id, 'last_triggered') > timedelta(minutes=4) }}"

Similar example with explanation.

NOTE

Neither this template or the original one will work correctly with a brand new automation. An automation that has never triggered will lack a last_triggered attribute and cause the subtraction to fail. Given that your automation has already triggered, you won’t encounter this problem (however there is a way to mitigate the issue using a default filter).

can you please show the example with the default filter?

- "{{ now() - state_attr(this.entity_id, 'last_triggered') | default(today_at()) > timedelta(minutes=4) }}"
1 Like

so here is my automation:

- id: livingroom_alexa_notify_phone_charging
  alias: Livingroom Alexa Notifys Any Phone Charging
  trigger:
  - platform: numeric_state
    entity_id: sensor.tasmota_energy_power
    above: 1
  condition: template
    value_template: "{{ now() - state_attr(this.automation.livingroom_alexa_notifys_samsung_phone_not_charging, 'last_triggered') | default(today_at())  > timedelta(seconds=2) }}"
  action:
  - delay: 00:00:11
  - service: notify.alexa_media
    data:
      target:
      - media_player.living_room_echo_dot
      message: Your phone is charging now !!
      data:
        type: tts

However I get this error:

2022-10-20 19:55:02.611 ERROR (MainThread) [homeassistant.components.automation] mapping values are not allowed here
  in "/config/automations.yaml", line 175, column 19

Because you changed the example I provided.

Just use my example exactly as shown (don’t change it).

You cannot use this.automation.livingroom_alexa_notifys_samsung_phone_not_charging.
Use either this.entity_id (I like more) or automation.livingroom_alexa_notifys_samsung_phone_not_charging.

Try this:

- id: livingroom_alexa_notify_phone_charging
  alias: Livingroom Alexa Notifys Any Phone Charging
  trigger:
  - platform: numeric_state
    entity_id: sensor.tasmota_energy_power
    above: 1
  condition: template
    value_template: "{{ now() - state_attr(this.entity_id, 'last_triggered') | default(today_at())  > timedelta(seconds=2) }}"
  action:
  - delay: 00:00:11
  - service: notify.alexa_media
    data:
      target:
      - media_player.living_room_echo_dot
      message: Your phone is charging now !!
      data:
        type: tts

Or this (although the previous one will support better if you rename your automation):

- id: livingroom_alexa_notify_phone_charging
  alias: Livingroom Alexa Notifys Any Phone Charging
  trigger:
  - platform: numeric_state
    entity_id: sensor.tasmota_energy_power
    above: 1
  condition: template
    value_template: "{{ now() - state_attr(automation.livingroom_alexa_notifys_samsung_phone_not_charging, 'last_triggered') | default(today_at())  > timedelta(seconds=2) }}"
  action:
  - delay: 00:00:11
  - service: notify.alexa_media
    data:
      target:
      - media_player.living_room_echo_dot
      message: Your phone is charging now !!
      data:
        type: tts
1 Like

Thank you :blush:

1 Like