Alert VS Automation Notification [SOLVED]

I would like to setup the following notifications:

  1. Send repeated messages every 30 min if doors or windows are open after sundown.
  2. Send repeated messages every 30 min if doors or windows are open and the forecast calls for rain.

Should I use Automation or Alert? My understanding is the Automation will be triggered once each time the conditions are met, but Alerts will send out repeat notification after one trigger. What I cannot find are examples of using conditions within Alerts.

1 Like

I have never used the alert component, but to me it looks like the second example from the documentation is what you need.
So you create a template binary sensor which evaluates the states of the doors, windows, sun etc., and based on that sensor fire the alert.

@danielperna84
Thanks for the pointer. I think binary sensor will be able to solve this problem. But after a night of fighting with HA I could NOT get the senor working. Hopefully someone can point out where I went wrong with the code.

binary_sensor:
  - platform: template
    sensors:
      sun_up:
        value_template: '{{ states.sun.sun.attributes.elevation > 0 }}'
        friendly_name: 'Sun Is Up'
        entity_id:
          - sun.sun
      window_open_at_night:
        value_template: >-
          {%- if states.binary_sensor.sun_up.state == 'off'
              and states.binary_sensor.windows.state == 'on' -%}
          on
          {%- else -%}
          off
          {%- endif -%}
        friendly_name: 'Window(s) Is Open At Night'
        device_class: opening
        entity_id:
          - binary_sensor.sun_up
          - binary_sensor.windows

I get two new binary sensors from the above, but only the state for sun_up changes. window_open_at_night state never changes even when the if condition is false. What is wrong here?

Hmm. I use template sensors for this kind of thing - not binary sensors.

sensor:
  - platform: template
    sensors:
      gloomy:
        friendly_name: Gloomy
        value_template: >
            {% if states.sensor.dark_sky_cloud_coverage -%}
            {% if (states('sensor.dark_sky_cloud_coverage') | float) > 80 %}on{% else %}off{% endif %}
            {%- endif %}

For a reason that I am yet to figure out, I can NOT get statements to execute for value_template.
This works:

value_template: "{{ states.binary_sensor.sun_up.state == 'off' }}"

This does NOT:

value_template: "{% if is_state('binary_sensor.sun_up.state', 'off') %}on{% else %}off{% endif %}"

NOR this:

value_template: "{% if states.binary_sensor.sun_up.state == 'off' %}on{% else %}off{% endif %}"

The is the full code:

binary_sensor:
  - platform: template
    sensors:
      sun_up:
        value_template: '{{ states.sun.sun.attributes.elevation > 0 }}'
        friendly_name: 'Sun Is Up'
        entity_id:
          - sun.sun
      window_open_at_night:
        value_template: "{% if is_state('binary_sensor.sun_up.state', 'off') %}on{% else %}off{% endif %}"
        friendly_name: 'Window(s) Is Open At Night'
        device_class: opening
        entity_id:
          - binary_sensor.sun_up
          - binary_sensor.windows

And now, this works:

value_template: "{{ states.binary_sensor.sun_up.state == 'off' and states.binary_sensor.windows.state == 'on' }}"

Working code:

binary_sensor:
  - platform: template
    sensors:
      sun_up:
        value_template: '{{ states.sun.sun.attributes.elevation > 0 }}'
        friendly_name: 'Sun Is Up'
        entity_id:
          - sun.sun
      window_open_at_night:
        value_template: "{{ states.binary_sensor.sun_up.state == 'off' and states.binary_sensor.windows.state == 'on' }}"
        friendly_name: 'Window(s) Is Open At Night'
        device_class: opening
        entity_id:
          - binary_sensor.sun_up
          - binary_sensor.windows

I think that’s because binary_sensors want that template to evaluate as true or false (rather than on/off).

I was following template guide which has “Off” and “On” in the last example. I left the expressions in for now, but will keep that in mind for the future.

So, switching to true/false worked, then?

@ih8gates I didn’t use if statements. I simply left it with whatever condition the expression {{ ... }} returned.

@ih8gates,
You are correct. The template is expecting true/false to be returned. Will try to do a pull request and update the binary_sensor documentation.