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
RickTSD
(Rick de Jong)
March 31, 2020, 6:43am
21
AhmadK:
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) }}
This does the trick :)! Thanks!!
The code I’ve got was from the post above:
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, s…
But this one is done, thanks again!