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).
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).
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
@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.
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.
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.
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?
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.
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 }}"