Automation not fired when using Time after / Sun condition

Hi,

I have a small issue with an automation not being fired, when I use the time as a condition.
Right now it does not fire with the code shown below (1st. Automation).

What I am trying to do, is to turn on the light when the sun elevation is below -4.0, but only if the time has passed 19:00:00.
If I add the “before” part again (which in my example below is out commented), then it runs.
I have tried reading the docs and maybe it does make sense, but I just don’t get it. I mean, the state would be “true” right, if the time is 21 and the sun.sun sensor updates its elevation and it returns lets say -10.35.

Why is it, that I need the “before” part in the first automation when I don’t need that part in my second automation?
The only difference here is that I use before instead of after.

1st. Automation

########## Xiaomi Nightlight Kids Automatic Turn On ########
  - alias: '[Playroom] Nightlight On'
    trigger:
      platform: numeric_state
      entity_id: sun.sun
      value_template: '{{ state.attributes.elevation }}'
      below: -4.0
    condition:
      - condition: state
        entity_id: alarm_control_panel.house
        state: 'disarmed'
      - condition: time
        after: '19:00:00'
  #     before: '03:00:00'
    action:
    - service: light.turn_on
      entity_id:
      - light.gateway_light_kids
      data:
        brightness_pct: 1
        color_name: white

2nd. Automation

##### Xiaomi Nightlight Kids Automatic Turn Off ####
  - alias: '[Playroom] Nightlight Off'
    trigger:
      platform: numeric_state
      entity_id: sun.sun
      value_template: '{{ state.attributes.elevation }}'
      above: -1.0
    condition:
      - condition: state
        entity_id: alarm_control_panel.house
        state: 'disarmed'
      - condition: time
        before: '09:00:00'
    action:
    - service: light.turn_off
      entity_id:
      - light.gateway_light_kids

Can someone explain why this is needed and give me a lesson so I learn it :slight_smile:

Thanks !

I’ve looked at your automation and I have one question : -
How can you be sure that the sun elevation will cross the -4 degree threshold after ‘19:00:00’ ?

Edit: Or from your second automation sun rises across -1 degree before 09:00 ?

1 Like

I can’t be sure, but I know that the sun will be below -4 during the evening / night.
During the last year I’ve monitored my outdoor automation where I turn on the lights after the sun has passed the -4 mark, where it gets so dark I want the light to be turned on. Happens on different times of course during the summer and winter - but the -4 suits me.

During the morning, once again I can’t be sure, but that is just what I’ve seen so far.

I guess you want me to state that I can’t be sure if something (which I can find the exact work for) happens to this elevation cycle.

But instead of using the before I probably could use the sun.sun and then before sunrise?

No you are missing my point.
I looked at my own sun elevations (well dusk and sunrise to be precise) and though the sunrise would ‘currently’ allow that automation to fire, I couldn’t swear that it would all year round.
Whereas your first automation just would not fire for me.
What time is your next dawn ?

Yeah I’m sorry - I just don’t get it :crazy_face: :confused:

That would be around 8.30 tomorrow morning.

Sorry, I’m typing on a phone, I meant next dusk (ie - 6 degrees evening) this is available as a standard sensor of the sun component which you evidently have set up.

No worries.

That would be 16.33 (or 4.33pm).

Thanks, you have just proved my point.
16:33 the automation fires (-6 is pretty close to - 4 and certainly not 2 1/2 hours apart)
But the after 19:00 condition blocks it from running

I understand that, but the sensor will eventually keep updating right?
So every single time the sun updates the elevation it will look at the condition.

So at 16:00 it would be a No, because the “after” condition is false.
But at 19:15 the sensor updates to -20.95 and the time has now passed 19:00 which then is true…

So why is that not happening? I want to make sure that it starts after 19:00 because it will be around that time we put the kids to sleep - but I don’t want it to turn on if it is not necessary due to sunlight during the summer.

No.

The trigger is the trigger to look at the conditions
If it doesn’t match you’ve missed that evenings trigger
And your ‘trigger’ only occurs at the point of crossing so from (say) -3,67° to -4.04° and as the update times for sun are discrete then that is an instant.

You have a basic misunderstanding of what a trigger is

instead set two triggers, one a crossing -4 the other at 19:00
then add another condition to check that the sun is below -4

Your automation then becomes : -
if it is sun < -4 OR it is 19:00
then check conditions
Condition 1 - Sun is < -4
Condition 2 - time is > 19:00
Condition 3 - Alarm is disarmed

I’m assuming you mean “after 19:00 or before 03:00” in which case you should explicitly include the “or”. The default is “and” and that’s never true.

      condition: or
        conditions:
           - condition: time
             after: '19:00:00'
           - condition: time
             before: '03:00:00'

Actually No.
I originally thought the same as you but on checking the docs they’ve changed the way this is interpretted (I don’t use this anyway - I’d use a template) but the docs say : -

Time condition windows can span across the midnight threshold if both after and before keys are used.

see : -

Maybe I got confused since I experienced that the trigger was triggered several times during a night where I played with my outdoor light last year. That was probably caused by the fact that the HA was rebooted and then the count of trigger was reset, which obviously makes sense and therefore when the sun updated its elevation again it fired.

Maybe I should just add back the before 03:00:00, because that would do the trick for me.

Mutt, would it be possible for you to create a template with my above example so I can see how you would tie it all together?

No it wouldn’t but you have to try, so that you can see the results

No, the level of understanding you have shown here would mean that the ‘quick-fix’ of a template would mean that you didn’t improve your understanding of HA
The automation you should be going for is along the lines of : -

  - alias: '[Playroom] Nightlight On'
    trigger:
      - platform: numeric_state
        entity_id: sun.sun
        value_template: "{{ state.attributes.elevation }}"
        below: -4.0
      - platform: template
        value_template: "{{ states('sensor.time') == '19:00' }}
    condition:
      - condition: template
        value_template: "{{ state.attributes.elevation | float <= -4 }}"
      - condition: state
        entity_id: alarm_control_panel.house
        state: 'disarmed'
      - condition: time
        after: '19:00:00'
    action:
      - service: light.turn_on
        entity_id: light.gateway_light_kids
        data:
          brightness_pct: 1
          color_name: white

Note that the method of triggering on time here evaluates 1/minute rather than the standard which has to evaluate 1/second, but this only equates to a ‘small’ improvement in efficiency as the other method is optimised for. Still I take whatever improvements I can get.

I have merely adapted the automation you gave, I have not and can not test this, I am trusting that the majority of your automation was correct. You will have to test

Edit: This uses sensor.time which requires you to set up some time and date sensors (read the docs)

I’m sorry if you feel that way. I do learn a lot when seeing examples and in fact it helps me understand the theory behind it.

I did get the point that my condition was not true, because the automation got triggered at around 16:30 (4.30pm), but at that point my condition was false.
So by either using your template (which I have to look over and fully understand - have a pretty good idea), then I will add the before to my time condition and set it to 03:00am.

I guess I could remove the before 09:00am in my 2nd. Automation, because what really matters is that the lights get turned off, when there is enough natural light.