Triggers, trigger id's choose and conditions - what am I missing here

Hi, I’m hoping someone can take a look at this and spot the glaring mistake in the automation.
Basically it looks for two switches on the fridge and freezer and when they go off (someone turned them off, power failure) or they go unavailable I send a critical notification including how long they have been off/unavailable for and then repeat every minute until its resolved. I plan on adding some more logic to it and notification to Alexa etc when someone is home.
Problem is if I turn the switches off there’s not notification at all, when I turn them back on I get notified that they are restored. Automation as follows:

  - alias: "🔔 Monitor Fridge & Freezer Power"
    id: MonitorFridgeFreezerGarage
    mode: parallel

    triggers:
      - id: fridge_off
        trigger: state
        entity_id: switch.garage_fridge_freezer
        from: 
          - 'on'
          - 'unavailable'
        to: 'off'

      - id: fridge_unavailable
        trigger: state
        entity_id: switch.garage_fridge_freezer
        from: 'on'
        to: "unavailable"

      - id: fridge_restored
        trigger: state
        entity_id: switch.garage_fridge_freezer
        from:
          - 'off'
          - "unavailable"
        to: 'on'

      - id: freezer_off
        trigger: state
        entity_id: switch.garage_freezer
        from: 
          - 'on'
        to: 
          - 'off'

      - id: freezer_unavailable
        trigger: state
        entity_id: switch.garage_freezer
        from: 'on'
        to: "unavailable"

      - id: freezer_restored
        trigger: state
        entity_id: switch.garage_freezer
        from:
          - 'off'
          - "unavailable"
        to: 'on'

    variables:
      fridge_state: "{{ states('switch.garage_fridge_freezer') }}"
      freezer_state: "{{ states('switch.garage_freezer') }}"
      fridge_off_since: "{{ states.switch.garage_fridge_freezer.last_changed if fridge_state in ['off', 'unavailable'] else None }}"
      freezer_off_since: "{{ states.switch.garage_freezer.last_changed if freezer_state in ['off', 'unavailable'] else None }}"
      fridge_duration: "{{ (now() - fridge_off_since).seconds // 60 if fridge_off_since != None else 'N/A' }} minutes"
      freezer_duration: "{{ (now() - freezer_off_since).seconds // 60 if freezer_off_since != None else 'N/A' }} minutes"

    conditions: []
    actions:
      - choose:
          - conditions:
              - condition: trigger
                id: fridge_off
            sequence:
              - action: notify.ios_wayne_devices
                data:
                  title: "🚨 Fridge Freezer POWER OFF!"
                  message: "⚠️ The fridge freezer has been OFF for {{ fridge_duration }}. Restore power immediately!"
                  data:
                    push:
                      sound: 
                        name: "default"
                        critical: 1
                        badge: 1
                        volume: 1.0
              - action: notify.send_message
                target:
                  entity_id: notify.alerts_log
                data:
                  message: >
                    {{ now().strftime('%d-%m-%y [%I:%M%p]') }}: ⚠️ Fridge Freezer OFF for {{ fridge_duration }}.

          - conditions:
              - condition: trigger
                id: fridge_unavailable
            sequence:
              - action: notify.ios_wayne_devices
                data:
                  title: "🚨 Fridge Freezer UNAVAILABLE!"
                  message: "⚠️ The fridge freezer has been UNAVAILABLE for {{ fridge_duration }}. Check power immediately!"
                  data:
                    push:
                      sound: 
                        name: "default"
                        critical: 1
                        badge: 1
                        volume: 1.0
              - action: notify.send_message
                target:
                  entity_id: notify.alerts_log
                data:
                  message: >
                    {{ now().strftime('%d-%m-%y [%I:%M%p]') }}: ⚠️ Fridge Freezer UNAVAILABLE for {{ fridge_duration }}.

          - conditions:
              - condition: trigger
                id: freezer_off
            sequence:
              - action: notify.ios_wayne_devices
                data:
                  title: "🚨 Freezer POWER OFF!"
                  message: "⚠️ The freezer has been OFF for {{ freezer_duration }}. Restore power immediately!"
                  data:
                    push:
                      sound: 
                        name: "default"
                        critical: 1
                        badge: 1
                        volume: 1.0
              - action: notify.send_message
                target:
                  entity_id: notify.alerts_log
                data:
                  message: >
                    {{ now().strftime('%d-%m-%y [%I:%M%p]') }}: ⚠️ Freezer OFF for {{ freezer_duration }}.

          - conditions:
              - condition: trigger
                id: freezer_unavailable
            sequence:
              - action: notify.ios_wayne_devices
                data:
                  title: "🚨 Freezer UNAVAILABLE!"
                  message: "⚠️ The freezer has been UNAVAILABLE for {{ freezer_duration }}. Check power immediately!"
                  data:
                    push:
                      sound: 
                        name: "default"
                        critical: 1
                        badge: 1
                        volume: 1.0
              - action: notify.send_message
                target:
                  entity_id: notify.alerts_log
                data:
                  message: >
                    {{ now().strftime('%d-%m-%y [%I:%M%p]') }}: ⚠️ Freezer UNAVAILABLE for {{ freezer_duration }}.

          - conditions:
              - condition: trigger
                id: fridge_restored
            sequence:
              - action: notify.ios_wayne_devices
                data:
                  title: "✅ Fridge Freezer Restored"
                  message: "The fridge freezer is back ON and functioning normally."
                  data:
                    push:
                      sound: 
                        name: "default"
                        critical: 1
                        badge: 1
                        volume: 1.0
              - action: notify.send_message
                target:
                  entity_id: notify.alerts_log
                data:
                  message: >
                    {{ now().strftime('%d-%m-%y [%I:%M%p]') }}: ✅ Fridge Freezer power restored.

          - conditions:
              - condition: trigger
                id: freezer_restored
            sequence:
              - action: notify.ios_wayne_devices
                data:
                  title: "✅ Freezer Restored"
                  message: "The freezer is back ON and functioning normally."
                  data:
                    push:
                      sound: 
                        name: "default"
                        critical: 1
                        badge: 1
                        volume: 1.0
              - action: notify.send_message
                target:
                  entity_id: notify.alerts_log
                data:
                  message: >
                    {{ now().strftime('%d-%m-%y [%I:%M%p]') }}: ✅ Freezer Power Restored.

      - repeat:
          while:
            - condition: or
              conditions:
                - condition: state
                  entity_id: switch.garage_fridge_freezer
                  state: "off"
                - condition: state
                  entity_id: switch.garage_fridge_freezer
                  state: "unavailable"
                - condition: state
                  entity_id: switch.garage_freezer
                  state: "off"
                - condition: state
                  entity_id: switch.garage_freezer
                  state: "unavailable"
          sequence:
            - action: notify.ios_wayne_devices
              data:
                title: "⚠️ Power Alert - Fridge/Freezer"
                message: >-
                  {% if fridge_state in ['off', 'unavailable'] and freezer_state in ['off', 'unavailable'] %}
                    🚨 Both fridge freezer and freezer are OFF or UNAVAILABLE for (Fridge: {{ fridge_duration }}, Freezer: {{ freezer_duration }})! Immediate action required!
                  {% elif fridge_state in ['off', 'unavailable'] %}
                    ⚠️ Fridge freezer OFF or UNAVAILABLE for {{ fridge_duration }}. Restore power now!
                  {% elif freezer_state in ['off', 'unavailable'] %}
                    ⚠️ Freezer OFF or UNAVAILABLE for {{ freezer_duration }}. Restore power now!
                  {% else %}
                    ✅ All appliances are functioning normally.
                  {% endif %}
                  data:
                    push:
                      sound: 
                        name: "default"
                        critical: 1
                        badge: 1
                        volume: 1.0
            - action: notify.send_message
              target:
                entity_id: notify.alerts_log
              data:
                message: >
                  {{ now().strftime('%d-%m-%y [%I:%M %p]') }} Repeating alert: Fridge Freezer - {{ fridge_state }} ({{ fridge_duration }}), Freezer - {{ freezer_state }} ({{ freezer_duration }})
            - delay: "00:01:00"

