Preventing "duplicate" runs of an automation

I have an automation that triggers a notification when a UV sensor goes above a certain value. Works well but it sends multiple times a day even if the UV level stays above the trigger value. (Not sure if that’s because the value periodically updates and the automation trigger just sees that it’s updated and is over the threshold so triggers or what.) Maybe there is just a better way to trigger this?

For example I got 3-5 notifications from the automation today and this is what the data looked like. The lowest it hit throughout the day was 3.4, clearly over the threshold of 2.9 (see below)
image

Instead of trying to find root cause (which I know is usually best) I decided to try and suppress additional runs of the automation (or at least the resulting action). Found many examples of this method but I can’t get it to work. It always returns true when I test it in the Developer Tools > Template as a result when I manually set a new UV to test the automation it always triggers even if it just ran 5 seconds ago.

condition: template
  value_template: '{{ (as_timestamp(now()) - as_timestamp(states.automation.climate_uv_warning_test.last_triggered
      | default(0)) | int > 60)}}'

automation.yaml

- id: '1600468153759'
  alias: 'Climate: UV Warning TEST'
  description: ''
  trigger:
  - above: '2.9'
    entity_id: sensor.current_uv_index_rounded
    platform: numeric_state
  condition:
  - condition: template
    value_template: '{{ (as_timestamp(now()) - as_timestamp(states.automation.climate_uv_warning_test.last_triggered
      | default(0)) | int > 60)}}'
  action:
  - data:
      message: "test"
      title: ☀️ UV TEST
    service: notify.mobile_app_xxxx_iphone
  mode: single

Open to any suggestions to reach my goal. Maybe I’m just missing a much simpler solution or reason for it triggering multiple times a day to begin with.

If you are using mode: single simply putting a delay as the last action will prevent the automation running any more often than the delay time.

Still, the automation should not trigger unless the numeric threshold is crossed.

3 Likes

Ah sounds like a simple solution for this particular use case. Will try it and report back. Though a HA restart would reset that I assume but hopefully restarts will be rare once I get most of my development finished.

Did you reload automations or restart Home Assistant? That would reset a Numeric State Trigger.

Sometimes but not all the time IIRC. It was something I considered but seemingly happens sometimes without a restart / reload. I’d have to track the correlation more closely though.

I just noticed that your template refers to the automation’s last_triggered attribute like this:

states.automation.climate_uv_warning_test.last_triggered

It should be referring to it like this:

states.automation.climate_uv_warning_test.attributes.last_triggered

You have that condition to prevent executing the action if the automation is re-triggered within 60 seconds? In theory, that condition shouldn’t be necessary because the Numeric State trigger will not trigger again so quickly (not unless it is reset within 60 seconds).

Thanks will try adding .attributes. The 60 second thing was just for testing feedback.

I still haven’t figured out why the automation gets triggered multiple times a day even though the UV level never dips below the threshold and HA doesn’t get rebooted. Maybe the underlying sensors the template replies on are refreshing which causes a refresh of the template which triggers the automation again?

current_uv_index_rounded:
        unit_of_measurement: UV index
        friendly_name: Current UV Index Rounded
        icon_template: 'mdi:weather-sunny'
        value_template: >
            {% if states('sensor.openuv_current_uv_index')  != 'unavailable' and states('sensor.openuv_current_uv_index')  != 'unknown' and states('sensor.openuv_current_uv_index')  != 'None' %}
                {{ states('sensor.openuv_current_uv_index') | round(1) }}
            {% else %}
              {{ states('sensor.uv_current_mean') | round(1) }}
            {% endif %}

Tried adding .attributes which seems to evaluate correctly in Developer Tools > Templates now but the automation still fires regardless of whether it returns true or false.

{{ (as_timestamp(now()) - as_timestamp(states.automation.climate_uv_warning_test.attributes.last_triggered | default(0)) | int > 60)}}

Probably going to give up on it for now since I used the “dummy” approach of adding a long delay at the end of the automation as suggested by @tom_l. Not “elegant” per say but it works so good enough for now.