Hi, I am using Islamic Prayer Time integration, however the triggered time are not exactly same in my location. I would like to know if it’s possible to offset time? Based on the time triggered by the integration I would like to add or reduce time…
sensor:
- platform: time_date
display_options:
- 'time'
- 'date'
- 'time_date'
- platform: template
sensors:
salah_fajr:
friendly_name: "Salah Fajr"
value_template: >
{%- set a = states("sensor.fajr_prayer").split("T")[1].split(":")[0] -%}
{%- set b = states("sensor.fajr_prayer").split("T")[1].split(":")[1] -%}
{{ a + ":" + b }}
salah_dhuhr:
friendly_name: "Salah Dhuhr"
value_template: >
{%- set a = states("sensor.dhuhr_prayer").split("T")[1].split(":")[0] -%}
{%- set b = states("sensor.dhuhr_prayer").split("T")[1].split(":")[1] -%}
{{ a + ":" + b }}
salah_asr:
friendly_name: "Salah Asr"
value_template: >
{%- set a = states("sensor.asr_prayer").split("T")[1].split(":")[0] -%}
{%- set b = states("sensor.asr_prayer").split("T")[1].split(":")[1] -%}
{{ a + ":" + b }}
salah_maghrib:
friendly_name: "Salah Maghrib"
value_template: >
{%- set a = states("sensor.maghrib_prayer").split("T")[1].split(":")[0] -%}
{%- set b = states("sensor.maghrib_prayer").split("T")[1].split(":")[1] -%}
{{ a + ":" + b }}
salah_isha:
friendly_name: "Salah Isha"
value_template: >
{%- set a = states("sensor.isha_prayer").split("T")[1].split(":")[0] -%}
{%- set b = states("sensor.isha_prayer").split("T")[1].split(":")[1] -%}
{{ a + ":" + b }}
Use a Template Trigger in your automation. The following example adds an offset of 5 minutes (300 seconds) to each time but you can change it to whatever you need (you can also subtract the offset).
- alias: 'Example 1'
trigger:
- platform: template
value_template: >
{% set f = as_timestamp(states('sensor.fajr_prayer')) + 300 %}
{% set d = as_timestamp(states('sensor.dhuhr_prayer')) + 300 %}
{% set a = as_timestamp(states('sensor.asr_prayer')) + 300 %}
{% set i = as_timestamp(states('sensor.isha_prayer')) + 300 %}
{% set m = as_timestamp(states('sensor.maghrib_prayer')) + 300 %}
{{ now().timestamp() | int in [f, d, a, i, m] }}
action:
... etc ...
Hi @123 thanks for quick reply, should I add this at the end of my current automation? Sorry I am still learning HA stuff… Here is my actual automation code…
I just applied your code however I don’t see the Time updated on my Dashboard. I would expect time to shift +5min for each entry
I guess the automation will still trigger with +5min but in Dasboard the time can’t be shifted?? Can I also add the same in Configuration
What I provided is the means of triggering your automation at each prayer time plus an offset (the example shows an extra 5 minutes but it can be anything you want). It does not modify the values of the 5 prayer sensors nor does it create 5 new prayer sensors showing an offset time.
If what you want is to create 5 new prayer sensors with an offset time, it’s possible (but not mandatory for triggering the automation).
I understood sorry for the confusion… automation looks good. I have in my cornfugraiton file the sensors, can we modify same as automation to add the offset time?
# start ici
homeassistant:
# Name of the location where Home Assistant is running
name: Home
# Location required to calculate the time the sun rises and sets
#### Initial location values for latitude, logitude, elevation, usint_system, and time_zone are calculated from the IP address used when installation occured.
#### Easiest place to get precice info is - https://www.maps.ie/coordinates.html
latitude: 65.623438805139294
longitude: -93.59339699662624
# Impacts weather/sunrise data (altitude above sea level in meters):
elevation: 21
# metric for Metric, imperial for Imperial
unit_system: metric
# Pick yours from here: http://www.timezoneconverter.com/cgi-bin/findzone.tzc
time_zone: America/Toronto
# end ici
# Configure a default setup of Home Assistant (frontend, api, etc)
default_config:
#start ici
media_extractor:
# Track the sun
sun:
# Islamic prayer times integration
islamic_prayer_times:
calculation_method: mwl
# Sensors
sensor:
- platform: time_date
display_options:
- 'time'
- 'date'
- 'time_date'
- platform: template
sensors:
salah_fajr:
friendly_name: "Sabah Namazi(Fajr)"
value_template: >
{%- set a = states("sensor.fajr_prayer").split("T")[1].split(":")[0] -%}
{%- set b = states("sensor.fajr_prayer").split("T")[1].split(":")[1] -%}
{{ a + ":" + b }}
salah_dhuhr:
friendly_name: "Öğle Namazı(Dhuhr)"
value_template: >
{%- set a = states("sensor.dhuhr_prayer").split("T")[1].split(":")[0] -%}
{%- set b = states("sensor.dhuhr_prayer").split("T")[1].split(":")[1] -%}
{{ a + ":" + b }}
salah_asr:
friendly_name: "İkindi Namazı(Asr)"
value_template: >
{%- set a = states("sensor.asr_prayer").split("T")[1].split(":")[0] -%}
{%- set b = states("sensor.asr_prayer").split("T")[1].split(":")[1] -%}
{{ a + ":" + b }}
salah_maghrib:
friendly_name: "Akşam Namazı(Maghrib)"
value_template: >
{%- set a = states("sensor.maghrib_prayer").split("T")[1].split(":")[0] -%}
{%- set b = states("sensor.maghrib_prayer").split("T")[1].split(":")[1] -%}
{{ a + ":" + b }}
salah_isha:
friendly_name: "Yatsı Namazı(Isha)"
value_template: >
{%- set a = states("sensor.isha_prayer").split("T")[1].split(":")[0] -%}
{%- set b = states("sensor.isha_prayer").split("T")[1].split(":")[1] -%}
{{ a + ":" + b }}
- platform: time_date
display_options:
- 'time'
- 'date'
- 'date_time'
- 'date_time_iso'
- 'time_date'
- 'time_utc'
- 'beat'
#end ici
# Text to speech
tts:
- platform: google_translate
group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml
I place all of my sensors in sensor.yaml and and include in configuration.yaml. You can chose to include directly in configuration.yaml. you will need (You have in your configuration file).
sensor:
-platform: template
sensors:
above the code I gave you to include in your configuration.yaml
If you are creating a sensor.yaml file
sensor: !include sensors.yaml #<-- in configuration.yaml
and your sensors.yaml file will begin with:
- platform: template
sensors:
as 123 taras has confirmed my value template that I gave is not correct.
As you have also confirmed, the suggestion offered by AllHailJ is incorrect because the template produces an integer value (a Unix Timestamp) and so doesn’t display any meaningful time and is not easily usable for triggering an automation.
The template is incorrect because it attempts to add an integer (the result of the as_timestamp function) to a timedelta object. You can add a timedelta object to a datetime object but not to a simple integer value.
@123 last question
Now my automation works and configuration also…
I have 5 google nest product in my house… In my automation how I can add the other products (I have them already integrated in HA)
If you can just help me adding 2nd device I will just apply same for the rest…
I would like if possible add 2sec delay for the 2nd device to start.
I guess the easiest way is just to add by comma in entity_id in two place but how I can do this with 2sec delay and different volume level for the 2nd device
# Automation for 5 times Azan
alias: Adhan
trigger:
- platform: template
value_template: >
{% set f = as_timestamp(states('sensor.fajr_prayer')) + 1320 %}
{% set d = as_timestamp(states('sensor.dhuhr_prayer')) + 300 %}
{% set a = as_timestamp(states('sensor.asr_prayer')) + 240 %}
{% set i = as_timestamp(states('sensor.isha_prayer')) + 420 %}
{% set m = as_timestamp(states('sensor.maghrib_prayer')) - 1920 %}
{{ now().timestamp() | int in [f, d, a, i, m] }}
action:
- service: media_extractor.play_media
data:
entity_id: media_player.basement_speaker
media_content_id: https://www.youtube.com/watch?v=nWnzLwEqxZk
media_content_type: audio/youtube
- service: media_player.volume_set
data:
entity_id: media_player.basement_speaker
volume_level: '0.2'
The following automation is untested but I believe it will work. You will need to customize the players variable so that it contains the names of your media_players and the desired volume level for each one.
The automation will play the chosen media on the first media_player, set its volume, pause 2 seconds then repeat the process for the second, third, etc media_players.
alias: Adhan
trigger:
- platform: template
value_template: >
{% set f = as_timestamp(states('sensor.fajr_prayer')) + 1320 %}
{% set d = as_timestamp(states('sensor.dhuhr_prayer')) + 300 %}
{% set a = as_timestamp(states('sensor.asr_prayer')) + 240 %}
{% set i = as_timestamp(states('sensor.isha_prayer')) + 420 %}
{% set m = as_timestamp(states('sensor.maghrib_prayer')) - 1920 %}
{{ now().timestamp() | int in [f, d, a, i, m] }}
action:
- variables:
players: [ {'player': 'basement_speaker', 'volume': 0.2}, {'player': 'kitchen_speaker', 'volume': 0.5}, {'player': 'bedroom_speaker', 'volume': 0.3}, {'player': 'guest_speaker', 'volume': 0.2}, {'player': 'hallway_speaker', 'volume': 0.6} ]
- repeat:
count: "{{ players | count }}"
sequence:
- variables:
player: 'media_player.{{ players[repeat.index-1].player }}'
volume: '{{ players[repeat.index-1].volume }}'
- service: media_extractor.play_media
data:
entity_id: "{{ player }}"
media_content_id: https://www.youtube.com/watch?v=nWnzLwEqxZk
media_content_type: audio/youtube
- service: media_player.volume_set
data:
entity_id: "{{ player }}"
volume_level: "{{ volume }}"
- delay: '00:00:02'
In the automation I tried to disable first prayer by adding # in front of the first variable template
value_template: >
# {% set f = as_timestamp(states('sensor.fajr_prayer')) + 1320 %}
{% set d = as_timestamp(states('sensor.dhuhr_prayer')) + 300 %}
{% set a = as_timestamp(states('sensor.asr_prayer')) + 240 %}
{% set i = as_timestamp(states('sensor.isha_prayer')) + 420 %}
{% set m = as_timestamp(states('sensor.maghrib_prayer')) - 1920 %}
{{ now().timestamp() | int in [f, d, a, i, m] }}
getting
bad indentation of a mapping entry at line 6, column 7:
{% set d = as_timestamp(states(’ …
^