My lights aren't switching off

description: ''
trigger:
  - platform: state
    entity_id: binary_sensor.lumi_lumi_sensor_motion_aq2_xxxxxxxx_ias_zone
    to: 'off'
    for: '00:01'
condition:
  - condition: state
    entity_id: device_tracker.iphone
    state: home
  - condition: sun
    after: sunset
  - condition: sun
    before: sunrise
    before_offset: '24:00:00'
  - condition: not
    conditions:
      - condition: time
        before: '21:30'
action:
  - service: light.turn_off
    target:
      entity_id:
        - light.osram_classic_b40_tw_lightify_xxxxxxxxx_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

This works in the at least between 21:30 and midnight I’ll have to see what it does after midnight some other time :wink:

morning, :wink:

the automation did not work after midnight.

That’s no surprise if you do

  - condition: not
    conditions:
      - condition: time
        before: '21:30'

And why have 24 hour offset on the sunset?

Think it should be:

description: ''
trigger:
  - platform: state
    entity_id: binary_sensor.lumi_lumi_sensor_motion_aq2_xxxxxxxx_ias_zone
    to: 'off'
    for: '00:01'
condition:
  - condition: state
    entity_id: device_tracker.iphone
    state: home
  - condition: sun
    after: sunset
  - condition: or
    conditions:
      - condition: and
        conditions:
          - condition: time
            after: '21:30'
          - condition: sun
            after: sunset
      - condition: sun
        before: sunrise
action:
  - service: light.turn_off
    target:
      entity_id:
        - light.osram_classic_b40_tw_lightify_xxxxxxxxx_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

Although, thinking about it. You probably only want those conditions in the turn on automation. Now, if you turn on the light just before sunrise they will never go out if they are still on after sunrise. Or if you leave the house with the lights still turned on. So are the light only turned on by motion? And only after 21:30 / before sunrise? If so, you can just remove all conditions for the turn off.

Hi @septillion

“And why have 24 hour offset on the sunset?”

the offset on the “sunrise” is because at 21:30 HA considers the sunrise of that day and I want it to consider the sunrise of the next day.

The Turn on automation is simple. Motion is detected switches on one light then switches on several other lights with different delays in a sequence. This only happens when the LUX value is below a certain threshold.

But after midnight it does not and after 21:30 you allow it anyway, so the 24 hour offset makes no sense…

But again, having all these conditions to turn OFF the light does not make sense :wink: Like I said, don’t you want the light to go out after sunrise or if you happen to leave? That is what you create with all this.

So if you only turn the light on by motion (and other conditions) it would make sense to have no conditions at all to turn them off again. So light is only allowed to turn of when it’s dark, you are home, there is motion etc. But if conditions are met and the light are turned on, just turn them off after the motion stopped.

I tried with and without the 24 hours. without the 24 hours it doesn’t work.

The lights are outside. So if they are turned on by motion I don’t want them to stay on the remainder of the night.

Indeed it might be a bit over complicated but it also a learning and maybe improving experience.

indeed If I have the lights turn on only after 21:30 and with a defined lower LUX value the setup a delay in that same automation the sunrise can be kept out of the automation all together.

It is just interesting to see if there is a way to define the period between 21:30 in one day and sunrise in the next.

Then I can assure you there was an error somewhere else :wink:

So don’t make the turn of conditional! :wink: Because of all the conditions the light might not turn off in edge cases, even if we fix the condition to what you want it to be. Just:

description: ''
trigger:
  - platform: state
    entity_id: binary_sensor.lumi_lumi_sensor_motion_aq2_xxxxxxxx_ias_zone
    to: 'off'
    for: '00:01'
condition: []
action:
  - service: light.turn_off
    target:
      entity_id:
        - light.osram_classic_b40_tw_lightify_xxxxxxxxx_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

Yeah, that’s easy:

description: ''
trigger: []
condition:
  - condition: or
    conditions:
      - condition: time
        after: '21:30'
      - condition: sun
        before: sunrise
action: []
mode: single
description: ''
trigger: []
condition:
  - condition: time
    after: '21:30'
  - condition: sun
    before: sunrise
action: []
mode: single

this will only work if triggered before midnight. after midnight the first condition will be false as it is not after 21:30 on that day.