I’ve checked the traces when the automation should run (when a switch is turned off) the trace is empty/blank


and when it’s switched back on

I’ve looked and not really found any good explanations/examples. I’m confused why when the switch is turn back on I get a notification.

Really appreciate someone taking a look and point out whats wrong

Much appreciation in advance

Have you checked the error logs?

The while loop notifies directly after the first notification (the delay is at the end) so I wonder if two notifications this short after one another result in errors.

The trace is probably empty because the automation has not finished yet. Have you waited more than a minute?

Maybe you shoult look at the alert integration, you are trying to mimic what is does.

There’s also a community driven version:

1 Like

Thanks - I finally figured it out, there was issues with the variables not working correctly, fixed now.

    variables:
      fridge_state: "{{ states('switch.garage_fridge_freezer') }}"
      freezer_state: "{{ states('switch.garage_freezer') }}"

      fridge_off_since: >-
        {% if fridge_state in ['off', 'unavailable'] and states.switch.garage_fridge_freezer.last_changed %}
          {{ as_datetime(states.switch.garage_fridge_freezer.last_changed) }}
        {% else %}
          none
        {% endif %}

      freezer_off_since: >-
        {% if freezer_state in ['off', 'unavailable'] and states.switch.garage_freezer.last_changed %}
          {{ as_datetime(states.switch.garage_freezer.last_changed) }}
        {% else %}
          none
        {% endif %}

      fridge_duration: >-
        {% if fridge_off_since != none and fridge_off_since is not string %}
          {{ ((now() - fridge_off_since).total_seconds() // 60) | int }} minutes
        {% else %}
          "N/A"
        {% endif %}

      freezer_duration: >-
        {% if freezer_off_since != none and freezer_off_since is not string %}
          {{ ((now() - freezer_off_since).total_seconds() // 60) | int }} minutes
        {% else %}
          "N/A"
        {% endif %}

And I’ll take a look at Alert2, looks like some awesome features fixing those missing from the builtin Alerts.

Thanks again, great to have another set of eyes on it.

1 Like