Automations - variable wait time or Sunrise or Sunset + or - variable time

Hello, I have spent several hours trying but without success. My goal is:
1 - Launch an “automation” for example at Sunrise with a random time adjustment between 2 terminals (ex. x and y minutes);
2 - Introduce in an automation a random time delay between 2 terminals (ex. x and y minutes).

Do you have any suggestions for me?
Thanks

What are the possible values of “x and y minutes”? Are they always positive values, negative values, or can be both?

Ideally I would like to be able to do it with negative as well as positive values

As you have already discovered, you can’t use a template for the offset option of a Sun Trigger. If that were possible, it would be easy to do what you want.

What I have in mind is a Trigger-based Template Sensor. At the beginning of each day it calculates the sunrise time plus or minus a random offset ranging between x and y minutes. In other words, at the start of each day, a new time is computed based on sunrise and a random offset. Your automation would no longer use a Sun Trigger but a Time Trigger that uses the Template Sensor.

Does that sound like it would meet your requirements?

Interesting solution avenue, but how?

Here are two Trigger-based Template Sensors to compute randomly offset sunrise and sunset (range of plus or minus 25 minutes). The calculation is performed at midnight and whenever Template Entities are reloaded and Home Assistant is restarted.

template:
  - trigger:
      - platform: time
        at: '00:00:00'
      - platform: event
        event_type: event_template_reloaded
      - platform: homeassistant
        event: start
    sensor:
      - name: 'Sunrise Offset'
        unique_id: 'sensor_sunrise_offset'
        state: "{{ state_attr('sun.sun', 'next_rising') | as_datetime + timedelta(minutes=range(-25, 26) | random) }}"
        device_class: timestamp
      - name: 'Sunset Offset'
        unique_id: 'sensor_sunset_offset'
        state: "{{ state_attr('sun.sun', 'next_setting') | as_datetime + timedelta(minutes=range(-25, 26) | random) }}"
        device_class: timestamp

The automation simply needs to use a Time Trigger with the desired Template Sensor.

alias: example sunrise offset
- platform: time
  at: sensor.sunrise_offset
action:
  ... etc ..
1 Like

Very interesting, I’ll give it a try.

I have integrated it in “configuration.yaml”.

I will now modify an “automation” for tonight.

You should see two new entities in Developer Tools > States:

The time for sensor.sunset_offset represents when you should expect your automation to be triggered this evening. If you want to know what that represents in your local time, copy-paste this into the Template Editor:

{{ states('sensor.sunset_offset') | as_datetime | as_local }}
1 Like

Indeed, here in Quebec City, the sunset should be at : 15 h 58

The variable sensor.sunset.offset is : 2021-12-16T21:20:29+00:00 is equivalent to 16h20 (utc -5)

Thank you
The whole thing works very well

Nice !
Is it possible to add / subtract a helper ?
Something like this (but it doesn’t work…) :

  - name: 'Sunrise Offset1'
    unique_id: 'sensor_sunrise_offset1'
    state: "{{ state_attr('sun.sun', 'next_rising') | as_datetime + datetime.strftime(states('input_datetime.tijdstip_offset_sfeerverlichting')) }}"
    device_class: timestamp

Assuming the input_datetime contains time only (no date) then this should work:

  - name: 'Sunrise Offset1'
    unique_id: 'sensor_sunrise_offset1'
    device_class: timestamp
    state: >
      {% set t = 'input_datetime.tijdstip_offset_sfeerverlichting' %}
      {{ state_attr('sun.sun', 'next_rising') | as_datetime +
          timedelta(hours=state_attr(t, 'hour'), minutes=state_attr(t, 'minute'), seconds=state_attr(t, 'second')) }}

That’s it, thank you !

But my trigger goes not off :frowning:

{{ states(‘sensor.sunset’) | as_datetime | as_local }} gives me 2022-01-14 17:08:57+01:00

My offset is 00:30 so :

{{ states(‘sensor.sunset_sfeerverlichting_plus’) | as_datetime | as_local }} gives me 2022-01-14 17:38:57+01:00

But my trigger goes not off

trigger:
  - platform: time
    at: sensor.sunset_sfeerverlichting_plus

Do I have to indicate that local time must been used ?

Sorry, it seems to work anyway !

Last question : now I have this 2 sensors

  - name: 'Sunset Sfeerverlichting Min'
    unique_id: 'sensor_sunset_sfeerverlichting_min'
    state: >
      {% set t = 'input_datetime.tijdstip_offset_sfeerverlichting' %}
      {{ state_attr('sun.sun', 'next_setting') | as_datetime -
          timedelta(hours=state_attr(t, 'hour'), minutes=state_attr(t, 'minute'), seconds=state_attr(t, 'second')) }}
    device_class: timestamp
  - name: 'Sunset Sfeerverlichting Plus'
    unique_id: 'sensor_sunset_sfeerverlichting_plus'
    state: >
      {% set t = 'input_datetime.tijdstip_offset_sfeerverlichting' %}
      {{ state_attr('sun.sun', 'next_setting') | as_datetime +
          timedelta(hours=state_attr(t, 'hour'), minutes=state_attr(t, 'minute'), seconds=state_attr(t, 'second')) }}
    device_class: timestamp

I created a select helper “input_select.select_offset_sfeerverlichting” with 2 options : “+” and “-”.

Now I want a new sensor depending of this helper…I did a try but there’s is an error :

  - name: 'Sunset Sfeerverlichting'
    unique_id: 'sensor_sunset_sfeerverlichting'
    state: >
      {% set t = 'input_datetime.tijdstip_offset_sfeerverlichting' %}
      if (state('input_select.select_offset_sfeerverlichting') == '+')
          {{ state_attr('sun.sun', 'next_setting') | as_datetime +
              timedelta(hours=state_attr(t, 'hour'), minutes=state_attr(t, 'minute'), seconds=state_attr(t, 'second')) }}
      else
          {{ state_attr('sun.sun', 'next_setting') | as_datetime -
                  timedelta(hours=state_attr(t, 'hour'), minutes=state_attr(t, 'minute'), seconds=state_attr(t, 'second')) }}
    device_class: timestamp

The syntax of the if-else-endif is invalid; it’s missing braces.

Look at the first example in Building Templates.


For future reference, you should create a new topic for your questions and not tack them on to an existing solved topic.

Super, found it…thanks a lot !

      {% set t = 'input_datetime.tijdstip_offset_sfeerverlichting' %}
      {% if (states('input_select.select_offset_sfeerverlichting') == '+') %}
          {{ state_attr('sun.sun', 'next_setting') | as_datetime + timedelta(hours=state_attr(t, 'hour'), minutes=state_attr(t, 'minute'), seconds=state_attr(t, 'second')) }}
      {% else %}
          {{ state_attr('sun.sun', 'next_setting') | as_datetime - timedelta(hours=state_attr(t, 'hour'), minutes=state_attr(t, 'minute'), seconds=state_attr(t, 'second')) }}
      {% endif %}