could be but when simply taking the automation with the 24 hours offset, working. and removing only the 24 hour offset it fails. Then leaving the 24 hours offset in there works until midnight but after that it fails.

I do want to thank you and say that I appreciate your involvement. really cool. Maybe there is a way to try it at your end?

Crap, I deleted a bit to much while I edited the code :zipper_mouth_face: Made a correction.

And although I could make an automation to check it, but I’m off to bed before midnight and up after sunset :smiley:

But do you get what I mean by it not making sense to have all the conditions for the off-automation?

:rofl: :joy: :rofl: :joy:

I do and I think it has become a bit more excessive because of not working from the get go. I’ve built it up by what I initially thought but then deleted conditions by trial and error. to see what happens. I must say the tracer that was recently added to the automations is awesome that really helps to see step by step what happens.

I would be cool to see automations following some if then than that logic.

I just went through this a couple days ago trying to set up an automation, I got stuck initially doing the same, what I thought was logical at the time, trying to turn on between sunset and sunrise.

Of course it didn’t work as both have already passed and the next sunrise is now on a completely different day, so if you just reverse your logic and instead of using between sunset and sunrise, change it to NOT between sunrise and sunset it should work like you expect.

This is relevant part of my now working solution,

condition:

  • condition: not
    conditions:
    • condition: sun
      before: sunset
      after: sunrise

Pretty sure you can make it work like you were expecting like this, just need to add in you 21:30 time part somehow. I’m sure there are many other ways as well but this worked for me.

@427cid That’s indeed true according to De Morgan law :wink:
not ( (before sunset) and (after sunrise) ) = not ( (not after sunset) and (not before sunrise) ) = (after sunset) or (before sunrise)

Hi @427cid

Thanks for pitching in. To me it’s all in the definition and in this case the mixing of them. On the one hand you have a day which is defined by 24 hours and the other hand a day is time between the end of sunrise and the time between the following sunrise. This last definition is of course variable in length depending on the latitude that your are.

What is happening in this case is that they a combined or mixed so you will. In other words once you pass midnight the sunrise sunset is reset. I have asked @frenck to look in to this and to maybe see if it possible to change the definition to, at the time of a trigger look at the next sunset or sunrise.

As for my automation I now changed it to independently of the sunrise turn off the lights x amount of minutes after they were turned. Keeping in mind that the wont turn on during daylight and not to trigger before 21:30.

The only definition of a day HA uses is from 0:00 to 24:00, that’s it. Nothing more, nothing less.

Sunrise and sunset just define points on the day, just like time does. So the same implicit ending applies as if you would use time. So just like “after 23:00” is only true till midnight, “after sunset” is also only valid until midnight. Changing this behaviour would only make it inconsistent :slight_smile: So if you would like to extend the time range, you have to add (aka or) the second range to the condition.

1 Like

Oh, you mean like I posted 15 posts up and two days ago:

It’s almost like I wasted my time (and still am…) trying to help someone who doesn’t want it to work the way it actually does and wants to do it like they think it should work even tho it doesn’t actually work because they disagree about “definitions”.

Of Course you don’t have to waste your time! No one is asking you to help.

I guess we also have a different definition of what “asking for help” is then, too.

Ah yes I forgot, you’re the only one here and the only one who can answer.

Look man, I hope that whatever your issue is you are in general a happy guy. Cause to me it seems you are a bit frustrated. I have had nothing but friendly assistance here except for your “help”

So I ask you politely not to provide anymore “help” on this subject. And thus not to waste anymore of your time.

I never said that but you did specifically ask me specifically for my help. That was a reply directly to me in that second quote above.

All of my assistance has been friendly too until you decided to ignore my friendly help and are still struggling two days later to get your stuff working.

The frustration comes from offering time out of my day to try to help someone get something working that they asked for help on and when you give them an answer they don’t use it and continue to complain that it should work a different way because of definitions.

And you aren’t just responding to me in that way either. You’ve been given the same suggestion a couple of times already by others and you still want to debate about what “day” means.

no problem.

Don’t get sour, just tried to get your point across to TS. :slight_smile:

But main thing here is, for the off-automation all the conditions are just nonsense.