Automation triggering 12hours wrong

hey there,

So i have a light automation to turn on if there is motion in a room if its close to sunset… but for whatever reason after upgrade it triggers a full 12 hours wrong (almost like for sunrise instead of sunset)

- id: turn_office_light_on_night_motion
  alias: Office light on when motion at night
  trigger:
  - entity_id: sensor.office_motion
    platform: state
    to: detected
  - event: sunset
    offset: '-00:45:00'
    platform: sun
  condition:
  - before: sunset
    before_offset: '-01:00:00'
    condition: sun
  action:
  - entity_id: light.office_light_level
    service: light.turn_on

That is my above automation, any ideas?

The condition as written only allows the automation action to run if one of the triggers occurs at least one hour before sunset (i.e., anytime from midnight, through morning and day, up until one hour before sunset.)

The triggers cause the automation action to run if either motion is detected, or the time becomes 45 minutes before sunset, assuming, of course, for both triggers, the condition described above is satisfied.

So, basically, the “45 minutes before sunset” trigger won’t do anything, because that’s after one hour before sunset, not before.

That means, as written, the action will run whenever motion is detected anytime during the morning or day, as long as it happens before one hour before sunset. My guess is this is not what you want.

Can you elaborate on what you mean by “if there is motion in a room if its close to sunset”? If so, I should be able to recommend a modification to the automation that will work for you.

Ah ok, that makes sense.

What It was doing, and what my expected automation would be is:

If there is motion 45min before sunset, turn the light on (i think is the simplest explanation)

I think you need a range of time. Not very likely the motion will happen exactly at 45 minutes before sunset! :wink: So, the trigger should be motion detected, and the condition should specify the period of time that trigger will be allowed to cause the action to run. So when do you want that period of time to start and end?

So the period of time would be from 45min before sunset - 45min before sunrise I think

- id: turn_office_light_on_night_motion
  alias: Office light on when motion at night
  trigger:
  - entity_id: sensor.office_motion
    platform: state
    to: detected
  condition:
    condition: or
    conditions:
    - after: sunset
      after_offset: '-00:45:00'
      condition: sun
    - before: sunrise
      before_offset: '-00:45:00'
      condition: sun
  action:
  - entity_id: light.office_light_level
    service: light.turn_on

So the trigger and action sections should be obvious. The condition probably needs some explaining…

First, you want a time period that is between “around sunset” and “around sunrise.” But, with the sun condition, you can’t specify a period that spans midnight. If you said “after sunset and before sunrise”, it would always be false, because during a single day (midnight to midnight) there is never a time when it is both after sunset and before sunrise.

So, you have to actually specify two periods and the condition should be true if either of them are true. That’s where condition: or comes in. The first period is from 45 minutes before sunset to midnight (i.e., 45 minutes before sunset and after, which is why I use after: sunset and after_offset:), and the second period is from midnight to 45 minutes before sunrise (i.e., 45 minutes before sunrise and before, which is why I use before: sunrise and before_offset.)

1 Like