As well. It is often forgotten.
In this case, the as_timestamp()
function returns a float. There’s no need to convert it to int because timestamp_custom()
knows how to handle floats.
If you want you can also :
- Create an
input_datetime.ptf_farj
(doc) - Use
input_datetime.set_datetime
service to match with yoursensor.ptf_fajr
(doc) (in an automation) - Use the
platform time
, to trigger an automation (see example)
If you need help
I totally agree.
This is just for the example, and the introduction of filters which can lead to the reflection of ‘’ hey! What is that ? What’s the point ?)
Right. That’s exactly what I had asked myself and my answer was: There is no point to it.
I understand your point of view and I do not share it.
Maybe 20 years ago (even 10 years ago), but today, with our processor, ram, storage …
On the other hand, I will review the int filter because I think that it can generate rounding errors …
I have only participated in this community for just over 2 years, but that’s the oddest explanation I have read for including unnecessary things in a template.
Whereas most of the experienced users here strive for efficiency, you feel today’s computing power is the justification for verbose templates. OK, whatever fiction works for you.
BTW, no need to be concerned about rounding errors with int
(i.e. eliminates microseconds from the float value). The timestamp_custom()
function is already “rounding” to the nearest minute because its argument is %H:%M
.
I agreed to the fact that I forgot to specify that it was not necessary, and the correction was made in the post (before? during? writing your message?)
I don’t pretend to say that my code is perfect and super super fast …
I simply respond, in my own way, to an answer.
I used to put more than necessary to show (make discover) to beginners.
My posts are always intended for newbies, not experts.
So, it is true that if I want to introduce something, I do it but generally I do not forget to specify it …
I didn’t kown that. Where can i read this ? (because i think it’s not the only thing i don’t know)
We can denigrate my code also on the fact of specifying that the time to be displayed is local and not utc is not necessary because it is the default display (as shown by the use of your code for example) .
But, I find this to be useful information … ( in fiction which works for me , )
In the future, I would clarify that my answer introduces several notions (not useless, but superfluous).
Thank you for your feedback
Just to add that I would take into account even a user that is 1 hour old…
it did not work but i did get an error in the log 5 min after fajr
2020-12-08 05:45:00 ERROR (MainThread) [homeassistant] Error doing job: Exception in callback async_track_point_in_utc_time.<locals>.run_action() at /usr/src/homeassistant/homeassistant/helpers/event.py:1137
Traceback (most recent call last):
File "/usr/local/lib/python3.8/asyncio/events.py", line 81, in _run
self._context.run(self._callback, *self._args)
File "/usr/src/homeassistant/homeassistant/helpers/event.py", line 1156, in run_action
hass.async_run_hass_job(job, utc_point_in_time)
File "/usr/src/homeassistant/homeassistant/core.py", line 434, in async_run_hass_job
hassjob.target(*args)
File "/usr/src/homeassistant/homeassistant/helpers/event.py", line 1362, in pattern_time_change_listener
hass.async_run_hass_job(job, dt_util.as_local(now) if local else now)
File "/usr/src/homeassistant/homeassistant/core.py", line 434, in async_run_hass_job
hassjob.target(*args)
File "/usr/src/homeassistant/homeassistant/helpers/event.py", line 811, in _refresh_from_time
self._refresh(None, track_templates=track_templates)
File "/usr/src/homeassistant/homeassistant/helpers/event.py", line 960, in _refresh
self.hass.async_run_hass_job(self._job, event, updates)
File "/usr/src/homeassistant/homeassistant/core.py", line 434, in async_run_hass_job
hassjob.target(*args)
File "/usr/src/homeassistant/homeassistant/components/template/trigger.py", line 55, in template_listener
entity_id = event.data.get("entity_id")
AttributeError: 'NoneType' object has no attribute 'data'
Can you share all code of automation ?
yes i can
Edit: i fixed a typo from quarn to quran and renamed the folder on the system.
- id: '1602013093065'
alias: Play Adhan
description: ''
trigger:
- platform: state
entity_id: input_boolean.adhan
from: 'off'
to: 'on'
condition: []
action:
- service: media_player.media_pause
data: {}
entity_id: media_player.home_group
- service: media_player.volume_set
data:
volume_level: 0.55
entity_id: media_player.living_room_speaker
- service: media_player.volume_set
data:
volume_level: 0.55
entity_id: media_player.master_bedroom_speaker
- service: media_player.volume_set
data:
volume_level: 0.55
entity_id: media_player.office_speaker
- service: media_player.play_media
data:
media_content_id: http://10.0.0.4:8123/local/mba.mp3
media_content_type: music
entity_id: media_player.home_group
mode: single
- id: '1602895411112'
alias: Quran
description: ''
trigger:
- platform: state
entity_id: input_boolean.quarn
from: 'off'
to: 'on'
condition: []
action:
- service: media_player.volume_set
data:
volume_level: 0.75
entity_id: media_player.living_room_speaker
- service: media_player.play_media
data:
media_content_type: music
media_content_id: http://10.0.0.4:8123/local/quarn/{{range(1, 114)|random}}.mp3
entity_id: media_player.living_room_speaker
- delay: '5'
- service: input_boolean.turn_off
data: {}
entity_id: input_boolean.quarn
mode: single
- id: '1603220813524'
alias: Auto For Fajr
description: ''
trigger:
- platform: template
value_template: '{{ states("sensor.time_utc") == states("sensor.salah_fajr") }}'
condition: []
action:
- service: input_boolean.turn_on
data: {}
entity_id: input_boolean.adhan
- delay: '30'
- service: input_boolean.turn_off
data: {}
entity_id: input_boolean.adhan
- delay: '1140'
- service: input_boolean.turn_on
data: {}
entity_id: input_boolean.fajr_quran
mode: single
- id: '1603220882286'
alias: Auto Dhuhr Prayer
description: ''
trigger:
- platform: template
value_template: '{{ states("sensor.time_utc") == states("sensor.salah_dhuhr")
}}'
condition: []
action:
- service: input_boolean.turn_on
data: {}
entity_id: input_boolean.adhan
- delay: '30'
- service: input_boolean.turn_off
data: {}
entity_id: input_boolean.adhan
- delay: '1140'
- service: input_boolean.turn_on
data: {}
entity_id: input_boolean.fajr_quran
mode: single
- id: '1603220941015'
alias: Auto Asr Prayer
description: ''
trigger:
- platform: template
value_template: '{{ states("sensor.time_utc") == states("sensor.salah_asr") }}'
condition: []
action:
- service: input_boolean.turn_on
data: {}
entity_id: input_boolean.adhan
- delay: '30'
- service: input_boolean.turn_off
data: {}
entity_id: input_boolean.adhan
- delay: '1140'
- service: input_boolean.turn_on
data: {}
entity_id: input_boolean.fajr_quran
mode: single
- id: '1603221001240'
alias: Auto Maghrib Prayer
description: ''
trigger:
- platform: template
value_template: '{{ states("sensor.time_utc") == states("sensor.salah_maghrib")
}}'
condition: []
action:
- service: input_boolean.turn_on
data: {}
entity_id: input_boolean.adhan
- delay: '30'
- service: input_boolean.turn_off
data: {}
entity_id: input_boolean.adhan
- delay: '1140'
- service: input_boolean.turn_on
data: {}
entity_id: input_boolean.fajr_quran
mode: single
- id: '1603221063694'
alias: Auto Isha Prayer
description: ''
trigger:
- platform: template
value_template: '{{ states("sensor.time_utc") == states("sensor.salah_isha") }}'
condition: []
action:
- service: input_boolean.turn_on
data: {}
entity_id: input_boolean.adhan
- delay: '30'
- service: input_boolean.turn_off
data: {}
entity_id: input_boolean.adhan
- delay: '1140'
- service: input_boolean.turn_on
data: {}
entity_id: input_boolean.fajr_quran
mode: single
- id: '1603221439747'
alias: Play Sura 67 & 36
description: ''
trigger:
- platform: state
entity_id: input_boolean.play_yaseen_and_mulk
from: 'off'
to: 'on'
condition: []
action:
- service: media_player.play_media
data:
media_content_type: music
media_content_id: http://10.0.0.4:8123/local/quarn/67.mp3
entity_id: media_player.living_room_speaker
- delay: '381'
- service: media_player.play_media
data:
media_content_type: music
media_content_id: http://10.0.0.4:8123/local/quarn/36.mp3
entity_id: media_player.living_room_speaker
- service: input_boolean.turn_off
data: {}
entity_id: input_boolean.play_yaseen_and_mulk
mode: single
- id: '1603223315797'
alias: Fajr Quran
description: ''
trigger:
- platform: state
entity_id: input_boolean.fajr_quran
from: 'off'
to: 'on'
condition: []
action:
- service: media_player.volume_set
data:
volume_level: 0.75
entity_id: media_player.living_room_speaker
- delay: '5'
- service: media_player.play_media
data:
media_content_type: music
media_content_id: http://10.0.0.4:8123/local/quarn/112.mp3
entity_id: media_player.living_room_speaker
- delay: '31'
- service: media_player.play_media
data:
media_content_type: music
media_content_id: http://10.0.0.4:8123/local/quarn/113.mp3
entity_id: media_player.living_room_speaker
- delay: '41'
- service: media_player.play_media
data:
media_content_type: music
media_content_id: http://10.0.0.4:8123/local/quarn/114.mp3
entity_id: media_player.living_room_speaker
- service: input_boolean.turn_off
data: {}
entity_id: input_boolean.fajr_quran
mode: single
- id: '1604738624931'
alias: Quran Before Fajr
description: ''
trigger:
- platform: template
value_template: '{{ states("sensor.ptf_fajr") == as_timestamp(now() - timedelta(
hours = 0, minutes = 5 ))| int | timestamp_custom(''%H:%M'', true) }}'
condition: []
action:
- service: input_boolean.turn_on
entity_id: input_boolean.fajr_quran
mode: single
You have post all automations…
Can you share only all automation in error ?
but in the extract code :
value_template: '{{ states("sensor.ptf_fajr") == as_timestamp(now() - timedelta( hours = 0, minutes = 5 ))| int | timestamp_custom(''%H:%M'', true) }}'
I think you should put in the form (watch out for the indentation) :
- platform: template
value_template: >-
{{
states("sensor.ptf_fajr")
==
as_timestamp(now() - timedelta(hours = 0, minutes = 5 ))| int | timestamp_custom(''%H:%M'', true)
}}
condition:
or in single line :
value_template: '{{ states("sensor.ptf_fajr") == as_timestamp(now() - timedelta(hours = 0, minutes = 5 ))| int | timestamp_custom("%H:%M", true) }}'
In notepad++ :
(bad indentation)
(good indentation)
you mix the apostrophes and the quotes and you must not do it
value_template: '{{ states("sensor.ptf_fajr") == as_timestamp(now() - timedelta(hours = 0, minutes = 5 ))| int | timestamp_custom(''%H:%M'', true) }}'
should be
value_template: '{{ states("sensor.ptf_fajr") == as_timestamp(now() - timedelta(hours = 0, minutes = 5 ))| int | timestamp_custom("%H:%M", true) }}'
this is how i enter it.
In GUI
Ignores my previous remark (I learned something suddenly with quotes )
As the documentation states, using the now () function is dangerous. (here, search ‘now()’)
So, replace the code with now()
with states.sensor.time.last_changed
.
{{ states("sensor.ptf_fajr") == as_timestamp(states.sensor.time.last_changed - timedelta(minutes = 5)) | timestamp_custom("%H:%M") }}
I removed the superfluous (@123 )
The documentation is outdated and the restriction no longer applies. There’s no need to replace now()
with sensor.time
.
As of version 0.117, the now()
function can serve as an auto-updating time reference in a template.
From 0.117’s Release Notes:
Templates - Auto-updating now()
The
entity_id:
attribute was recently removed from template entities because the templates are now able to find all referenced entities automatically. This removal introduced a new problem where templates using the current time could no longer useentity_id: sensor.time
to ensure periodic updates.With apologies to those who have spent the last couple of releases adding workarounds to their templates, we are now introducing an auto-refresh feature to time-based templates.
It is thus no longer necessary to reference
sensor.time
,sensor.date
or manually update template entities whennow()
orutcnow()
is present in the template.The template will automatically be updated when:
- A referenced entity changes state.
- At the start of each minute when
now()
orutcnow()
is present in the template.Please note, if you have a time-based template where you do not want it to update periodically it will now have to be reworked to not use
now()
.
Ultimately, it’s as simple as this:
{{ states('sensor.ptf_fajr') == (now().timestamp() - 300) | timestamp_custom('%H:%M') }}
ok i have figured it out by taking what i have learned from @clemalex and @123 this is how i did it
in configuration.yaml i added a test entry.
salah_dhuhr_test:
friendly_name: "Dhuhr test"
value_template: >
{{ (now().timestamp() + 300) | timestamp_custom('%H:%M') }}
and in my automations.yaml
- id: '1604738624931'
alias: Quran Before Fajr
description: ''
trigger:
- platform: template
value_template: '{{ states("sensor.ptf_dhuhr") == states("sensor.salah_dhuhr_test")
}}'
condition: []
action:
- service: input_boolean.turn_on
entity_id: input_boolean.fajr_quran
mode: single
and this fired off 5 min before dhuhr so now all i have to do is change it so it reflects fajr prayer. I want to thank all of you who have helped me get this working. esp you @clemalex for putting in the reference doc for me to look at.
I tried your suggestion and it seems not work in the trigger …
I have this error (like @tigan earlier in the day) :
2020-12-08 20:01:00 ERROR (MainThread) [homeassistant] Error doing job: Exception in callback async_track_point_in_utc_time.<locals>.run_action() at /usr/src/homeassistant/homeassistant/helpers/event.py:1137
Traceback (most recent call last):
File "/usr/local/lib/python3.8/asyncio/events.py", line 81, in _run
self._context.run(self._callback, *self._args)
File "/usr/src/homeassistant/homeassistant/helpers/event.py", line 1156, in run_action
hass.async_run_hass_job(job, utc_point_in_time)
File "/usr/src/homeassistant/homeassistant/core.py", line 434, in async_run_hass_job
hassjob.target(*args)
File "/usr/src/homeassistant/homeassistant/helpers/event.py", line 1362, in pattern_time_change_listener
hass.async_run_hass_job(job, dt_util.as_local(now) if local else now)
File "/usr/src/homeassistant/homeassistant/core.py", line 434, in async_run_hass_job
hassjob.target(*args)
File "/usr/src/homeassistant/homeassistant/helpers/event.py", line 811, in _refresh_from_time
self._refresh(None, track_templates=track_templates)
File "/usr/src/homeassistant/homeassistant/helpers/event.py", line 960, in _refresh
self.hass.async_run_hass_job(self._job, event, updates)
File "/usr/src/homeassistant/homeassistant/core.py", line 434, in async_run_hass_job
hassjob.target(*args)
File "/usr/src/homeassistant/homeassistant/components/template/trigger.py", line 55, in template_listener
entity_id = event.data.get("entity_id")
AttributeError: 'NoneType' object has no attribute 'data'
with the code :
automation:
- alias: test_template_01
description: test_template_01
id: test_template_01
trigger:
- platform: template
value_template: >-
{{ (states("input_datetime.my_custom_time") == (now().timestamp() - 0) | timestamp_custom("%H:%M:%S", true) ) }}
condition: []
action:
- service: persistent_notification.create
data:
message: "Toast"
title: "Titre Toast"
notification_id: "{{ (range(1, 9999)|random) }}"
But it’s work with my code :
- alias: test_template_02
id: test_template_02
description: test_template_02
trigger:
- platform: template
value_template: >-
{{ states("input_datetime.my_custom_time") == as_timestamp(states.sensor.time.last_changed - timedelta(minutes = 0)) | timestamp_custom("%H:%M:%S") }}
condition: []
action:
- service: persistent_notification.create
data:
message: "Toast"
title: "My template"
notification_id: "{{ (range(1, 9999)|random) }}"
The only difference (apart from the error generated by your code in automation because in development tools, it works), is that my code makes the sensor.time entity appear as a listener unlike yours.
This is why I based myself on the documentation and the warning.
I had followed the story of the update following the disappearance of the entity_id: key
If you have an explanation for this error, I’m interested!
Good day, afternoon, evening, night…
Do not pay attention to the subtracted values, it was for testing
You may have discovered a software bug.
I can confirm that I get the same error message when now()
is used in a Template Trigger (otherwise it works correctly in a Template Sensor ).
In the past, if now()
was used in a Template Trigger, it would simply fail quietly (it would not trigger and it would not report an error). I tested it in both 0.117.0 and 118.3 and it fails to trigger and reports an error.
What’s interesting is that in 0.117.0, the Template Editor warns that now()
will not serve to update the template.
However, they probably overlooked to update the message, because I checked 0.117.4 and it’s corrected; it reports the presence of now()
will cause the template to be updated “at the start of each minute”. Here’s how it appears in 0.118.3
Nevertheless, it not only fails to update the Template Trigger every minute, it generates an error message caused by a problem encountered by the template_listener.
File “/usr/src/homeassistant/homeassistant/components/template/trigger.py”, line 55, in template_listener
entity_id = event.data.get(“entity_id”)
AttributeError: ‘NoneType’ object has no attribute ‘data’
It appears to be trying to identify an entity within the event that was created but that event doesn’t have it. That looks like a bug.
Here’s the automation I used for testing purposes:
- alias: Test now
description: ''
trigger:
- platform: template
value_template: "{{ '15:29' == (now().time()|string)[:5] }}"
condition: []
action:
- service: persistent_notification.create
data:
title: Test
message: Hello world!
mode: single
I’ll post this as an Issue in the Core repo.
i ran this again for Maghrib and it fired off the automation 5 minutes before the Maghrib prayer. i am confident that it is now going to work. again thank you all for the help.