My lights aren't switching off

Hello There my fellow Home Assistants :wink:

Could someone help me with this automation? I can’t figure out why it’s not working

It stops at “before sunrise” while that is that is a condition that is met.

alias: Motion Garden Turn All Garden Lights Off
description: ‘’

  - platform: state
    entity_id: binary_sensor.lumi_lumi_sensor_motion_aq2_a1b93d06_ias_zone
    to: 'off'
    for: '00:01'
condition:
  - condition: state
    entity_id: device_tracker.iphone_keep_that_my _myself
    state: home
  - condition: and
    conditions:
      - condition: time
        after: '21:30'
      - condition: sun
        before: sunrise
  - condition: or
    conditions:
      - condition: state
        entity_id: device_tracker.iphone_keep_that_my _myself
        state: not_home
      - condition: sun
        after: sunset
        before: sunrise
action:
  - service: light.turn_off
    target:
      entity_id:
        - light.osram_classic_b40_tw_lightify_8614ae00_level_light_color_on_off
        - light.osram_classic_b40_tw_lightify_level_light_color_on_off
  - service: switch.turn_off
    target:
      entity_id: switch.garden_light_switch_channel_1
mode: single

after 21:30‘ AND ‚before sunrise‘ won‘t work because they never become true at the same time:

after 21:30‘ = 21:30:01 - 23:59:59
‚before sunrise‘ = 00:00:00 - sunrise

Hi @pedolsky and thanks for your reply.

To be honest that does not make any sense to me as sunset and sunrise are celestial timings. For instance there are locations on earth where the sun sets and rises in the same 24hrs. To combine the two time systems is confusing. But ok if that is the way it is my automation is not going to work :joy:

I’ll see if I can move the creators to changing that :wink:

See the documentation for time conditions here and the documentation for sunrise/sunset conditions here

it’s fairly well explained.

also please post any code you provide properly formatted by adding three backticks (```) on the line before and after the code block. It makes it possible to see syntax errors.

@finity thanks for your references.

and I am truely sorry for not using the backticks really I feel so bad I didn’t do that.

1 Like

see the SPACE after the my _myself should it be there ???

Kia Ora to you my fellow homie half way across the globe.

Nope it shouldn’t and it wasn’t I changed it after pasting it here to preserve a bit of privacy. Well spotted though :wink: Thanks.
The issue is indeed the sunrise sunset bit. In my humble opinion the period between sunset and sunrise is one period and therefor should not be interrupted by a clock moving from 23:59:59 to 00:00:00. conversely the period between sunrise and sunset is also one period which is uninterrupted. The key word being “sun”. I’ll will have to learn to live with the way the creators of HA set it up or convince them of changing it LOL.
Another thing that I miss in conditional automations is for examples the terms “if then than that” but maybe that will come at some point in time, either after sunrise or before sunset :rofl: :joy:

But don’t get me wrong i’m absolutely not dissing HA I fricken love and truely think it’s Sweet As :stuck_out_tongue_winking_eye:

TBH, I’m not sure how it could be any other way and still make sense.

the problem is that each condition has to be evaluated as a self contained unit.

so let’s say I have the conditions as you have them above:

- condition: time
  after: '21:30'
- condition: sun
  before: sunrise

taken together it makes sense to interpret them the way you expect.

but taking them individually they make less sense to interpret them that way.

If you just look at:

- condition: time
  after: '21:30'

it would mean “after 2130”. Is that 2130 today? tomorrow? next year? yesterday? it would always be after 2130 because there’s no boundary to either end.

the same for “before sunrise”. Which one? today? yesterday? tomorrow?

both of those situations are resolved by assuming that you mean “today”. Which the developers decided would be delimited by midnight.

if you put both a “before” and “after” in the same condition you won’t have that non-limitation issue. and even then the standard is to wrap those conditions around a reference of midnight.

when using time as a condition it would be within the day. So indeed when I say 21:30 it is the first 21:30 it meets and then it is true. That time is taken over 24hours makes all the sense.

But when you consider sunrise and sunset the premis becomes different because a day starts at sunrise and ends at sunset. The night starts at sunset and ends at sunrise. This is a celestial timing and has nothing to do with hour human 24hour clock. Had we had a day with 27 and half hours sunrise and sunset would stil determine one day and one night. Iow on cyclye.

Maybe it’s not possible to define it that way backended, that I don’t know but. It is as you mentioned “delimited” by the clock.

Of course if I say I want it to work after 21:30 and before 07:00 than it will work. And I could adjust the 07:00 everyday to match the sunrise.

Let me ask you this then; I would like the automation to work between 21:30 until the next sunrise how would you set that up?

      - condition: time
        after: "21:30"
        before: "12:00"
      - condition: sun
        before: sunrise
1 Like

You could also set it up using ‘or’ instead of ‘and’:

condition:
  - condition: or
    conditions:
      - condition: time
        after: "21:30"
      - condition: sun
        before: sunrise

that would be any time after 2130 till midnight or from midnight till sunrise.

that really depends on your definitions of “day”.

you say it is daylight hours.

I say it is a period of time of 24 hours (this Day, the next Day, A Day next month).

I don’t say “a day and night” tomorrow.

they just chose the midnight moment to reference it because that is when the date changes (it’s a new “Day”) and makes the most sense since it never changes.

If they used sunrise then it would change every Day as in your current situation.

“If they used sunrise then it would change every Day as in your current situation.”

I agree my point being that day in Time and date in date is defined by time iow 24hrs.

When you then create a condition with sunrise and sunset it should use those actual inputs as controls not time.

In my simple mind if sunrise and sunset are apples and time and date are peers then when I want and apple pie I should not use peers.

I know what you’re gonig to say now cause I’m using apples and peers LOL but its the definition :wink:

Anyway I’ll try your “or” suggestion and given the HA logic I can imagine that will work.

there is another option that hasn’t been discussed but might be more closely to what you are thinking.

you can use “below_horizon” and “above_horizon” as the sun state condition and will likely give you what you describe. IOW, “is it above or below the horizon (‘day’ or ‘night’) right now?”

But it still won’t solve the time requirement (after 2130) so you will still need the ‘or’ for those two conditions to work correctly.

Thanks, and thanks for the conversation.

I’ll go for the “or” first. :wink:

By the way are you up to speed “or” savvy on InfluxDB and Grafana?

1 Like

Thank you @firstof9 I’ll give that a try too :pray:

I’ve used them a bit but a very little bit so I won’t be any help at all.

Ok, Tried this one but it fails as it automation takes the current time. I suspect it will work after midnight although then the after 21:30 and before 12:00 will the issue for the next day. LOL

Or just trigger on sunrise and turn the lights off, seems much simpler.

That I don’t entirely understand.

The whole motion automation triggers one light on, then after few seconds another light and after a few more seconds even more lights. After the motion is cleared plus an X amount of minutes all lights should turn off.

when I’m home it should only do that between 21:30 and sunrise the following day. When I am not home it should turn off between after sunset and before sunrise.

as we were talking about days and midnight I used the offset function and added 24 hrs to force next day. that worked but then i’m not sure what that will do after midnight :wink:

For shits and grins I tried saying between sunset and sunrise not before 21:30 that hasnt worked yet but might be because of the automation being fired at the wrong time in the sequence.