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.
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= )
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.