How to enable an automation between times

Maybe this is better suited to a “Script” or “Scene”, but here’s the idea…

I’d like to turn my den lights on eariler than normal (normal = Sunset, already programmed) if the weather is very overcast/rainy (and thus it’s “dark outside early”).

I enabled OpenWeatherMap, and that looks great. I can get to the values I want for the weather conditions.

However, I’d like this automation to run anytime between like “Sunset -02:00” and “Sunset” (when the usual one would run anyway). But not at a specific time. So basically “Enable this automation to run at certain times of the day, and then wait to see if the weather_condition int meets the triggers”.

The way I read the automation template is that if I specify a Time, it will test/trigger AT that time. But if it suddenly gets dark and stormy a few minutes later…the automation won’t run, because the time isn’t right.

Hopefully I’m describing my scenario correctly. Please ask for clarification.

THANKS

Here is a trigger from one of my automations which shows offsetting from sunset.

- id: Turn on and off evening lighting
  alias: Turn On and Off Evening Lighting
  description: Family room lights and kitchen table light triggered for on and off
  trigger:
  - id: 'on'
    platform: sun
    event: sunset
    offset: -00:45:00
  - id: 'off'
    platform: time
    at: input_datetime.family_room_lights_out
1 Like

How about a schedule helper in a condition, so that the automation only runs if it’s “on”.

Use your sunset-2 & weather_condition as triggers, then also add them as conditions.

That way if any of the triggers are met, it will check against both conditions being true.

1 Like

These are good suggestions, but from what I understand, any of these will trigger ONLY at “Sunset -02:00” (and then check the other values).

There must be a way to basically enable the check and then IF/WHEN the weather-related value happens, actually execute.

The “Helper” may work…I need to look into helpers…maybe program a helper to set a custom variable to “On” (or 1) at the time of day, until actual Sunset (set it back to 0). Then use both in the trigger, and set the variable to 0 or something else in the automation so it doesn’t run in a loop?

Use a light sensor. It’s a better judge of darkness inside your home than a weather forecast for a geographic area. I’ve used one to offset my home’s interior lighting schedule for well over a decade (originally with other home automation software and more recently with Home Assistant).

You don’t need a helper. Re-read my previous reply - you can have multiple triggers in an automation.

Since triggers are ORed, your automation will fire when ANY ONE of those things happen

Well, technically in this case I need the time-range AND’ed with the 3 possible sensor values (sensor values OR’ed).

Plus, if I have to use this in multiple automations, it would be way better if there were a helper that I could edit once and have it apply across anywhere it’s used instead of having to maintain it in multiple places, right?

Yeah, the multiple automations part is a new requirement, so a helper might be more useful.

Then again, if you follow Taras’ advice and get a light sensor, you’d only have the time range and a single numerical sensor value to deal with.
With that, I don’t even think you’d need to deal with time in the trigger section. You’d simply trigger on the lux value, since if it’s too dark in the middle of the day (for whatever reason), you’d want to turn on the light anyway.

It might be easiest to use a template binary sensor like this:

template:
  - binary_sensor:
    - name: "Time in Window"
      unique_id: "Time in Window"
      state: >-
        {% if states('sensor.home_sun_setting') | as_timestamp >= (now() | as_timestamp) and 
              states('sensor.home_sun_setting') | as_timestamp - 45*60  <=  (now() | as_timestamp) %}
              'on'
        {% else %}
              'off'
        {% endif %}

1 Like