Can anyone tell me why this automation doesnt work

Hi all, new to HA - using a PI4 and got it all working, dashboard created.

Had my combi zigbee stick delivered today and play around with automations

Ive added a smartthings motion sensor and all I want to do is a simple automation

motion detected turn light on (easy can do that)

where Im stuggling is getting further things to happen.

So ideally id like the light to turn when motion is detected after sunset and until sunrise and turn off if no motion is detected say after a minute. Id be over the moon if some one could help me figure out how to say dim the light to 60% after 10pm lol

I have tried to create this in the visual editor. Yaml code as follows

description: ""
trigger:
  - platform: sun
    event: sunset
    offset: 0
  - type: motion
    platform: device
    device_id: 7667cfee7c70159c4879bda5f3a778bd
    entity_id: be924323c013961b6cba9714e19d71ca
    domain: binary_sensor
condition: []
action:
  - type: turn_on
    device_id: f1e6d9fd56509eb7331be5689c38a458
    entity_id: c4170ef8ab9b859ddf03794d4a60ffb2
    domain: light
  - type: is_no_motion
    condition: device
    device_id: 7667cfee7c70159c4879bda5f3a778bd
    entity_id: be924323c013961b6cba9714e19d71ca
    domain: binary_sensor
    for:
      hours: 0
      minutes: 1
      seconds: 0
  - type: turn_off
    device_id: f1e6d9fd56509eb7331be5689c38a458
    entity_id: c4170ef8ab9b859ddf03794d4a60ffb2
    domain: light
mode: single```

the light comes on but doesnt switch off

what cant i see?

First off, I (and many others) would recommend against using device triggers/conditions/actions. There are several reasons but I won’t list them here but if you do a search you should be able to find a post about it.

you should try to use other triggers like state/numeric_state/etc. And services as actions.

And the logic you have (obviously) doesn’t work like you think it will.

the second action won’t sit around and wait for the 1 minute you have. As soon as the light gets turned on it will immediately check to see if the condition is met (that there has been no motion for a minute) and will take the next action ifd that check is true.

in your case it is impossible for the motion to trigger the light to turn on and then a millisecond or so later also there has been no motion for a minute.

There are a couple of ways to do what you want tho.

you can use two triggers (one for motion and one for no motion) and then choose which action to take based on which trigger happens.

Or you can use one trigger to turn on the light and then in the actions section after the light is switched on use a “wait_for_trigger” that will be no motion for 1 minute then turn off the light.

The first option is a bit more complicated but it’s more resilient than the wait option.

Since you didn’t give the entities I can give a basic outline of the first option but you’ll need to find your entity_id’s to replace the examples with:

trigger:
  - platform: state
    entity_id: binary_sensor.your_motion_sensor
    to: 'on'
    id: motion_on
  - platform: state
    entity_id: binary_sensor.your_motion_sensor
    to: 'off'
    for:
      minutes: 1
    id: motion_off
condition: 
  - condition: state
    entity_id: sun.sun
    state: 'below_horizon'
action:
  - choose:
      - conditions:
          - condition: trigger
            id: motion_on
        sequence:
          - service: light.turn_on
            entity_id: light.your_light
      - conditions:
          - condition: trigger
            id: motion_off
        sequence:
          - service: light.turn_off
            entity_id: light.your_light

that is the basic automation for turning the light on and off with motion and only between sunset and sunrise when the sun is below the horizon.

once you get that working then I’m sure someone can help you with the dimming part.

Thank you i have a lot to learn - coming from sharptools, im struggling to get my head round the automations…

I woll have a play around with triggers…

Thnx for the pointers

@finity forgot to mention to change

mode: single

into

mode: restart

So the light doesn’t keep going off and on when motion is detected during the 1 minute window…
When motion is detected within the one minute, it should stop the running action, and restart it :wink:
For that reason, I would also increase the time to 3-10 mins…but it depends a bit of the location of the detector :grin:

And yes… stay away from devices…try to use entities, it will libe a lot easier when something needs to be replaced :wink:

sorry where do I add this into the code?

I just tried what was mentioned above but the light didnt come on at all…

Try this one then:

alias: Light on Motion
description: Off 3 mins after on (and no motion)
trigger:
  - platform: state
    entity_id:
      - binary_sensor.your_motion_sensor
    from: "off"
    to: "on"
condition:
  - condition: state
    entity_id: sun.sun
    state: 'below_horizon'
action:
  - service: light.turn_on
    target:
      entity_id: light.your_light
    data: {}
  - delay:
      hours: 0
      minutes: 3
      seconds: 0
      milliseconds: 0
  - service: light.turn_off
    data: {}
    target:
      entity_id: light.your_light
mode: restart

ok I will, thank you

last question -

were exactly am i putting this?

on the yamal config file

or do I go into automations and edit in yaml and paste it in there

sorry if this is a really stupid q

sorry i didnt read yoyr full message you pre-empted my struggle :slight_smile:

you can just import this by going into automations,
Create a new automation, and then enter edit yaml mode (found in the 3 little dots in upper right corner)
Select all, and copy paste my code into it.
(but off coarse correct the sensor/light name 1st)
:wink:

1 Like

Yes, that works but was mentioned as my second option which is less resilient to restarts etc.

I assumed the cool-down period of the motion sensor was longer than 1 minute.

And it really doesn’t make any sense to add that the “light will be going off and on during that minute” when there is nothing in that minute to turn the light off until the minute expires.

so worst case is that the light keeps getting turned on. but since it’s already on no one should notice.

What exactly did you try?

you sir are a genius - i have no idea how folks on here get your head around this - if i had a hat on i would doff it in your direction.

Ill be in touch when im struggling with my next automations :slight_smile:

ofc you are correct, not during the window…

And yes, you could use the ‘off’ condition of the motion sensor…but them you also have to take that delay into account…and few of mine are triggered by mqtt, which doesn’t have an off state, they just trigger once :thinking:

Who is?

it is not that hard…and one does learn a few thing left and right over the last 4 years…just be consistent and persistent :wink:

1 Like

Haha well i apprecitste both your help

Last question i tried to follow logic and lets say after 10pm i wanted the light to dim so it didnt “blind” anyone walking over the landing

I tried this

description: Off 1 min after on (and no motion), dim to 40% after 10pm
trigger:
  - platform: state
    entity_id:
      - binary_sensor.landing_sensor_motion
    from: "off'
    to: "on"
condition:
  - condition: state
    entity_id: sun.sun
    state: below_horizon
action:
  - service: light.turn_on
    target:
      entity_id: light.landing_light
  - delay:
      hours: 0
      minutes: 1
      seconds: 0
      milliseconds: 0
  - service: light.turn_off
    target:
      entity_id: light.landing_light
  - condition: time
    after: '22:00:00'  # After 10pm
  - service: light.turn_on
    target:
      entity_id: light.landing_light
    data:
      brightness_pct: 40
mode: restart```

 But it still came on at 100%

