mrochette01
(Michel Rochette)
December 16, 2021, 4:25pm
1
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
123
(Taras)
December 16, 2021, 4:33pm
2
What are the possible values of “x and y minutes”? Are they always positive values, negative values, or can be both?
mrochette01
(Michel Rochette)
December 16, 2021, 4:42pm
3
Ideally I would like to be able to do it with negative as well as positive values
123
(Taras)
December 16, 2021, 4:52pm
4
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?
mrochette01
(Michel Rochette)
December 16, 2021, 4:54pm
5
Interesting solution avenue, but how?
123
(Taras)
December 16, 2021, 5:47pm
6
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
mrochette01
(Michel Rochette)
December 16, 2021, 6:12pm
7
Very interesting, I’ll give it a try.
mrochette01
(Michel Rochette)
December 16, 2021, 6:38pm
8
I have integrated it in “configuration.yaml”.
I will now modify an “automation” for tonight.
123
(Taras)
December 16, 2021, 6:46pm
9
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
mrochette01
(Michel Rochette)
December 16, 2021, 6:54pm
10
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)
mrochette01
(Michel Rochette)
December 16, 2021, 7:44pm
11
Thank you
The whole thing works very well
Tsar
(Christian)
January 13, 2022, 2:17pm
12
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
123
(Taras)
January 13, 2022, 2:40pm
13
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')) }}
Tsar
(Christian)
January 13, 2022, 4:38pm
14
That’s it, thank you !
But my trigger goes not off
{{ 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 ?
Tsar
(Christian)
January 14, 2022, 9:53am
15
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
123
(Taras)
January 14, 2022, 1:24pm
16
Tsar:
but there’s is an error
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.
Tsar
(Christian)
January 14, 2022, 1:35pm
17
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 %}