Newbie needs help with Hue Motion Sensor (daytime: no trigger / sunset - midnight: trigger, 100% / midnight - sunrise: trigger, 10%)

Hey there,
currently im struggling to find a way to turn on the lights using hue motion sensors, but only in the time between sunset and sunrise, and from midnight to sunrise the light should be dimmed to XX%

Short version:
daytime: no trigger
sunset - midnight: trigger, 100%
midnight - sunrise: trigger, 10%

Would be really helpful to get some hints on how to extend the stock “Motion-activated Light” automation with my conditions, since I still struggle with the syntax in yaml-files.

THANKS!

You can use the gui, why would you want this in yaml?

  1. trigger on motion sensor
  2. add action ‘choose’
    a. Option 1: condition after sunset and time < 23:25 …action: device … light.abc, briogthness 100
    b. Option 2: condition: before sunrise and time > 0:00 …action: device … light.abc, briogthness 10
1 Like

Here’s what I suggest you do:

You need to set up the trigger, condition and actions of an automation as follows:

The trigger needs to be that your hue motion sensor detects motion.

You need a condition that the sun is below the horizon. This will restrict your automation to only run between sunset and sunrise.

You need an if/then action to differentiate between before and after midnight:

The condition for the if/then can be that the time is after 12 noon. If you omit a before time then this will span noon to midnight.

You can then put your different lighting actions in the Then and Else parts.

Of course, this won’t turn the lights off but, you didn’t mention this.

1 Like

thanks for the help. I guess I’m coming up much with yaml configuration cause you find many people doing it that way, I didn’t know the GUI is that mighty. Then many of my problems won’t be one anymore.

One question left: If i choose the device “motion sensor” and use as a trigger “detect motion”, can I add a second trigger with “no motion detected” in the same automation to turn the lamps off again?

Yes you can.

The way I normally do this is:

Give each trigger an ID. You’ll find an ‘Edit id’ option on the 3-dot menu.

Then use the Choose action to pick between the different triggers. There is a ‘triggered by’ condition that tests for a particular trigger id.

You’ll then need to transplant your current actions into the relevant option of the Choose action.

Okay, I’ve tried many different things but I can’t get it to work like I want to. I try to put it just into words and write my yaml after, maybe someone of you can fix it and tell me what I did wrong. I’ve used the virtual editor for this. thanks alot for your help!

  • after sunset until midnight turn on the light 100%
  • after midnight until 3am turn on the light 30%
  • turn the light always on when the contactsensor of the door opens
  • turn the light always off after 30 seconds

alias: Flur Test
description: ""
trigger:
  - type: motion
    platform: device
    device_id: 6ea74b0cfb3f98475c08b5057f3165f9
    entity_id: binary_sensor.flur_hue_bm_motion
    domain: binary_sensor
condition: []
action:
  - if:
      - condition: sun
        after: sunset
      - condition: time
        before: "00:00:00"
        weekday: []
      - condition: or
        conditions:
          - type: is_open
            condition: device
            device_id: f3d4672d01b877942530f0b130c2c65a
            entity_id: binary_sensor.lumi_lumi_sensor_magnet_aq2_opening
            domain: binary_sensor
    then:
      - type: turn_on
        device_id: 67b8a1285acb5b90e92122afb346cc09
        entity_id: light.flur_hue_ww
        domain: light
        brightness_pct: 100
    else:
      - if:
          - condition: time
            after: "00:00:00"
            before: "03:00:00"
        then:
          - type: turn_on
            device_id: 67b8a1285acb5b90e92122afb346cc09
            entity_id: light.flur_hue_ww
            domain: light
            brightness_pct: 30
        else:
          - delay:
              hours: 0
              minutes: 0
              seconds: 30
              milliseconds: 0
          - type: turn_off
            device_id: 67b8a1285acb5b90e92122afb346cc09
            entity_id: light.flur_hue_ww
            domain: light
mode: single

Try this one:


alias: Flur Test
description: ""
trigger:
  - platform: state
    entity_id: binary_sensor.flur_hue_bm_motion
    from: "off"
    to: "on"
condition: []

action:
  - choose:
      - conditions:
          - condition: or
            conditions:
              - condition: state
                entity_id: binary_sensor.lumi_lumi_sensor_magnet_aq2_opening
                state: "on"
              - condition: and
                conditions:
                  - condition: sun
                    after: sunset
                  - condition: time
                    before: "00:00:00"
        sequence:
          - service: light.turn_on
            target:
              entity_id: light.flur_hue_ww
            data:
              brightness_pct: 100

      - conditions:
          - condition: time
            after: "00:00:00"
            before: "03:00:00"
        sequence:
          - service: light.turn_on
            target:
              entity_id: light.flur_hue_ww
            data:
              brightness_pct: 30

  - delay: 30

  - service: light.turn_off
    target:
      entity_id: light.flur_hue_ww

A shorter version would be:


alias: Flur Test
description: ""

variables:
  sunset: >-
    {{ (state_attr('sun.sun',
    'next_setting')|as_datetime|as_local).strftime('%H:%M:%S') }}
  midnight: "{{ today_at('00:00:00') }}"
  evening: "{{ today_at(sunset) < now() < midnight }}"
  morning: "{{ now() < today_at('03:00:00') }}"
  door: "{{ states('binary_sensor.lumi_lumi_sensor_magnet_aq2_opening') == 'on' }}"

trigger:
  - platform: state
    entity_id: binary_sensor.flur_hue_bm_motion
    from: "off"
    to: "on"
