# Calculate negative offset for sunset

Hi all,

In the automation editor I currently have a -45 minute offset before sunset for turning lights on. I would like to calculate that offset by using “sensor.owm_cloud_coverage” which returns 0 - 100 representing the percentage of cloud cover.

Can you put a calculation in the offset field and if so how do you format it? Calculation something like “-30 + (- sensor.owm_cloud_coverage/ 4)”. This would create a -55 minute offset at 100% cloud coverage and -30 at 0% coverage.

Thanks for reading this and helping out.
Clay

You can do this with a template trigger.

Thanks for the reply, however I don’t understand how to write the json equation.

Okay, first a couple of questions to see where we stand.

1. Do you have sun.sun as an integration ?
2. Do you actually have a minute offset requirement (ie “I need 15 minutes to get outside each night to take a photo of the sunset”) ?
3. Or is it to estimate a light level to maybe turn on some lamps ? In which case you would be better with sun elevation < 15 degrees and falling (adjust to suit)
4. Is it always a negative offset. Because once the event happens the next event is recalculated and that will be the next day. So you will never reach “5 minutes past the next sunset” (though there are some workarounds)

I see you also want to factor in cloud cover, why not just use a light sensor ?

(it’s not JSON, it’s just jinja wrapped python, just call it a ‘template’) give me a couple of hours till I get home, though someone else may step in before then.

Try this : -
The only entity id you gave was sensor.own_cloud_coverage (which I’m assuming is an integer 0-100, if it is an actual percentage, then you will need to cast to float and multiply the factor by 100 to get the formula to work).

``````automation:
- alias: au_switch_lights_on
trigger:
platform: time_pattern
minutes: '/1'
condition:
- condition:  template
value_template: >
{% set crnttime = states('sensor.time') %}
{% set sunset = as_timestamp(state_attr('sun.sun', 'next_setting')) %}
{% set cldcvr = states('sensor.own_cloud_coverage') | int %}
{% set offst = (30 + cldcvr / 4) * 60 %}
{% set modtime = sunset - offst %}
{% set fnlstring = modtime | timestamp_custom('%H:%M') %}
{{ crnttime == fnlstring }}
action:
- service: light.turn_on
entity_id: light.big_light
``````

Note because we need to evaluate the cloud cover at ALL possible trigger times and we don’t know what the cover will be until we test it, then this automation has to trigger every minute but only fire when the cloudcover condition offset is met, to equal the current time
And as such (with the minute checking) it ‘could be’ anything up to 59 seconds earlier than expected
We could adjust this to be +30/-29 but it’s still in a 59 second range

Try it and see

I’ve tried to write it so that you have the best chance of modifying it if you need to.

NOTE: This is an automation and goes with your other automations. You should only ever have ONE automation: heading (unless you use packages, if you do use packages then you’ll know all about this anyway)

Edit: just had a thought, if : -
Say Sunset today is at 18:55 and you had 100% cloud cover then the automation will fire at 18:00 (the sunset will not recalculate until it has set but day to day variations are never as much as 30 minutes (your minimum offset)) but say 5 minutes after the lights come on at 18:00 the cloud cover drops to 50% (30 + 12 = 42) so the automation will fire again at 18:13 I don’t think it will have any effect and you won’t notice unless you had manually switched the light off between 18:00 and 18:12
Just thought I’d mention it

Mutt

I didn’t see your previous post until know. I installed the automation and I will see if it works.

But you are right, this is to turn on interior lights and adjust for cloud cover. I see how sun angle would be better. Choose an angle early enough and add a delay offset that is always after the angle event.

I light sensor is the best solution and I have one waiting for weather station project.

Thanks for the help!