Hi Guys,
Would someone be able to help me with my code here, Im trying add the duration of hours stored in a counter to a time variable stored in an Input Date/Time helper.
my code seems to be calculating the time however it is adding an addition hour for some reason.
input_datetime.start_fasting is currently set to 16:00 and counter.fasting_duration is set to 4, I am expect it to output 20:00 however I am getting 21:00.
Any ideas what I have done wrong here?
{% set end = (state_attr('input_datetime.start_fasting', 'timestamp') + 3600 * states ('counter.fasting_duration') | float) | timestamp_custom('%H:%M:%S') %}
{{ end }}
It looks to me that your trying to do timestamp math on things that aren’t really timestamps. Timestamps require a date component, and I assume your input_datetime.start_fasting is really just a time.
To see this clearly, try this:
{{ state_attr('input_datetime.test_reminder','timestamp') | timestamp_custom('%H:%M:%S') }}
You might expect it to be 16:00:00, but I bet that’s not what you’ll get (I got 11:00; I suspect the difference in your and my errors are timezone related so you might see 17:00:00).
So instead, try making it a real timestamp first by using today_at()
:
{{ (today_at(states('input_datetime.test_reminder')) | as_timestamp + (3600 * states('counter.test_counter') | int )) | timestamp_custom('%H:%M:%S') }}
(I used my own helper entities and didn’t set it to a variable, but hopefully it’s clear)
I would just skip all the timestamps and use datetime methods:
{% set duration_h = states('counter.fasting_duration') | int(0) %}
{{ ( today_at(states('input_datetime.start_fasting'))
+ timedelta(hours=duration_h)).strftime('%H:%M:%S') }}
Hey thanks for your help, that appears to have fixed it, I have really bizarre issue though, Ive developed the code in development tools which is now showing the correct status of “Eat” as it is within the window, however when I view the entity it gives me a different result, any ideas what im doing wrong here:
- platform: template
sensors:
intermittent_fasting_status:
friendly_name: "Intermittent Fasting Status"
value_template: >
{% set start = states('input_datetime.start_fasting') %}
{% set duration_h = states('counter.fasting_duration') | int(0) %}
{% set end = ( today_at(states('input_datetime.start_fasting')) + timedelta(hours=duration_h)).strftime('%H:%M:%S') %}
{% set time = states('sensor.time') %}
{% if (time > start) and (time < end) %}
Eat
{% else %}
Fasting
{% endif %}
attribute_templates:
Start Time: >
{% set start = states('input_datetime.start_fasting') %}
{{ start }}
End Time: >
{% set duration_h = states('counter.fasting_duration') | int(0) %}
{% set end = ( today_at(states('input_datetime.start_fasting')) + timedelta(hours=duration_h)).strftime('%H:%M:%S') %}
{{ end }}
Fast Duration: >
{% set hours = states('counter.fasting_duration') %}
{{ hours }} Hours
Current Time: >
{% set time = states('sensor.time') %}
{{ time }}
Very odd the history seems to report the correct value, but the state doesn’t.