Lights on with sun elevation (keeps triggering)

I’m trying to make an automation to turn a light on when the sun elevation goes below -1 and then turns the light off at 10:05pm (plus another random 1-15 minutes). My current YAML is below. However, my issue is that the first entry to turn on the lights seems to continue triggering, so even after the light turns off, it gets triggers again and turns back on! I want the first automation to run only once when the elevation is -1 and dropping (so not with it is -1 in the morning either). I thought this would achieve that as this is similar to the examples I’ve found. None of the examples I’ve seen mention anything about extra conditions to prevent continual triggers. The screen show below shows the logbook entries for the turning on automation (not sure what the “turned off” stuff is about).

- id: family_room_evening_on
  alias: "Family Room (evening-on)"
  trigger:
    - platform: numeric_state
      entity_id: sun.sun
      attribute: elevation
      below: -1.0
  action:
    - service: switch.turn_on
      entity_id: switch.family_room
- id: family_room_evening_off
  alias: "Family Room (evening-off)"
  trigger:
    - platform: time
      at: "20:05:00"
  action:
    - delay:
        minutes: '{{ range(1, 15)|random|int }}'
    - service: switch.turn_off
      entity_id: switch.family_room

Here is the action on this “Family Room (evening-on)” from my log book:

Also, as 1 follow-up to this: is there any difference to the three triggers below, with how the elevation is accessed? As far as I can tell, all 3 are trying to access the elevation attribute, why is there three different ways to do this? Is there an advantage that any of them have, besides the top one looking the cleanest (and why I used it).

The way the UI accesses the elevation attribute:

  trigger:
    - platform: numeric_state
      entity_id: sun.sun
      attribute: elevation
      below: -1.0

From the HA documentation

  trigger:
    platform: numeric_state
    entity_id: sun.sun
    value_template: "{{ state_attr('sun.sun', 'elevation') }}"
    below: 3.5

From this blog post of the person that apparently added this elevation functionality

  trigger:
    platform: numeric_state
    entity_id: sun.sun
    value_template: "{{ state.attributes.elevation }}"
    below: -4.0

Why does your automation keep turning off? There is more going on that just that automation.

1 Like

I had a similar problem and I added the ‘above’ option too

# Floorlight on at Sunset*******************************************************
- id: Floorlight on at Sunset
  alias: Floorlight on at Sunset
  trigger:
    - platform: numeric_state
      entity_id: sun.sun
      value_template: "{{ state.attributes.elevation }}"
      below: 3
      above: 2
  condition:
    - condition: time
      after: '14:00:00'
      before: '22:00:00'
  action:
    - service: switch.turn_on
      entity_id: switch.sonoff_10001bb5c7

another way is to add an input boolean.
condition to be off.
and at the action to turn the input boolean on.
but you will need one more automation each morning for example to turn it off

Turned off is probably due to you reloading automations/restarting Home Assistant.

Did you reload automations/restart Home Assistant while testing?

the only thing i can think of that would cause that would be if your sun entity loses the correct values.

if it goes to unavailable then becomes available again it will probably re-trigger.

what does a sun elevation attribute history graph look like? (you’ll probably have to create a sensor to track it…)

Here is the sensor code if you want to verify it’s working correctly:

- platform: template
  sensors:
    sun_elevation:
      friendly_name: "Sun elevation"
      unit_of_measurement: '°'
      value_template: "{{ state_attr('sun.sun', 'elevation') }}"

Thank you for all of the feedback everyone.

@nickrout - I’m not sure, perhaps it was from reloading the automation script as I tried to get it working as @Burningstone suggested. I did reload automations while testing, but I believe the automation still triggers a few times well after the last time I’d changed anything. I’ll try not touching anything tonight and seeing what happens. But the automation did also trigger again this morning at roughly 3am, perhaps when the sun rised above the -2 elevation? But it seems that it would be a common thing to only want to trigger as it goes down (or only as it goes up). Is that not possible without extra conditions?

Also, it trigger at 7:30pm and again at 9:58pm, without ever being turned off in between there.