action:
  - if:
      - condition: template
        value_template: "{{ evening or morning or door }}"
    then:
      - service: light.turn_on
        data:
          brightness_pct: |-
            {% if evening or door %}100
            {% else %}30
            {% endif %}
        target:
          entity_id: light.flur_hue_ww
      - delay: 30
      - service: light.turn_off
        target:
          entity_id: light.flur_hue_ww
    else:
      - stop: Bedingungen nicht erfüllt

1 Like

Thanks for your answer. I’ve just tried the long and short version, both aren’t working. In the automation tab I see the long version gets triggered, but the light doesn’t turn on. I’ve looked up the entity sun.sun, which says “under the horizont”. I’ve also removed the part with getting triggered after sunset, still no lights with motion detection, nor door opening…

In the second automation I’ve mixed up the trigger binary sensor with the door sensor. I’ve corrected it.

The first automation could be blocked by the time condition “00:00:00”.

EDIT:

The mixed or/and conditions were not ok. It has to be:


      - conditions:
          - condition: or
            conditions:
              - condition: state
                entity_id: binary_sensor.lumi_lumi_sensor_magnet_aq2_opening
                state: "on"
              - condition: and
                conditions:
                  - condition: sun
                    after: sunset
                  - condition: time
                    before: "00:00:00"

1 Like

Thanks for your edits, tried those aswell and it seems to work… ALMOST. The silly door sensor doesn’t seem to trigger the light. I’ve been toodeling around all day already and ended running two automations, one for the motion sensor and a second one for the contact sensor. Not the best solution, but I can’t find a better way right now.

The following I’ve come up with:

alias: Flur Test
description: ""
trigger:
  - type: motion
    platform: device
    device_id: 6ea74b0cfb3f98475c08b5057f3165f9
    entity_id: binary_sensor.flur_hue_bm_motion
    domain: binary_sensor
condition:
  - condition: sun
    after: sunset
    before: sunrise
action:
  - if:
      - condition: time
        before: "23:59:00"
        after: "16:00:00"
    then:
      - service: light.turn_on
        data:
          brightness_pct: 100
        target:
          entity_id: light.flur_hue_ww
      - if:
          - condition: state
            entity_id: binary_sensor.flur_hue_bm_motion
            state: "off"
            for:
              hours: 0
              minutes: 0
              seconds: 30
        then:
          - service: light.turn_off
            data: {}
            target:
              entity_id: light.flur_hue_ww
        else: []
    else:
      - service: light.turn_on
        data:
          brightness_pct: 30
        target:
          entity_id: light.flur_hue_ww
      - if:
          - condition: state
            entity_id: binary_sensor.flur_hue_bm_motion
            state: "off"
            for:
              hours: 0
              minutes: 0
              seconds: 30
        then:
          - service: light.turn_off
            data: {}
            target:
              entity_id: light.flur_hue_ww

EDIT
To explain what I’ve tried to do… The light acted very stiff. So once it gets triggered it stayed on for 30 seconds and went off, ignoring if someone is still around or not. Thats why I’ve added the “binary_sensor” state “off” for 30 secs.

alias: Flur Test
description: ""
trigger:
  - platform: state
    entity_id: binary_sensor.flur_hue_bm_motion
    from: 'off' 
    to: 'on'
  - platform: state
    entity_id: binary_sensor.flur_hue_bm_motion
    from: 'on' 
    to: 'off'
    for: '00:00:30'
condition:
  - condition: sun
    after: sunset
    before: sunrise
action:
  - service: light.turn_on
    target:
      entity_id: light.flur_hue_ww
    data:
      brightness_pct: "{{ 0 if trigger.to_state.state == 'off' else 100 if now().hour >= 16 else 30 }}"

EDIT

Corrected State Trigger.

Thanks for your code. It took a while for me to understand whats happening in the last line, but I got it. So basically it waits for the binary_sensor state to change from on to off, else it goes 100%, if later than 16:00 it goes 30%. I’ll give it a try, tbh I liked previous solutions better cause I could understand them easier - tho this is the cleanest way to write it

u guys are awesome

Your requirements are to have a motion detector turn on a light the moment when motion is detected and turn it off if no motion is detected for at least 30 seconds. The only added twist is that when it turns on the light, it sets brightness to less than 100% if the current time is before 16:00.

The automation I posted meets those requirements and works like this:

  • It’s triggered by the binary sensor’s state changing to on or to off.
  • The condition confirms the current time is between sunset and sunrise.
  • The action is simply one service call:
    • If the binary sensor’s state changed to off it turns off the light by setting brightness_pct to zero.
    • If the binary sensor’s state changed to on it turns on the light and sets brightness_pct to either 100 (if the current hour is greater than or equal to 16) or 30.
1 Like

Thanks for the explanation, that really helped me since I’m new to HA. Does it make sense to implement the door sensor in it or is it fine to run two automations for that? I don’t know if they will interrupt each other.

Like:
door sensor open = light 100% if time inbetween sunset and midnight, turn off after 30 secs if motion = off

If the door sensor is supposed to control the light in the same way as the motion sensor, you can add simply add it to the first State Trigger.

  - platform: state
    entity_id:
      - binary_sensor.flur_hue_bm_motion
      - binary_sensor.lumi_lumi_sensor_magnet_aq2_opening
    from: 'off' 
    to: 'on'

You don’t have to add the door sensor to the second State Trigger (unless you also want it to turn off the light after the door is closed for 30 seconds) because the motion sensor can take care of doing that on its own.


EDIT

Corrected State Trigger.

I just tried saving your code and it says

Message malformed: extra keys not allowed @ data[‘target’]

Sorry about that; that’s due to my mistake. I corrected both of the examples I posted above.