Time Stamp Class Sensor

How to offset the value of time stamp class sensor in home assistant automation?

that’s a super broad question. can you be more specific about the scenario you’re trying to solve?

Thank you for the response.
I will be more specific hopefully you could help:
I installed this integration:
Islamic Prayer Times - Home Assistant (home-assistant.io)
I want to use this integration to trigger some actions based on prayer times that are provided via this integration.
This is quite easy at first, but the issue I am encountering is that I cannot make the integration trigger before the prayer time starts, for example 15 minutes before prayer time.
The point is this integration does not act like the regular time or sun entities. So, I am unable to set an offset value for the prayer time.
Hope this makes my objective clearer for you.

oh. ok. That makes sense and shouldn’t be too hard. I have not used that integration but it looks like it exposes times in a sensor. So we just need to do something like {{ states('sensor.name_of_sensor') - timedelta(minutes=15) }} or something similar to that

Do you already have an automation that is working except for the offset? Please post what you have and we can edit it for the offset.

Your question (about offsetting the time of prayer times) has been asked, and answered (including by me), several times in the past. When you searched the forum for an answer, were you unable to find them?


For example, I searched for “prayer times offset” and found many results including this solution:

it seems the results did not show up for me because i used a generic text asking about how to offset time stamp class sensor.
yet, i will go through these answers and see if they can help me.
Thanks.

1 Like

i finalized the code as per the advise from @123 Taras.

Here is the resulting code:

description: “”
mode: single
trigger:

  • platform: template
    value_template: |-
    template:
    - sensor:
    - name: Fajr Prayer Offset
    device_class: timestamp
    state: “{{ (states(‘sensor.fajr_prayer’) | as_datetime - timedelta(minutes=5)).isoformat() }}”
    condition:
    action:
  • service: light.turn_on
    metadata: {}
    data: {}
    target:
    entity_id: light.floor_lamp_socket_1

Please advise if i need to change anything.
Thanks.

glad you got it working. when you post code, please always use the pre-formatted text functionality. the formatting is significant.

as i guessed way above, the key was about timedelta(minutes= ) :slight_smile:

Thanks.
But still I have doubts it will work. You see the sensor ID is shown in the integration I have is like this:
sensor.islamic_prayer_times_fajr_prayer
while, the code that is used in the post referred to by @123 Taras is different.
Do you think I need to change anything?

i’m not sure i understand your question. it looks like you’ve defined a new sensor named fajr prayer offset which is to be 5 minutes earlier than the sensor.fajr_prayer.

you said you have doubts about it working… you don’t need to guess… put it in your code, reload the sensors (you may need to restart home assistant) and go look and see if indeed it is correctly 5 minutes before.

as expected, the code did not work. I used the sensor for midnight time with offset of 90 minutes.
the time now in my location is 10:36 PM and the automation did not work yet…

Here is the code if you could check for me:

description: ""
trigger:
  - platform: template
    value_template: |-
      template:
        - sensor:
            - name: Fajr Prayer Offset 
              device_class: timestamp
              state: "{{ (states('sensor.islamic_prayer_times_midnight_time') | as_datetime - timedelta(minutes=90)).isoformat() }}"
condition: []
action:
  - service: light.turn_on
    metadata: {}
    data: {}
    target:
      entity_id:
        - light.floor_lamp_socket_1
mode: single

Hi. When pasting the template in Templates tab under Developer page, I got this message: TypeError: unsupported operand type(s) for -: ‘NoneType’ and ‘datetime.timedelta’

What do you think this is about?

what do you get when you put this in dev tools template page?

{{ states('sensor.islamic_prayer_times_midnight_time') }}
Result type: string
2024-03-18T21:01:00+00:00
This template listens for the following state changed events:

Entity: sensor.islamic_prayer_times_midnight_time

try this:

description: ""
trigger:
  - platform: template
    value_template: >-
      {{ ((states('sensor.islamic_prayer_times_midnight_time') | as_datetime) -
          now()) == timedelta(minutes=90)}}
condition: []
action:
  - service: light.turn_on
    target:
      entity_id:
        - light.floor_lamp_socket_1
mode: single

I added now. But need to wait until 10:30 pm our time to see if it works.

try what i posted above.
note, if you grabbed it earlier, grab it again. i made a change to one part.

Done.
Thank you.

by the way… you don’t need to wait till tonight if you don’t want to… i don’t know what timezone you are in, but take the number minutes till midnight and change the 90 to something close to now…

I just did… it was supposed to run 5 minutes ago. But nothing happened.