First automation re: z-wave PIR and z-wave dimmer

OK,
Dipping my toe into Home Assistant from Domoticz.

I have moved 2 z-wave devices over to home assistant, a z-wave motion sensor and a z-wave fibaro dimmer 2 relay.
What i need is to sort out an automation where 30 mins after sunset and 30 minutes before, if motion is detected on the PIR, activated the dimmer relay at 5% power and keep that on until motion has stopped for 2 minutes.

How would i achieve this in home assistant?
I’m not seeing any examples of this with light levels, i am seeing on and off.

Must say, i have only moved over 2 nodes so far to the z-wave and its so smooth, so far!
Stil have 90 odd nodes to move over, if i get this automation working well.

Just to add, i have got it working with the UI, but without the lower light level and sunrise/sunset,using 2 automations, one for on, one for off.

> id: '1582130344989'
>   alias: Landing Light PIR
>   description: ''
>   trigger:
>   - device_id: e547897571e940358b60a35026a41d37
>     domain: binary_sensor
>     entity_id: binary_sensor.landingmotion_sensor
>     for:
>       hours: 0
>       minutes: 0
>       seconds: 0
>     platform: device
>     type: turned_on
>   condition: []
>   action:
>   - device_id: 0c747cd2dd1a49efa789ca400a11afe4
>     domain: light
>     entity_id: light.landinglight_level_5
>     type: turn_on
> - id: '1582132368701'
>   alias: Landing light off after motion passed
>   description: ''
>   trigger:
>   - device_id: e547897571e940358b60a35026a41d37
>     domain: binary_sensor
>     entity_id: binary_sensor.landingmotion_sensor
>     for:
>       hours: 0
>       minutes: 1
>       seconds: 0
>     platform: device
>     type: turned_off
>   condition: []
>   action:
>   - device_id: 0c747cd2dd1a49efa789ca400a11afe4
>     domain: light
>     entity_id: light.landinglight_level_5
>     type: turn_off

So, you trigger is motion detection, your conditions are the time window though, I’d suggest not a fixed time offset, but a sun elevation. The elevation is consistent for light levels through the year.

This cookbook example and this article will be useful

One boolean to indicate you’re controlling the light with the automation:

input_boolean
  motion_light:
    initial: off

One automation to turn the light on, if it’s off:

  - alias: "Motion causes a dim light"
    trigger:
      - platform: state
        entity_id: binary_sensor.your_motion_sensor_here
        to: 'on'
    condition:
      # Only turn the light on if it's already off
      - condition: state
        entity_id: light.your_light_here
        state: 'off'
      # Require the sun elevation to be between -3 and +3 degrees - tune to suit
      - condition: numeric_state
        entity_id: sun.sun
        above: -3
        below: 3
        value_template: '{{ state_attr("sun.sun", "elevation") }}'
    action:
      # Turn on the boolean to show we're controlling the light with the automation
      - service: input_boolean.turn_on
        entity_id: input_boolean.motion_light
      # Turn on the light at 5%
      - service: light.turn_on
        data:
          entity_id: light.your_light_here
          brightness_pct: 5

One to turn it off after two minutes:

  - alias: "No motion brings the darkness back"
    trigger:
      - platform: state
        entity_id: binary_sensor.your_motion_sensor_here
        to: 'off'
        for: '00:02:00'
    condition:
      # Only turn it off if it's on
      - condition: state
        entity_id: light.your_light_here
        state: 'on'
      # Only turn it off if the automation turned it on
      - condition: state
        entity_id: input_boolean.motion_light
        state: 'on'
    action:
      - service: light.turn_off
        data:
          entity_id: light.your_light_here

And, finally, one to turn off the input boolean when the light turns off:

  - alias: "If the light is off, so is the magic"
    trigger:
      - platform: state
        entity_id: light.your_light_here
        to: 'off'
    condition:
      # Harmless to not have the condition check
      - condition: state
        entity_id: input_boolean.motion_light
        state: 'on'
    action:
      - service: input_boolean.turn_off
        entity_id: input_boolean.motion_light

You can build in other logic, like an automation to detect if the light was turned above 5% brightness and then turn off the boolean too:

  - alias: "Turn it up and I turn it off"
    trigger:
      - platform: numeric_state
        entity_id: light.your_light_here
        # 13 is roughly 5% of 255, the maximum brightness
        above: 14
        value_template: '{{ state_attr("light.your_light_here", "brightness") }}'
    condition:
      - condition: state
        entity_id: input_boolean.motion_light
        state: 'on'
    action:
      - service: input_boolean.turn_off
        entity_id: input_boolean.motion_light

Please see the sticky post on, amongst other things, how to format your code so that it’s readable :wink: