How to notify instantly on sensor state but not more often than x hours

I can’t see a way to do this, maybe you can…

I want to receive instant notification as soon as a sensor drops below a given value, and I want to be repeatedly notified but no more often than once per day.

This can’t be done with a trigger running once every x unit of time with numeric_state condition because that fails to give immediate notification. You can make the interval smaller and approach a solution, but never reach it - also it’s inelegant.

This also can’t be done with numeric_state or template trigger because from what I understand and have observed, these fail to trigger if the sensor state hasn’t changed. Even though the sensor state is being polled by HASS very frequently the value may be very stable at a single value below the threshold condition and in this case there will be no trigger ever.

Is there some other way to do this that I’m missing?

Simple solution is to have an automation based on the numeric state. That will trigger immediately.

Then, as the last action turn off that automation. Have another automation turn it back on at midnight or whatever time you choose.

Hi Silvrr, thanks for chiming in. I’ve tried this approach, the issue is that when I turn the automation back on it doesn’t fire immediately if the sensor state is already below threshold… it waits until the sensor value changes to a new value that is also below threshold.

A bit of a crude way of doing it but…

Trigger 1: Numeric State < threshold
Trigger 2: Time trigger at 12:00:01 (right after the automation is turned back on)

Condition 1: Numeric state < threshold

The automation would trigger anytime you go below the threshold, it would also trigger right after the automation is turned back on, however, if not below the threshold the condition would prevent the action from firing.

Interesting - a hybrid approach. As crude as it is, it’s the best option so far.

Is it possible to trigger on enabling of an automation? If so I could have Trigger 2 you described be tied to the automation coming on as opposed to a static time.

I thought about that, however, I wasn’t sure how the timing would work. Would the automation be on and running in time to see the state change?

You can certainly test it, setup a trigger based on the automation state.

I would use a time trigger and add condition to check if the state is below the threshold and if the automation was triggered in the past 24 hours.

Thanks arsaboo but one requirement is for instant action and I don’t see how time trigger can give instant action - eg if the state changes in between checks. Latency can be minimized by increasing frequency but can never be made 0, plus it’s a bit inelegant.

You can have two triggers…one time and another state. It should work in the same automation, otherwise you may have to separate them.

  - alias: Detect freezing weather test
    trigger:
      - platform: numeric_state
        entity_id: sensor.dark_sky_daily_low_temperature
        below: 2.0
      - platform: state
        entity_id: automation.detect_freezing_weather_test
        from: 'off'
        to: 'on'
    condition:
      condition: numeric_state
      entity_id: sensor.dark_sky_daily_low_temperature
      below: 2.0
    action:
      - service: notify.notify
        data:
          message: "Freezing weather in forecast!"
          title: "Weather warning"
      - service: automation.turn_off
        entity_id: automation.detect_freezing_weather_test
      - delay:
          hours: 24
      - service: automation.turn_on
        entity_id: automation.detect_freezing_weather_test

This almost works except it doesn’t trigger after it re-enables itself, but it works the first time it’s turned on manually.

Yeah I know I’m making things difficult on myself… practically I could get away with having my 24 hours start and stop at a set time every day, but some OCD part of me wants to try and solve the challenge of making it 24 hours relative to the time of the trigger instead.

You don’t need a delay. Remove that and add a condition last_triggered similar to this.

I assume you also mean to remove the turn_off and turn_on action. So if I do that the trigger will fire once on startup, because of:

  - platform: state
    entity_id: automation.detect_freezing_weather_test
    from: 'off'
    to: 'on'

…but not ever again unless the sensor value changes. Your last_trigger idea gave me a thought however. Rather than using it as a condition, I tried using it as the trigger:

  - alias: Detect freezing weather test
    trigger:
      - platform: state
        entity_id: automation.detect_freezing_weather_test
        to: 'on'
      - platform: template
        value_template: "{{ (as_timestamp(now()) - as_timestamp(states.automation.detect_freezing_weather_test.attributes.last_triggered)) > 60  }}"

    action:
      - service: notify.notify
        data:
          message: "Freezing weather in forecast!"
          title: "Weather warning"

hoping that would cause a 60s trigger loop, but alas it only fired on startup.

That is why you also need a time trigger to check periodically. BTW, "{{ (as_timestamp(now()) - as_timestamp(states.automation.detect_freezing_weather_test.attributes.last_triggered)) > 60 }}" checks if the automation was triggered in the last 60 seconds.

1 Like