Goodnight Automation? (Smartthings clone)

Hi everyone,
Total newbie here, and if there’s a thread on this, I couldn’t find a helpful one via the search (sorry just point me to it!).

So a great feature in SmartThings was the goodnight automation. For me, with no movement after say 9pm, lights and some switches downstairs would turn off.
Trying to replicate that in HA is really not working for me, can anyone advise what I’m doing wrong please?

Right now I think I have it set to:

  • Wait for no movement on my motion sensors
    AND
  • Wait for my Nest Protects to show no room occupancy
    AND
  • Wait for my SmartThings Goodnight virtual Switch to be off (it’s inversed)
    AND
  • Wait for the time to be after 20:57

Despite all the above, everything turns off at 20:57.
I’m trying to retire SmartThings, but the Goodmorning, Goodnight and alarm functions are things I’m struggling to workout in HA.

Any help on what I’ve done wrong would be greatly appreciated.

Thanks!

You have to have your sensors as conditions as well, otherwise this will fire when any sensor changes state and time is after 20:57.

Sorry if I’m wrong; I’ve never used the HA Automation interface.
But, if I tried to reproduce your automation in NodeRed, it would not function since the logic and/or order is flawed. Currently, you have one condition and three distinct triggers. Hence, the automation will just fire if any of those triggers activate after 20:57.

So, right now I think you have it set to:

Wait for no movement on my motion sensors
OR
Wait for my Nest Protects to show no room occupancy
OR
Wait for my SmartThings Goodnight virtual Switch to be off (it’s inversed)
AND
Wait for the time to be after 20:57

Simply choose one of those to serve as the trigger, and the others as conditions. I don’t know what the most logical trigger would be in your setup as I don’t know what the switch does, but yeah. I hope you get what I mean :smiley: (I’m not a native speaker)

Thanks so just to clarify, the sensors are the triggers AND the conditions right?

Yes, but in the conditions you just have to check that they are in the off state (not for 15 minutes) but in the trigger you need the time delay.

Post your automation as formatted YAML (not a screenshot).

Reference: FAQ guideline 11 - Format it properly

Good thinking, here we go:

alias: (HA) Goodnight Automation
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.utility_room_motion_sensor_motion
      - binary_sensor.motion_sensor_dining_room_motion
      - binary_sensor.motion_sensor_garage_loft_motion
      - binary_sensor.motion_sensor_pool_house_motion
      - binary_sensor.motion_sensor_downstairs_motion
      - binary_sensor.motion_sensor_kitchen_motion
      - binary_sensor.motion_sensor_living_room_motion
      - binary_sensor.motion_sensor_garage_motion
    to: "off"
    for:
      hours: 0
      minutes: 15
      seconds: 0
    enabled: true
  - platform: state
    entity_id:
      - binary_sensor.nest_protect_kitchen_occupancy
      - binary_sensor.nest_protect_living_room_occupancy
      - binary_sensor.nest_protect_hallway_occupancy
      - binary_sensor.nest_protect_utility_room_occupancy
      - binary_sensor.nest_protect_office_occupancy
    to: "off"
    for:
      hours: 0
      minutes: 20
      seconds: 0
    enabled: true
  - platform: state
    entity_id:
      - switch.ha_power_down_v_switch
    to: "off"
condition:
  - condition: and
    conditions:
      - condition: time
        after: "22:30:00"
        before: "06:30:00"
        weekday:
          - mon
          - tue
          - wed
          - thu
          - fri
          - sat
          - sun
action:
  - service: notify.notify
    data:
      title: Home Power Alert
      message: Goodnight! Powering down switches and lights for the night.
  - type: turn_off
    device_id: edited
    entity_id: light.lights_porch
    domain: light
  - type: turn_off
    device_id: edited
    entity_id: switch.nspanelgarage_relay_1
    domain: switch
  - type: turn_off
    device_id: edited
    entity_id: switch.nspanelgarage_relay_2
    domain: switch
  - type: turn_off
    device_id: edited
    entity_id: light.christmas_tree
    domain: light
  - type: turn_off
    device_id: edited
    entity_id: switch.lounge_wal
    domain: switch
  - type: turn_off
    device_id: edited
    entity_id: switch.light_garden_wall_led_strip
    domain: switch
  - delay:
      hours: 0
      minutes: 3
      seconds: 0
      milliseconds: 0
  - service: switch.turn_off
    data: {}
    target:
      entity_id: switch.nspanel_garage_relay_1
mode: single

I think what I am trying to achieve is have all my triggers have an “AND” instruction between them, right now I think they’ve defaulted to an “OR” instruction.

Create a Group Binary Sensor containing the 8 motion detectors. For example purposes, let’s say you name it binary_sensor.all_motion.

Create another Group Binary Sensor containing all five occupancy detectors (called binary_sensor.all_occupancy).

Use the following automation:

alias: (HA) Goodnight Automation
description: ""
trigger:
  - platform: state
    entity_id: binary_sensor.all_motion
    to: "off"
    for:
      minutes: 15
  - platform: state
    entity_id: binary_sensor.all_occupancy
    to: "off"
    for:
      minutes: 20
  - platform: state
    entity_id: switch.ha_power_down_v_switch
    to: "off"
condition:
  - condition: time
    after: "22:30:00"
    before: "06:30:00"
  - condition: state
    entity_id: binary_sensor.all_motion
    state: "off"
  - condition: state
    entity_id: binary_sensor.all_occupancy
    state: "off"
  - condition: state
    entity_id: switch.ha_power_down_v_switch
    state: "off"
action:
  - service: notify.notify
    data:
      title: Home Power Alert
      message: Goodnight! Powering down switches and lights for the night.
  - service: homeassistant.turn_off
    target:
      entity_id:
        - light.lights_porch
        - switch.nspanelgarage_relay_1
        - switch.nspanelgarage_relay_2
        - light.christmas_tree
        - switch.lounge_wal
        - switch.light_garden_wall_led_strip
  - delay:
      minutes: 3
  - service: switch.turn_off
    target:
      entity_id: switch.nspanel_garage_relay_1
mode: single

Try that and let me know if it needs further refinement. For example, I would add a Time Trigger at 22:30. If there’s already no motion or occupancy prior to 22:30 (i.e. no one is home or everyone went to bed early), at 22:30 the automation will turn off the specified lights and switches.

1 Like

Thanks @123 I’ll give this a go tomorrow.
Question though, does your yaml contain the “OR” command I’m looking for?
If so, can I not just format my original automation to include it too?

Not sure what or command you mean but what Taras posted will work for you as far as I can understand from your description.

But… It’s time to get rid of the Christmas tree. It’s mid March now

1 Like

LOOOOOOOOOL!!!
I was hoping no one would notice that!!!

Multiple triggers are, by default, logically ORed. They can’t be logically ANDed because that would require two events to occur at the exact same moment in time (i.e. front door AND back door closing at the exact same time).

Multiple conditions are, by default, logically ANDed. They can be also be ORed or a combination of ANDed and ORed.

Hi @123 I just wanted to say thank you, this worked!

Logically speaking, I don’t get why my original one didn’t work though, but at least it’s working now.
I’ve also learnt about group binary sensors which I’ll now use elsewhere.

Thanks again!

1 Like