Offset time

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…

# Automation for 5 times Azan
- action:
  - alias: 'Azan'
    data:
      entity_id: media_player.basement_speaker
      media_content_id: https://www.youtube.com/watch?v=nWnzLwEqxZk
      media_content_type: audio/youtube
    service: media_extractor.play_media
  - data:
      entity_id: media_player.basement_speaker
      volume_level: '0.2'
    service: media_player.volume_set
  alias: Adhan
  trigger:
#    - platform: template
#      value_template: '{{ as_timestamp(strptime(states("sensor.time_date"), "%H:%M, %Y-%m-%d")) == as_timestamp(strptime(states("sensor.fajr_prayer"), "%Y-%m-%dT%H:%M:%S")) }}'
    - platform: template
      value_template: '{{ as_timestamp(strptime(states("sensor.time_date"), "%H:%M, %Y-%m-%d")) == as_timestamp(strptime(states("sensor.dhuhr_prayer"), "%Y-%m-%dT%H:%M:%S")) }}'
    - platform: template
      value_template: '{{ as_timestamp(strptime(states("sensor.time_date"), "%H:%M, %Y-%m-%d")) == as_timestamp(strptime(states("sensor.asr_prayer"), "%Y-%m-%dT%H:%M:%S")) }}'
    - platform: template
      value_template: '{{ as_timestamp(strptime(states("sensor.time_date"), "%H:%M, %Y-%m-%d")) == as_timestamp(strptime(states("sensor.maghrib_prayer"), "%Y-%m-%dT%H:%M:%S")) }}'
    - platform: template
      value_template: '{{ as_timestamp(strptime(states("sensor.time_date"), "%H:%M, %Y-%m-%d")) == as_timestamp(strptime(states("sensor.isha_prayer"), "%Y-%m-%dT%H:%M:%S")) }}'

It replaces your automation’s existing trigger.


# Automation for 5 times Azan
alias: Adhan
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:
  - 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'

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
image

I don’t think you understood my suggestion.

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

Only values f,d,a,i,m (local variables) contain the modified value. You will have to create new sensors to hold the modified values.

In Sensors.yaml you add

      fajr_prayer_offset:
        friendly_name: "Fajr Prayer Offset Time"
        unique_id: "fajr_prayer_offset_time"
        value_template: "{{ as_timestamp(states('sensor.fajr_prayer')) + 300 }}"

I may not have the value_template set correctly.

In lovelace you use the offset sensor not the actual sensor.

I can confirm that the resulting Template Sensor is incorrect.

Do I need to create another file for the sensor offset or can i just modify the actual configuration file I posted above? Thanks

Yes you can do it in your configuration file.

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.

This is what I added in my configuration.yaml

# Sensors
sensor:
#test offset
  - platform: template
    sensors:
      fajr_prayer_offset:
        friendly_name: "Fajr Prayer Offset Time"
        unique_id: "fajr_prayer_offset_time"
        value_template: "{{ as_timestamp(states('sensor.fajr_prayer')) + 300 }}"
#test offset

Result is;
image

so you are displaying seconds.

I think the + 300 should be

+ timedelta(seconds=300)

I believe you have to convert to a string

{{ as_timestamp(states('sensor.fajr_prayer')) + timedelta(seconds=300) | timestamp_custom('%H:%M')" }}`

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.

We can but the template we need to use is slightly different.

Replace the 5 Template Sensors you currently have with this:

  - platform: template
    sensors:
      salah_fajr:
        friendly_name: "Sabah Namazi(Fajr)"
        value_template: "{{ (as_timestamp(states('sensor.fajr_prayer')) + 300) | timestamp_custom('%Y-%m-%dT%H:%M:%S+00:00', false) }}"
        device_class: timestamp
      salah_dhuhr:
        friendly_name: "Öğle Namazı(Dhuhr)"
        value_template: "{{ (as_timestamp(states('sensor.dhuhr_prayer')) + 300) | timestamp_custom('%Y-%m-%dT%H:%M:%S+00:00', false) }}"
        device_class: timestamp
      salah_asr:
        friendly_name: "İkindi Namazı(Asr)"
        value_template: "{{ (as_timestamp(states('sensor.asr_prayer')) + 300) | timestamp_custom('%Y-%m-%dT%H:%M:%S+00:00', false) }}"
        device_class: timestamp
      salah_maghrib:
        friendly_name: "Akşam Namazı(Maghrib)"
        value_template: "{{ (as_timestamp(states('sensor.maghrib_prayer')) + 300) | timestamp_custom('%Y-%m-%dT%H:%M:%S+00:00', false) }}"
        device_class: timestamp
      salah_isha:
        friendly_name: "Yatsı Namazı(Isha)"
        value_template: "{{ (as_timestamp(states('sensor.isha_prayer')) + 300) | timestamp_custom('%Y-%m-%dT%H:%M:%S+00:00', false) }}"
        device_class: timestamp

Adjust the offset value (300 seconds = 5 minutes) to whatever you need.

Here’s the result of the 5 new Template Sensors. They are all offset by 5 minutes.

For reference, here are the original 5 prayer sensors created by the Islamic Prayer integration:

@123 Thanks for your time bro. This is what I have now… if click on the remaining hours it will show the correct time with offset time we specified…

Instead of showing by remaining hours or passed hours… how I can integrate format time: so it shows corectly…

EDIT: figured out… I just added as below… not it’s working

1 Like

@123 last question :slight_smile:
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'

Thanks I will test that!

@123 Looks like it worked :slight_smile:

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(’ …
^