dawnlord
(Christer)
March 5, 2018, 11:44am
1
I want to have my coffee started before i wake up.
Is it possible to do that with offset?
This is what i tried but did not work.
- alias: 'Kaffe'
initial_state: true
trigger:
platform: template
value_template: '{{ states.sensor.time.state == states.sensor.alarm_time.state }}'
offset: "-00:20:00"
condition:
- condition: state
entity_id: group.all_devices
state: home
- condition: state
entity_id: input_boolean.coffeornot
state: 'on'
- condition: state
entity_id: input_select.status_hemma
state: 'Hemma'
action:
- service: homeassistant.turn_on
entity_id:
- switch.net_perkulator
- switch.perkulator
lolouk44
(lolouk44)
March 5, 2018, 12:28pm
2
dawnlord:
states.sensor.time.state
That would not work indeed as your value template would return true or false (which is good for triggers), but the offset canāt be applied to a true/false statement.
Can you tell me what your states.sensor.alarm_time.state
returns?
Based on what you have there I can get you a formula for your value_templateā¦
lolouk44
(lolouk44)
March 5, 2018, 1:02pm
3
assuming your alarm_time is formatted in āhh:mmā, try and replace your code as such:
- alias: 'Kaffe'
initial_state: true
trigger:
platform: template
value_template: '{{ ((as_timestamp(now()) +1200 ) | timestamp_custom('%H:%M')) == states.sensor.alarm_time.state }}'
condition:
- condition: state
entity_id: group.all_devices
state: home
- condition: state
entity_id: input_boolean.coffeornot
state: 'on'
- condition: state
entity_id: input_select.status_hemma
state: 'Hemma'
action:
- service: homeassistant.turn_on
entity_id:
- switch.net_perkulator
- switch.perkulator
Iāve not tried it with a real instance of HA, so Iād probably add a condition that your automation hasnāt already ran in the last 2 min to make sure it doesnāt loop for a whole minute
- condition: template
value_template: '{{ (as_timestamp(now()) - as_timestamp(states.automation.kaffe.attributes.last_triggered | default(0)) | int > 120)}}'
If your automation doesnāt trigger, move your the value_template from your trigger in your conditions and set the trigger to be:
trigger:
- platform: time
minutes: '/1'
seconds: 0
so it runs every minute
dawnlord
(Christer)
March 5, 2018, 1:05pm
4
Here is the sensors:
# time
- platform: time_date
display_options:
- 'time'
- 'date'
- 'date_time'
- 'time_date'
- 'time_utc'
scan_interval: 30
- platform: template
sensors:
alarm_time:
friendly_name: 'Alarmtid'
value_template: '{{ "%0.02d:%0.02d" | format(states("input_number.alarmhour") | int, states("input_number.alarmminutes") | int) }}'
dawnlord
(Christer)
March 5, 2018, 1:08pm
5
Am i thinking wrong?
To have the trigger 20 minutes before alarm
{{ ((as_timestamp(now()) +1200 )
Should be
{{ ((as_timestamp(now()) -1200 )
dawnlord
(Christer)
March 5, 2018, 1:25pm
6
Actually i would like to use the same input_number values as in this sensor:
- platform: template
sensors:
alarm_time:
friendly_name: 'Alarmtid'
value_template: '{{ "%0.02d:%0.02d" | format(states("input_number.alarmhour") | int, states("input_number.alarmminutes") | int) }}'
Would it be an option to instead create another sensor using the same input number selections but with an offset of minus 20 minutes?
lolouk44
(lolouk44)
March 5, 2018, 1:55pm
7
if you want your alarm to come 20 mins early, you need to add 20 min to now() and check if now + 20 min = your alarm time. (as now + 20 min is your alarm - min 20)
lolouk44
(lolouk44)
March 5, 2018, 2:14pm
8
ok try and replace the value template with this:
'{{ (((as_timestamp(now()) +1200 ) | timestamp_custom('%H')) == states.input_number.alarmhour.state ) and (((as_timestamp(now()) +1200 ) | timestamp_custom('%M')) == states.input_number.alarmminutes.state ) }}'
But itās starting to get quite complexā¦
dawnlord
(Christer)
March 5, 2018, 2:25pm
9
I get an error with that code
bad indentation of a mapping entry at line 688, column 80:
ā¦ ()) +1200 ) | timestamp_custom(ā%Hā)) == states.input_number.ala ā¦
lolouk44
(lolouk44)
March 5, 2018, 2:44pm
10
sorry the code was for the automation. It takes into account the offset of 20 min and then compare the time (hour and min) to make sure it matches the alarmā¦
dawnlord
(Christer)
March 5, 2018, 3:02pm
11
I got the same error on the automation and having same indentation as other value_templates
I am using the following for my automation to start half an hour before the alarm clock:
trigger:
platform: template
value_template: '{{ states.sensor.time.state == states.sensor.alarm_clock_time_less_30.state }}'
And this is the template that takes off 30 minutes:
alarm_clock_time_less_30:
value_template: >-
{%if (states("input_number.alarm_clock_minutes") | int >= 30) %}
{{ "%0.02d:%0.02d" | format(states("input_number.alarm_clock_hour") | int, states("input_number.alarm_clock_minutes") | int -30) }}
{% else %}
{{ "%0.02d:%0.02d" | format(states("input_number.alarm_clock_hour") | int -1, states("input_number.alarm_clock_minutes") | int +30) }}
{% endif %}
Hope this helps
1 Like
lolouk44
(lolouk44)
March 5, 2018, 3:50pm
13
you need to check you code and indentations then. Iāve just checked the value_template formula again and there are no typo or errors.
Restore to a previous saved time, then add items bit by bit to see when it fails.
dawnlord
(Christer)
March 5, 2018, 4:25pm
14
Thank you both for helping
1 Like
I tried to amend this to be only 5 minutes and it isnāt working.
Any suggestions?
I adjusted it to be 45 minutes and that works fine. if you sum the first and the second part it needs to be 60.
{%if (states("input_number.alarm_clock_minutes") | int >= 5) %}
{{ "%0.02d:%0.02d" | format(states("input_number.alarm_clock_hour") | int, states("input_number.alarm_clock_minutes") | int -5) }}
{% else %}
{{ "%0.02d:%0.02d" | format(states("input_number.alarm_clock_hour") | int -1, states("input_number.alarm_clock_minutes") | int +55) }}
{% endif %}```
1 Like
RickTSD
(Rick de Jong)
March 29, 2020, 11:24am
17
I must be an idiotā¦ But where do I place the template? In a new script, automationā¦? Iāve tried several things, but I canāt get it to work
LaurensBot
(Laurens Bot)
March 29, 2020, 12:14pm
18
Hardly
The one with ātriggerā goes into an automation. The other blob goes into a sensor (sensor template).
Let me know if you figured it out with the above pointers, if not I will write a bit more when I am done with work.
RickTSD
(Rick de Jong)
March 29, 2020, 1:07pm
19
Not quiet. This is what iāve got
Sensors
sensor:
- platform: template
alarm_clock_time_less_30:
value_template: >-
{%if (states("input_number.alarmhour") | int >= 30) %}
{{ "%0.02d:%0.02d" | format(states("input_number.alarmminutes") | int, states("input_number.alarmhour") | int -30) }}
{% else %}
{{ "%0.02d:%0.02d" | format(states("input_number.alarmminutes") | int -1, states("input_number.alarmhour") | int +30) }}
{% endif %}
The automation is as
- id: '1092782611721'
alias: Wake Me Up
trigger:
platform: template
value_template: '{{ states.sensor.time.state == states.sensor.alarm_clock_time_less_30.state}}'
condition:
condition: or
conditions:
- condition: and
conditions:
- condition: state
entity_id: input_boolean.alarmweekday
state: 'on'
- condition: time
weekday:
- mon
- tue
- wed
- thu
- fri
- condition: state
entity_id: input_boolean.alarmweekday
state: 'off'
action:
service: notify.notify
data_template:
message: 'Test wekker'
title: ''
The input for the alarm is
alarmhour:
name: Hour
icon: mdi:timer
min: 0
max: 23
step: 1
alarmminutes:
name: Minutes
icon: mdi:timer
min: 0
max: 59
step: 5
I canāt get the sensor to show up in entities and also the alarm doesnāt notify me half an hour beforeā¦
What I do see in the entities however is this:
[sensor.alarm_time] | 15:30 | friendly_name: Time
Perhaps thatās something I can work with?
AhmadK
(akasma74)
March 30, 2020, 7:54pm
20
You need to pay more attention to the information youāre using.
And consult with the official HA docs - take a look .
nope.
and where did you get your code from - this does not make sense to me
as itās
Hereās your template sensor that should work as expected:
sensor:
- platform: template
sensors:
alarm_clock_time_less_30:
value_template: >-
{% set alarmhour = states("input_number.alarmhour") | int %}
{% set alarmminutes = states("input_number.alarmminutes") | int %}
{% if alarmminutes >= 30 %}
{% set alarmminutes = alarmminutes - 30 %}
{% else %}
{% set alarmhour = alarmhour - 1 %}
{% set alarmminutes = alarmminutes + 30 %}
{% endif %}
{{ "{:02d}:{:02d}".format(alarmhour, alarmminutes) }}
1 Like