Add a “choose”

description: Off 1 min after on (and no motion), dim to 40% after 10pm
trigger:
  - platform: state
    entity_id:
      - binary_sensor.landing_sensor_motion
    from: "off'
    to: "on"
condition:
  - condition: state
    entity_id: sun.sun
    state: below_horizon
action:
  - alias:”Between 22:00 and 06:00 turn on 40% otherwise 100%”
    choose:
    - conditions:
        - condition: time
          after: '22:00:00'  # After 10pm
          before: after: '06:00:00'  # Before 6am
      sequence:
        - service: light.turn_on
          data: 
            brightness_pct: 40
          target:
            entity_id: light.landing_light
    default:
    - service: light.turn_on
      data:
        brightness_pct: 100
      target:
        entity_id: light.landing_light
  - delay:
      hours: 0
      minutes: 1
      seconds: 0
      milliseconds: 0
  - service: light.turn_off
    target:
      entity_id: light.landing_light
mode: restart
1 Like
description: >-
  Turn on landing light before sunset, dim at 9:30 AM, and keep running if
  triggered again
trigger:
  - platform: state
    entity_id: binary_sensor.landing_sensor_motion
    from: "off"
    to: "on"
condition:
  - condition: state
    entity_id: sun.sun
    state: below_horizon
action:
  - service: light.turn_on
    entity_id: light.landing_light
    data:
      brightness_pct: 100
  - delay:
      minutes: 2
  - service: light.turn_off
    entity_id: light.landing_light
  - choose:
      - conditions:
          - condition: time
            after: "21:30:00"
            before: "07:30:00"
        sequence:
          - service: light.turn_on
            entity_id: light.landing_light
            data:
              brightness_pct: 30
  - delay:
      minutes: 1
  - service: light.turn_off
    entity_id: light.landing_light```
 

Light doesnt seem.ti be turning off post 9?

Erm…that doesn’t make sense…what are you trying to achieve???

Haha i think ive confused myself…

So essentially…what you helped me with before

What i wanted

50mins before sunset if the landing sensor detects motion turn on tbe landing light to 100% then turn off after 2 minutes. You added before a line to keep the automation running if motion is detected again…

Then i wanted between 2130 and 0730 if motiom was detected then turn the light in dimmed to 30% turn off after a minute. Automation stops at 730am

I could get it working but it would turn the dimmed light off post 2130

Am i better running 2 automations?

So what happens if the motion sensor never turns off because you are still in the area? If it never turns off because it is continuously detecting motion then it would never need to turn turn back on.

The way you have it programmed now after 2 minutes the lights turn off regardless of if motion is still being detected.