Template sensor time

Yes… that works

exactly, so why does the editor see it as a number, and not a timestamp? what’s more, when trying to subtract anything from it, the editor sees it as a string

Timestamps are numbers based on zero being 1970 in seconds… it’s always a number.

That’s why as_timestamp() converts a date string to a… timestamp, I.e number.

of course, I get that…duh.
sorry, starting to be a long day here now. Let me phrase it differently: how can I do what OP wants, subtract a time period from this sensor.next_alarm_timestamp.

I can’t subtract a timedelta() ( returning TypeError: unsupported operand type(s) for -: 'str' and 'datetime.timedelta') or a number (returning TypeError: unsupported operand type(s) for -: 'str' and 'int')

The alarm on the phone is set to 18:00 I want to subtract 01:17 což je 16:43 The result of your code is 17:17

My last 2 replies were not to you, but to Marius

sounds like you have a DST issue maybe ???

What time does your OS think it is ?

I had this in the template editor originally. The editor kept complaining that the output of strptime was a string and could not be added/subtracted from the deltatime. Hence the replace gymnastics I was trying.

If strptime ever returns a string, that means strptime failed. Strptime returns the input string if it fails to convert. My guess is that you were using %D for day instead of %d, and it went unnoticed in your testing.

1 Like

Yep that was it. Thank you.

Hello.
I’m following this topic and I really like your post. I really like the solution for its completeness and especially simplicity.
But let me ask you one question:
Is it possible to replace the 77 in the calculation with input_number.xxxxx for the possibility of adjusting the length of time before the alarm from the user interface?
Thank you for your contribution.

please help me out, because I can’t fix it.
How do I substract an offset from this timestamp sensor.

Use case to have a light or wakeup radio start fading in let’s say 20 minutes before the wakeup time (the timestamp).

of course, the timestamp itself is built on the sensor.alarmclock_wd_time, which has the format of the sensor.time:

or might it be easier to use the manipulation you use in the macro here?

use case:
alarmtime 07:20 - 20 min is fadein time 07:00
alarmtime 07:00 - 20 min is fadein time 06:40

got this far:

{%- macro time_in_minutes(t) %}
{%- set h, m = t.split(':') | map('int') %}
{{ h * 60 + m }}
{%- endmacro %}

{% set offset = 20 %}
{% set fadein_time = time_in_minutes(states('sensor.alarmclock_wd_time'))|int - offset %}
{{states('sensor.alarmclock_wd_time')}}
{{fadein_time}}


but need to manipulate that back to a format I can use to trigger actual fadein

    trigger:
      platform: template
      value_template: >
        {{states('sensor.time') == states('sensor.fadein_time')}}

thanks for having another look

just use the same macro on sensor.time

wait, you mean like this:

{%- macro time_in_minutes(t) %}
{%- set h, m = t.split(':') | map('int') %}
{{ h * 60 + m }}
{%- endmacro %}

{% set offset = 20 %}
{% set fadein_time = time_in_minutes(states('sensor.alarmclock_wd_time'))|int - offset %}
{% set time = time_in_minutes(states('sensor.time'))|int %}
{{states('sensor.alarmclock_wd_time')}}
{{fadein_time}}
{{time}}
{{time == fadein_time}} #<-- for the trigger template ?

yep, exactly like that

cool, thanks!
still leaves me with the challenge how to subtract from a timestamp number… got a tip for that too?

say I need to use the same offset on that? how would I proceed using the sensor.next_alarm_timestamp ?

this yields nothing:

</s> <s>{{as_timestamp(states('sensor.next_alarm_timestamp')|int - 20*60)|timestamp_custom('%X') }}</s> <s>

and in the editor the states('sensor.next_alarm_timestamp') which is a number is in fact stated to be a string…(hence my |int filter)

this is processed alright:

{{states('sensor.next_alarm_timestamp')|int - 20*60}}

NM:

{{(states('sensor.next_alarm_timestamp')|int - 20*60)|timestamp_custom('%H:%M') }}

or

{{(states('sensor.next_alarm_timestamp')|int - states('input_number.offset_minutes')|int *60)|timestamp_custom('%H:%M') }}

making the trigger:

trigger:
      platform: template
      value_template: >
        {{states('sensor.time') == (states('sensor.next_alarm_timestamp')|int - 
                                    states('input_number.offset_minutes')|int *60)
                                    |timestamp_custom('%H:%M')}}

form that matter. both issues solved!

sorry… and thanks again

čerpáno ze zdroje

Hi.
so in the final I used the code:

I created a helper for time and date input_datetime.set_datetime, and using a small automation service running every 30 minutes, I assigned it this value with a modified time difference.
Now I can conveniently use the help in starters and automation conditions

Thank you Marius
and everyone else who inspired me

1 Like

Hey @petro I have been following your suggestion and I believe I am there however, my result produces date which I am not interested in, all I need is the time.

I have this sensor.fajr whose output is when I:

{{ states.sensor.fajr }} =

Result type: string
<template TemplateState(<state sensor.fajr=03:19; icon=mdi:clock, friendly_name=Fajr @ 2023-05-04T00:05:17.240375+01:00>)>

All I am trying to do is subtract 1 minute from the Fajr Sensor which is at the moment 03:19 and it would then become 03:18.

So I used what you suggested above and came up with this:

{% set time = states('sensor.fajr')%} {{ strptime(time, '%H:%M') - timedelta( minutes = 1 ) }}

the output however produces date which I am not interested in.

Output: 1900-01-01 03:18:00

Can you please help?

{{ (today_at(states('sensor.fajr')) - timedelta(minutes = 1)).strftime('%H:%M') }}
1 Like

thank you that worked perfectly :slight_smile: