Motion controlled light with sun condition

Hi had a motion sensor from my security system set up to open staircase light when it sees motion.
Was working perfectly and had other automation to close after 2 minutes also working great.

Decided to combine the two and at the same time if I open the light manually and don’t pass in front of motion detector light stays on which is ok…

Read up and did the following automation.
It works but when I add the sunset sunrise so it only works after dusk and before dawn it doesn’t work any longer.
Surely an easy fix

  alias: Motion controlled Staircase
  description: ''
  trigger:
  - platform: state
    entity_id: binary_sensor.motion_basement
    to: 'on'
  - platform: state
    entity_id: binary_sensor.motion_basement
    to: 'off'
    for: 0:02:00
  condition:
  - condition: template
    value_template: "{{ (trigger.to_state.state == 'on' and is_state('switch.basement_staircase',\
      \ 'off')) or\n   (trigger.to_state.state == 'off' and is_state('switch.basement_staircase',\
      \ 'on')) }}\n"
  - condition: sun
    before: sunrise
    before_offset: -00:10:00
    after: sunset
    after_offset: -00:30:00
  action:
  - service: homeassistant.turn_{{ trigger.to_state.state }}
    entity_id: switch.basement_staircase
  mode: single

If I do this it works but 24hrs


  alias: Motion controlled Staircase
  description: ''
  trigger:
  - platform: state
    entity_id: binary_sensor.motion_basement
    to: 'on'
  - platform: state
    entity_id: binary_sensor.motion_basement
    to: 'off'
    for: 0:02:00
  condition:
  - condition: template
    value_template: "{{ (trigger.to_state.state == 'on' and is_state('switch.basement_staircase',\
      \ 'off')) or\n   (trigger.to_state.state == 'off' and is_state('switch.basement_staircase',\
      \ 'on')) }}\n"
  action:
  - service: homeassistant.turn_{{ trigger.to_state.state }}
    entity_id: switch.basement_staircase
  mode: single

how to add sun condition?

One issue may be with “before sunrise” and “after sunset”. The two end up being mutually exclusive for the same day. You could use the sun’s elevation with negative numbers meaning below the horizon. You could do that with one condition.

ok tried this and seems to work.
Changed offset to plus 3hrs after sunset to test and didn’t go on so all good
will see tomorrow morning to make sure
thanks

  alias: Motion controlled Staircase
  description: ''
  trigger:
  - platform: state
    entity_id: binary_sensor.motion_basement
    to: 'on'
  - platform: state
    entity_id: binary_sensor.motion_basement
    to: 'off'
    for: 0:02:00
  condition:
  - condition: template
    value_template: "{{ (trigger.to_state.state == 'on' and is_state('switch.basement_staircase',\
      \ 'off')) or\n   (trigger.to_state.state == 'off' and is_state('switch.basement_staircase',\
      \ 'on')) }}\n"
  - condition: or
    conditions:
    - condition: sun
      after: sunset
      after_offset: -01:00:00
    - condition: sun
      before: sunrise
      before_offset: -02:00:00
  action:
  - service: homeassistant.turn_{{ trigger.to_state.state }}
    entity_id: switch.basement_staircase
  mode: single

Ok so above was working great but security motion sensor went dead so service from ADT replaced the sensor. Problem is it has an 8 second delay before it triggers so worthless for what I was doing.
So decided to install zwave on HA with zooz 4 in 1 sensor

Modified to this, and it isn’t working.

  alias: Motion controlled Staircase
  description: ''
  trigger:
  - platform: state
    to: '8'
    entity_id: sensor.zooz_sensor_basement_staircase_burglar_2
  - platform: state
    to: '0'
    for: 0:02:00
    entity_id: sensor.zooz_sensor_basement_staircase_burglar_2
  condition:
  - condition: template
    value_template: "{{ (trigger.to_state.state == 'on' and is_state('switch.basement_staircase',\
      \ 'off')) or\n   (trigger.to_state.state == 'off' and is_state('switch.basement_staircase',\
      \ 'on')) }}\n"
  - condition: or
    conditions:
    - condition: sun
      after: sunset
      after_offset: -01:00:00
    - condition: sun
      before: sunrise
      before_offset: -02:00:00
  action:
  - service: homeassistant.turn_{{ trigger.to_state.state }}
    entity_id: switch.basement_staircase
  mode: single

thought It was the on off in template so replaced to 8,0 like this and still isn’t working.


  alias: Motion controlled Staircase
  description: ''
  trigger:
  - platform: state
    to: '8'
    entity_id: sensor.zooz_sensor_basement_staircase_burglar_2
  - platform: state
    to: '0'
    for: 0:02:00
    entity_id: sensor.zooz_sensor_basement_staircase_burglar_2
  condition:
  - condition: template
    value_template: "{{ (trigger.to_state.state == '8' and is_state('switch.basement_staircase',\
      \ 'off')) or\n   (trigger.to_state.state == '0' and is_state('switch.basement_staircase',\
      \ 'on')) }}\n"
  - condition: or
    conditions:
    - condition: sun
      after: sunset
      after_offset: -01:00:00
    - condition: sun
      before: sunrise
      before_offset: -02:00:00
  action:
  - service: homeassistant.turn_{{ trigger.to_state.state }}
    entity_id: switch.basement_staircase
  mode: single

