Rogue sun elevation trigger. Two elevation events in one evening

Running 0.108.3 but been doing this as far as I can tell for the last 3 or 4 weeks so maybe popped up in 107??

I have a notification that triggers on a sun elevation event and turns my PTZ so it looks at the sunset then it sends the picture to my Android TV. I know…very cool aye :wink:

Problem is, it triggers at sunset like it supposed to and it just triggered again at 1156 pm! Anyone see an issue with the trigger?

  - alias: 'Take sunset snapshot and send to TV'
    initial_state: true    
      platform: numeric_state
      entity_id: sun.sun
      value_template: "{{ state_attr('sun.sun', 'elevation') }}"
      below: 0.3
      - condition: template
        value_template: >-
          {% set season = states('sensor.season') %}
          {{ season == 'autumn' or season == 'summer' }}      
      - service: rest_command.move_to_preset_05_07s # view preset defined in #
      - delay: '00:00:05'    
      - service: camera.snapshot
            entity_id: camera.proxy_deck_720_2fps
            filename: '/config/www/images/sunset.jpg'
## Adds a condition inside actions. More info here 
      - condition: state
        entity_id: media_player.sony_bravia_tv
        state: 'on'            
      - delay: 00:00:02
      - service: rest_command.androidtv_sunset_view 

Image shows an approximate time when HA retriggered the automation.


Did you restart home assistant shortly before it triggered?

Because -56.86 is below 0.3, it may have happened when the sun component updated after a restart.

Lightbulb!! Last restart 2236 so that’ll be it. Thought I was going nuts. Great to know.

I’m not sure how you could prevent it (other than only restarting when the sun was well above the horizon).

You could check if the sun has already gone down this day and if the automation has already been triggered.

Thanks. As I’ve got some lights coming on with this elevation level too, I’ll maybe add in something to turn off the automation once triggered and turn it back on once the sun comes up?

Could I use an exact elevation level instead?

As in only fire at x degrees if sun is falling ?
you may find that the elevation is 0.03 … and then -0.01 … so difficult

It’s not an analogue value it’s calculated according to the time, Phil could give you more info on this but the last time I studied it …

I really hate turning automations on and off, you never know where you stand sometimes.
I would have thought the best thing to do is just add a condition if last.triggered(this auotmation) > 12hrs. Sorted

Thanks, that’s a better approach.

Hey Muttley. Any chance you could expand on your automation condition idea? I’m struggling with the code that I would add as a condition to prevents actions if the automation already triggered in the last 12hrs.

Would this work? That returns true if the automation ran in the last 12hrs but true won’t stop the automation. I think I need a ! somewhere?

{{ (as_timestamp(now()) - as_timestamp(states.automation.turn_on_stair_lights_when_dark.attributes.last_triggered | default(0)) | int > 12)}}

The timestamp returns a value in seconds (since epoch, 1970 yeah don’t worry about it) so for example timestamp(now()) + 3 * 60 * 60 will give you a timestamp value equivalent to 3 hours from now.
You are nearly there you just need to increase the numbers appropriately.
Let me get to a workstation and I’ll feedback a bit more.

I’m an openElec guy myself

Thanks Muttley, appreciated. Switched from OpenELEC to LibreELEC and now running CoreELEC on Odroid N2. HDR 4K Rocket Ship!

Your template (for the condition) to stop it running again in the last 15 hours (12 plus a bit to be sure) is along the lines of : -

      - condition: template
        value_template: "{{ (as_timestamp(now()) - as_timestamp(states.automation.turn_on_stair_lights_when_dark.attributes.last_triggered) | int) > 60 }}"

The 15 * 60 * 60 - is - 15 hours * 60 minutes * 60 seconds to give a value in seconds, adjustd to suit.
So it HAS to be greater than 15 hours since last triggered to be true and thus allow the automation to fire again.

You ‘could’ include the | default(0) if you want but the | int won’t do much as it’s already a number and all you’re doing is adding an extra operation. Dunno !

Edit: I did some experiments, and If you have never fired the automation before ‘last_tiggered’ returns “none” So casting it to int gives a ‘zero’ in this case. Which is fine for our purposes.

Thanks to @Anwen as he made me think about this more.
I think the last triggered will be useful in a lot more circumstances and thus you have another tool in your box.
The bit Anwen made me think about was "well if I trigger the automation (even though it was previously fired 2 hours ago) does that count as a trigger ? "
Well it turns out it doesn’t, it seems only to record last triggered IF it also passes any conditions in the condition section (action conditions don’t count). This is an interesting and useful wrinkle.
Though you have to be careful with this as if you do a restart before first firing the automation and the sun has set then the “first fire” will be the ‘restart’ as it’s sun is below set elevation. So the first fire must be your required event and then wait the given time before refire (admittedly this will pan out over a couple of days but not ideal).
Anyway I’ve adjusted the template above but I actually think given that you only need to block on restarts - that you might be better off with Anwens suggestion.

1 Like

Ah ha. I get it now. The automation fired a few hours ago so when I run that now in the template editor it returns false which is what I’m looking for. Thank you so much for your help and detailed explanation.

So, if you check ‘about’ an hour before sunset you should see that it returns true and will allow the automation to fire.
Get back if there’s any issues.
Also when you are happy, post the finalised automation for the next guy who comes looking.

I posted about a different way that I handle automations that use sun elevation as a trigger, that prevents triggering on restart here. Maybe it might be useful to someone.

1 Like

I’ve edited the above solution after testing, please re-read

1 Like

Thank you, that is very helpful.

1 Like

Done and thanks for the update. Modifying accordingly.