Nightly House Check Routine Automation

I want to implement a ‘nightly check’ routine, triggered when we set the home alarm system that will do a bunch of checks and send a mobile alert if any fail, things like:

  • Check door locks are locked
  • Check car is locked and plugged in to charge
  • Check dishwasher is set to run
  • Check certain lights are off
  • Check certain appliances are off

If all the checks pass, it should not send any notification, if any of the checks fail it should send one notification with a list of the failed checks.

What is the easiest way to implement this? Is it an Automation with a bunch of if/thens? Or is there an easier way?

The only thing I can’t figure with the automation is how to construct the Notification body text from the output of all the failed tests - would need to be stored in a temp variable somehow?

Thanks!

So yes this is pretty simple but I can’t get my head around where to do the string concatenation for the output message. The pseudo code is:

if night_check_1 = false
then message = message + "Check 1 Failed"
if night_check_2 = false
then message = message + "Check 2 Failed"
if length(message) > 0
then send_notification(message)

How would I do this in the automation, the logic to do this needs to be in the ‘then’ blocks below but I can’t work out the syntax.

alias: Nightly Check
description: ""
trigger: []
condition: []
action:
  - if:
      - condition: state
        entity_id: light.hue_smart_plug_1_2
        state: "on"
    then:
      - service: logbook.log
        metadata: {}
        data:
          message: "Nightly Check: ALERT - Festoon Lights are ON"
    else:
      - service: logbook.log
        metadata: {}
        data:
          message: "Nightly Check: OK - Festoon Lights are OFF"
  - if:
      - condition: state
        entity_id: light.gledopto_gl_c_008_light
        state: "on"
    then:
      - service: logbook.log
        metadata: {}
        data:
          message: "Nightly Check: ALERT - Hot Tub Light are ON"
    else:
      - service: logbook.log
        metadata: {}
        data:
          message: "Nightly Check: OK - Hot Tub Lights are OFF"
mode: single

You will encounter a hurdle imposed by a script variable’s scope.

  • When you define a script variable at the start of an automation’s actions, that variable’s value is accessible in all actions. However, if you attempt to modify that variable’s value inside of an if structure, the new value exists only within that if structure.

  • Similarly, if you define a variable inside of an if then it’s only defined within the if and undefined elsewhere.

What I suggest you do is create variables for each check you want to perform. Compute the variable’s value using a template. Then simply concatenate all variables containing alert messages and send the result in a single notification.

BTW, I would test a list of lights, not each one individually, and report all that are on in a single text string.

1 Like

Thank you!

I’m confused as to how that is possible if the variable value set inside the IF statement is not accessible outside the IF statement.

Surely even if I define:

variables: 
  test_1_result: ""
  test_2_result: ""

Then set those results strings with the tests inside the IF statements, surely when I come to concatentate them at the end, they will still be equal to “” per the scope problem you mentioned?

I think I’m missing the understanding of this one sentence you said:

Compute the variable’s value using a template.

Can you link to an example?

Alternatively is it possible to use a text ‘helper’ as a substitute for a global variable here?

The notification produced by the following example will be “red zilch”. The color variable’s value remains unchanged and the animal variable is undefined.

action:
  - variables:
      color: 'red'
  - if: "{{ color == 'red' }}"
    then:
      - variables:
          color: 'blue'
          animal: 'cat'
  - service: notify.persistent.notification
    data:
      message: "{{ color }} {{ animal | default('zilch', true) }}"
action:
  - variables:
      lights:
        - light.hue_smart_plug_1_2
        - light.gledopto_gl_c_008_light
      lights_on: > 
        {{ expand(lights) | selectattr('state', 'eq', 'on')
          | map(attribute='attributes.friendly_name')
          | join(', ') }}
      locks:
        - lock.front_door
        - lock.rear_door
      locks_unlocked: > 
        {{ expand(locks) | selectattr('state', 'eq', 'unlocked')
          | map(attribute='attributes.friendly_name')
          | join(', ') }}
  - condition: "{{ lights_on | count > 0 or locks_unlocked | count > 0 }}"
  - service: notify.whatever
    data:
      message: >
        {% if lights_on | count > 0 %}
          The following lights are on: {{ lights_on }}
        {% endif %}
        {% if locks_unlocked | count > 0 %}
          The following doors are unlocked: {{ locks_unlocked }}
        {% endif %}
1 Like

Thank you! This is awesome.

1 Like