Lights on motion & at night

Hi all, tried a few things but can’t get this to work. One of my automations below with lights on motion works fine (Alias - Turn on outside front lights when motion is detected) now I want to add a condition with time, want it to only trigger between 7pm and 6am, how do I add this condition?

I have a couple of other issues, e.g. notifications to WebOS doesn’t work, and after adding HomeBridge and integrating Apple HomeKit, my home automation controls / buttons on the Home Assistant GUI (Automation tab) don’t do anything, they’ll trigger even when I turn them off. This is another issue I can deal with later.

Your help here is much appreciated.

  • alias: iOS notification when motion is detected
    trigger:
    platform: state
    entity_id: sensor.aeotec_zw100_multisensor_6_burglar
    from: ‘0’
    to: ‘8’
    action:
    service: notify.ios_ashors_iphone
    data:
    message: “Motion detected outside front door!”
  • alias: webOS notification when motion is detected
    trigger:
    platform: state
    entity_id: sensor.aeotec_zw100_multisensor_6_burglar
    from: ‘0’
    to: ‘8’
    action:
    service: notify.living_room_tv
    data:
    message: “Motion detected outside front door!”
  • alias: Turn on outside front lights when motion is detected
    trigger:
    platform: state
    entity_id: sensor.aeotec_zw100_multisensor_6_burglar
    from: ‘0’
    to: ‘8’
    action:
    service: homeassistant.turn_on
    entity_id: switch.aeotec_zw132_dual_nano_switch_switch
  • alias: Turn off outside front lights 5 minutes after last movement
    trigger:
    platform: state
    entity_id: sensor.aeotec_zw100_multisensor_6_burglar
    from: ‘0’
    to: ‘0’
    for:
    minutes: 5
    action:
    service: homeassistant.turn_off
    entity_id: switch.aeotec_zw132_dual_nano_switch_switch

Your configuration is unreadable, please use the editor’s “pre-formatted” code block.

In general, to make an automation’s action happen only during a given timeframe, add a time condition to it.

trigger:
  ....
condition:
  condition: time
  after: '19:00:00'
  before: '06:00:00'
action:
  ....

Alternatively, you could use the ‘sun’ component and it’s provided condition, so the action happens only during darkness (from dusk 'till dawn):

trigger:
  ....
condition:
  condition: or  # 'when dark' condition: either after sunset or before sunrise
  conditions:
    - condition: sun
      after: sunset
    - condition: sun
      before: sunrise
action:
  ...

Just to show there’s more than one way to do things in HA :slight_smile:
Here’s an example using the action block to do things based on time as well:

  action:
    - service: scene.turn_on
      data_template:
        entity_id: >
          {% if now().hour >= 20 or now().hour <= 7 %}
            scene.hallway_nightlight
          {% else %}
            scene.hallway_bright
          {% endif %}

to clarify a post above…

yaml is extremely syntax dependent. Spacing and indentation is important.

the only real way to help most of the time is to see your code in the format that you typed it into your configuration files. the best way to do that is when you copy your code to the editor, select it all and then click the pre-formatted text button ( </> ) at the top of the editor window.

- alias: iOS notification when motion is detected
  trigger:
    platform: state
    entity_id: sensor.aeotec_zw100_multisensor_6_burglar
    from: '0'
    to: '8'
  action:
    service: notify.ios_ashors_iphone
    data:
      message: "Motion detected outside front door!"
- alias: webOS notification when motion is detected
  trigger:
    platform: state
    entity_id: sensor.aeotec_zw100_multisensor_6_burglar
    from: '0'
    to: '8'
  action:
    service: notify.living_room_tv
    data:
      message: "Motion detected outside front door!"
- alias: Turn on outside front lights when motion is detected
  trigger:
    platform: state
    entity_id: sensor.aeotec_zw100_multisensor_6_burglar
    from: '0'
    to: '8'
  action:
    service: homeassistant.turn_on
    entity_id: switch.aeotec_zw132_dual_nano_switch_switch
- alias: Turn off outside front lights 5 minutes after last movement
  trigger:
    platform: state
    entity_id: sensor.aeotec_zw100_multisensor_6_burglar
    from: '0'
    to: '0'
    for:
      minutes: 5
  action:
    service: homeassistant.turn_off
    entity_id: switch.aeotec_zw132_dual_nano_switch_switch

Hey guys, did anyone get a chance to look at this