@Makis - thanks for the suggestion! It seems that it should be possible to make it trigger only once per day without making extra “above” tigger nor extra condtions. But maybe I’m naive and it isn’t. I’ll try this if I don’t find something else that works!

@finity - thanks, I’ve added this sensor, so I’ll try tracking it to see what it does. One of the times the automation triggers (when I didn’t want it to) was was in the room. I checked the elevation of the sun in Home Assistant at that time and found it to show an elevation of -18.X degrees.

This might be a little easier to follow if we knew what time the sun was rising and setting for you now.

e.g. the sun sets at 8.30 pm here now and your logic would create problems, because as I read it you want the lights on at sundown and off at 8:05(+random minutes up to 15) pm.

@nickrout, good point. That is actually a typo in my 24-hour time. That should be 22:05:00 as I want them to turn off after 10pm, not 8pm. That said, my “evening-on” script triggered to day at 4:36 PM and it has not, yet, triggered again (for 3.5 hours). So I’m hopeful that if I don’t reload my automations, that I won’t have it restarting.

That said, I suspect that it will still get triggered on tomorrow morning when the sun is at -2 elevation, but we’ll see.

At this time of year, the sun rises at ~7:15am and sets and ~4:30pm here.

1 Like

Wouldn’t this trigger fire every time the sun elevation changes? When it changes from -2 to -3 it would still evaluate to true and would fire again.

You should create a binary template sensor like “sun off” and put your condition “sun elevation below -1” there. Then you can use this binary sensor as trigger.

No, a numeric state trigger only fires when the threshold has been crossed. It will only trigger again when the value goes above the threshold and then below the threshold again.

You are right in this. My automation was working fine for 6-7 months only with the below option. I really don’t know-remember what happened but sometime (it started a few months ago) started to been triggered twice or more. That’s why I added the above option. Since then I left it as it now.

Ah, you are right. I might have mixed this up with a plain state trigger.

Ok, so after looking closer at my data from the last few days, I think I do have this “figured” out. If I don’t touch my home assistant configuration/automations, then my original YAML does seem to work fine. However, if I reload automations or restart HA, then the automation will be re-triggered after exactly 20 minutes. This makes sense, because sun.sun updates every 20 minutes after the sun is below -18 elevation, per this pull request. So when home assistant starts up (or the automation is reloaded), it must assume an initial elevation of 0 or something, so that the first time the automation triggers it drops below the -2 threshold.

That said, this still isn’t ideal for me as most of my edits to HA are later in the evening/night and it is (will be) annoying to have my lights getting triggered every time I reload my automations to test something new. So I guess that I’ll still need some other condition to prevent these false positives, although really it seems like a bug to me.

There is a related github issue about this here.

I’m late to the party but stopped by to say that a Numeric State Trigger is ‘reset’ when Home Assistant restarts (i.e. when automations are reloaded). This is considered to be nominal behavior. To mitigate it see Frenck’s post in the linked GitHub Issue (in Spectre5’s post).

@123, how would you actually do that? “You could use a condition on the automation to ensure the attribute state is not coming from a unknown/none state to filter out these occurrences.” How would I get the “coming from” value and compare it to unknown? Is the previous value actually “unknown” or is it None or 0 or something else?

EDIT

Changed my reply. I don’t use the Numeric State Trigger the way it’s used in this topic so I have no stake in this discussion; please direct your question to the linked GitHub Issue.

I’m using a sun elevation automation for a while now, but I keep on struggling with the following.

I’ve got a simple slider on the lovelace cards:

input_number.sunset_for_automation

Gives me this attributes:

initial: 5
editable: false
min: -10
max: 25
step: 0.5
mode: slider

Current data

7.5

Whatever I try to do, I don’t get this “elevation” into the automation. Ive tried it in yaml and visual editor, but it just won’t work. Tried this with single quotes, double quotes, no quotes, etc… Anyone the correct workaround?

"{{ states('input_number.sunset_for_automation') | float }}"
"{{ states('input_number.sunset_for_automation') | int }}"