tried a simple automation like this and light turns on with motion
so how can I add like top automation delay and sunrise sunset


  alias: test turn on staircase light after motion
  description: ''
  trigger:
  - platform: state
    entity_id: sensor.zooz_sensor_basement_staircase_burglar_2
    to: '8'
  condition: []
  action:
  - service: switch.turn_on
    data: {}
    entity_id: switch.basement_staircase
  mode: single

anyone could help?

Where are you finding the states (as in ‘0’ or ‘8’)? I would have expected a multi-sensor to have the same motion states as your original. (The Aeon Multisensor 6 I have does.)

thanks for the reply. here is an extract from the zooz sight

Once the sensor is added, note that it won’t report motion via binary sensor. It will rather report motion via burglar notifications where values 0 and 8 translate to no motion/motion. Here’s more about this from the HA community.

I see.
One possibility is to create a binary sensor (in configuration.yaml if you haven’t split that yet) from that as follows:

binary_sensor:
  - platform: template
    sensors:
      motion_basement:
        friendly_name: 'MotionBasement'
        device_class: motion
        value_template: "{{states('sensor.zooz_sensor_basement_staircase_burglar_2')|float > 0}}"

Then you can use the binary sensor state in your automation. (State would by on/off true/false).
Alternately, you could use something like this as the automation:

 alias: test turn on staircase light after motion
  description: ''
  trigger:
  - platform: numeric_state
    entity_id: sensor.zooz_sensor_basement_staircase_burglar_2
    above: 0
  condition: []
  action:
  - service: switch.turn_on
    data: {}
    entity_id: switch.basement_staircase
  mode: single

ok thanks finally went with two step option
one for turning on and one to turn off
For some reason the one combining does not work
but all good just have two entries instead of one.

what works

  alias: turn on staircase basement motion
  trigger:
  - platform: state
    to: '8'
    entity_id: sensor.zooz_sensor_basement_staircase_burglar
  condition:
  - condition: or
    conditions:
    - condition: sun
      after: sunset
      after_offset: -00:10:00
    - condition: sun
      before: sunrise
      before_offset: -01:00:00
  action:
  - service: homeassistant.turn_on
    entity_id: switch.basement_staircase
  mode: single
- id: 00c428acda0b4b7295fb524003412708
  alias: staircase basement OFF after 1 mins
  trigger:
  - platform: state
    entity_id: sensor.zooz_sensor_basement_staircase_burglar
    to: '0'
    for: 0:01:00
  action:
  - service: homeassistant.turn_off
    entity_id: switch.basement_staircase
  mode: single

what doesn’t


- id: '1607377170368'
  alias: Motion controlled Staircase
  description: ''
  trigger:
  - platform: state
    entity_id: sensor.zooz_sensor_basement_staircase_burglar
    to: '8'
  - platform: state
    to: '0'
    for: 0:02:00
    entity_id: sensor.zooz_sensor_basement_staircase_burglar
  condition:
  - condition: template
    value_template: "{{ (trigger.to_state.state == 'on' and is_state('switch.basement_staircase',\
      \ 'off')) or\n   (trigger.to_state.state == 'off' and is_state('switch.basement_staircase',\
      \ 'on')) }}\n"
  - condition: or
    conditions:
    - condition: sun
      after: sunset
      after_offset: -01:00:00
    - condition: sun
      before: sunrise
      before_offset: -02:00:00
  action:
  - service: homeassistant.turn_{{ trigger.to_state.state }}
    entity_id: switch.basement_staircase
  mode: single

Hi, @aqus555 - I suggest testing all the templates in the developer/template tool to make sure they are doing exactly what you think they are doing. The problem with that, of course, is that the trigger.xxx won’t have a value in the template tool. Which means you have to replace those with the actual entity. For example, replace:

trigger.to_state.state

with

states('sensor.zooz_sensor_basement_staircase_burglar')

Reading through the forums on using sunset and sunrise in a condition together seems to be less than intuitive, mostly because it doesn’t seem to consider what happens at midnight - they only work for the same day and don’t consider the next day (although the or condition should work - that’s weird).

I got around all this by creating a time-of-day binary_sensor. The time-of-day platform is smart enough to recognize that if after is sunset and before is sunrise, then it’s obvious the before: sunrise means the next day.

Here’s what I use (modified for your offsets) - then you can simply write the condition around this binary_sensor:

- platform: tod
  name: lights allowed on
  after: sunset
  after_offset: '-01:00:00'
  before: sunrise
  before_offset: '02:00:00'

The condition then becomes (note: I took the liberty of simplifying the syntax in your existing condition template - you don’t need the escape characters):

  condition:
  - condition: template
    value_template: >
      {{ (trigger.to_state.state == 'on'  and is_state('switch.basement_staircase','off') or
          trigger.to_state.state == 'off' and is_state('switch.basement_staircase','on')) and
          is_state('binary_sensor.lights_allowed_on','on') }}

Hope this helps!

Other than this, is your experience with the zooz sensor a positive one? I’m looking for zwave sensors that will work with an Aotec stick.

Thanks KFC for the info. I will try the mods when I have a little time.
As for the Zooz 4 in one sensor it is working great for me. I have two installed and the sensor picks up movement and HA triggers set automation about 1/2 second later so works very well and so far consistent.

Thanks! That sounds perfect for a few areas I’m looking at.