Rain delay automation

Hello! I am new to home assistant and trying to figure out the best way to implement a rain delay for my lawn mower robot.

Specifically, I would like there to be a rain delay of two hours whenever

  1. There is precipitation
  2. There is dew

For the moment, I’m trying to set a rain delay helper, and I’ll use that helper to control the mower later.

To determine the rain condition, I am using a template:

{% macro macro_is_wet(unused, returns) %}
{%-
  do returns(states('sensor.kpamanhe83_precipitation_rate') | float > 0.0 or 
             states('sensor.kpamanhe83_dewpoint') | float - states('sensor.kpamanhe83_temperature') | float >= -2.0)
-%}
{% endmacro %}
{% set is_wet = macro_is_wet | as_function %}

I then have two automations. One sets the rain delay helper as soon as is_wet(none). The other unsets the helper after not is_set(none) for two hours.

  1. Is there a better way of implementing this in general? It doesn’t seem like it will work correctly upon HA restart because the template triggers need to “change”. I can add a separate automation for restart, but it seems a bit clunky and like I’m fighting against the system to get the behavior I want.
  2. I added the unused parameter because it seems like as_function requires the macro to have a parameter besides the returns parameter. Is this right?

This seems like a prime candidate for a template sensor.

template:
  - binary_sensor:
      - name: Wet Outside
        state: >
          {{ states('sensor.kpamanhe83_precipitation_rate') | float(0) > 0.0 or 
             states('sensor.kpamanhe83_dewpoint') | float(-2) - states('sensor.kpamanhe83_temperature') | float(0) >= -2.0) }}
        delay_off: "2:00:00"
1 Like

Thanks @mekaneck! I think you are right; I’ll give this is a shot!

Hi,
Your use of macros is very strange in the way you define it, use it, why you use it in the first place and how you think it should help with the delay. Is this something AI came up with? AI is no good for any of HA configuration.

I would just use the template itself:

{{ states('sensor.kpamanhe83_precipitation_rate') | float(0) > 0.0 or 
   (states('sensor.kpamanhe83_dewpoint') | float(0) -
    states('sensor.kpamanhe83_temperature') | float(0)) >= -2.0 }}

If you must use a macro:

{% macro is_wet() %}
{{ states('sensor.kpamanhe83_precipitation_rate') | float(0) > 0.0 or 
   (states('sensor.kpamanhe83_dewpoint') | float(0) -
    states('sensor.kpamanhe83_temperature') | float(0)) >= -2.0 }}
{% endmacro %}

{{ is_wet() }}

If you create a template sensor from this, you can use a state condition with a for clause for the delay.

No, I did not use AI. I created the macro because I didn’t want to duplicate code across multiple automations.

I believe that your macro suggestion would not work for {{ not is_wet() }} because macros return strings.

I agree that a template sensor seems like the right way to go.

So the template sensor is very nearly working (I think), but when I first restart the sensor becomes Unknown rather than Off. I think this is because of the delay_off. Is there any way around this? If not, I can probably just treat Unknown as synonymous as Off.

Yes, macro’s return strings. you can use to_jason and from_json to get around it or test for a specific string if you want. Both I and other replies suggested to create a binary sensor from the result. A binary sensor can be used as many times as you like. It is, in a sense, replacing your macro. That is why I suggested to not use macros at all.

I don’t think any of the delay options will survive a restart. I think you’d need to change to a trigger-based template sensor (without using the delay options) for more control and to achieve a persistent state.

So I’m still doing some testing, but actually it does seem like the template sensor survives a restart! It is only during the initial delay_off period that it shows as Unknown.

The issue is that if you are, for example, one hour into the 2-hour delay period and then perform a restart, the delay will have to run the full 2 hours once HA starts back up again.

You are right, it only resets after 2 hours in case HA is restarted. But that is not too bad. My original solution I think could get stuck indefinitely in the wrong state waiting for a state change.

1 Like

I just wanted to say I’ve been using this for a while and it works really well for me.

input_boolean:
  mower_paused_for_rain_delay:
    name: "Mower is currently paused for rain delay"
    icon: mdi:toggle-switch

automation:
- id: '1753390024208'
  alias: Navimow warning
  description: ''
  triggers:
  - trigger: state
    entity_id:
    - sensor.mr_yuka_activity_mode
    to: MODE_PAUSE
    for:
      hours: 0
      minutes: 10
      seconds: 0
    from: MODE_RETURNING
  conditions:
  - condition: state
    entity_id: binary_sensor.mr_yuka_charging
    state: 'off'
    for:
      hours: 0
      minutes: 10
      seconds: 0
  actions:
  - action: notify.pushover
    metadata: {}
    data:
      message: Mower has been paused for 10 minutes!
  mode: single

- id: '1753716263782'
  alias: 'Lawn mower: return to dock'
  description: ''
  triggers:
  - trigger: template
    value_template: "{{ \n  states('binary_sensor.rain_delay_wet_damp_outside') ==
      \"on\" and \n  states('sensor.mr_yuka_activity_mode') != \"MODE_READY\" and\n
      \ states('input_boolean.mower_paused_for_rain_delay') == \"off\"\n}}"
  conditions: []
  actions:
  - action: lawn_mower.dock
    metadata: {}
    data: {}
    target:
      device_id: d09117bd2602ad6d2b23ab292d69e92a
  - action: input_boolean.turn_on
    metadata: {}
    data: {}
    target:
      entity_id: input_boolean.mower_paused_for_rain_delay
  mode: single

- id: '1753749431987'
  alias: 'Lawn mower: resume mowing after rain delay and charging'
  description: Resume mowing after rain delay is off and charging has completed.
  triggers:
  - trigger: template
    value_template: "{{ \n  states('binary_sensor.rain_delay_wet_damp_outside') ==
      \"off\" and \n  states('input_boolean.mower_paused_for_rain_delay') == \"on\"\n}}"
  conditions: []
  actions:
  - action: input_boolean.turn_off
    metadata: {}
    data: {}
    target:
      entity_id: input_boolean.mower_paused_for_rain_delay
  - action: lawn_mower.start_mowing
    metadata: {}
    data: {}
    target:
      device_id: d09117bd2602ad6d2b23ab292d69e92a
  mode: single

template:
- binary_sensor:
    - name: Rain delay (wet/damp outside)
      state: >
        {{ states('sensor.precipitation_rate') | float(0) > 0.0 or
          states('sensor.kpamanhe83_temperature') | float(100) - 2.0 <= states('sensor.kpamanhe83_dewpoint') | float(50) }}
      delay_off: "2:00:00"

- binary_sensor:
    - name: Currently raining
      state: >
        {{ states('sensor.precipitation_rate') | float(0) > 0.0 }}

- binary_sensor:
    - name: Currently dewy
      state: >
        {{ states('sensor.kpamanhe83_temperature') | float(100) - 2.0 <= states('sensor.kpamanhe83_dewpoint') | float(50) }}