#Time Calculations

1 messages · Page 1 of 1 (latest)

naive dawn
#

I have 3 datetime helpers, 2 are defined and changed rarely but one changes daily.
I wish to compare the daily changing helper against the other 2 and then trigger a number of automations to run based on the result.

So far…

  1. The result from initial calculation I’m planning on storing in a 4th datetime helper so it can be used as a trigger.
  2. I have converted the datetime helpers to timestamps so I have 3 numbers, I’m trying to store these as variables but no success, surely I do not need to store the results in 3 numeric helpers ?
{{state_attr('input_datetime.sunset_next','timestamp')}}

Above takes the time and changes to a timestamp, using this to try and create a variable or a number helper generates an error on the comma so am unable to use.

limber lantern
#

There isn't anything wrong with the template you posted on its own, so the error is likely coming from the context in which you are using it.

surely I do not need to store the results in 3 numeric helpers ?
No, you probably don't need 3 more helpers. But it isn't clear what you are trying to say with the rest of the post or what the question is...

Post the complete script or automation configuration you are working on.

naive dawn
#

Looking at it with a rested head helps, the (basic) working logic (below has since been simplified)

{## Time Calculations ##}
{% set next = state_attr('input_datetime.sunset_next','timestamp') %}
{% set earliest = state_attr('input_datetime.sunset_earliest','timestamp') %}
{% set latest = state_attr('input_datetime.sunset_latest','timestamp') %}

{% if earliest > next %}
  {% set result = earliest %}
{% elif next > latest %}
  {% set result = latest %}
{% else %}
  {% set result = next %}
{% endif %}

{{ (result + 1800) | timestamp_custom('%H:%M') }} 

Now ready to make it more complicated and utilise in automations etc.

ionic flame
#

you could simplify it a little bit with something like:
{% set result = [[next, latest] | min, earliest] | max %}

#

Also, you will need to update earliest and latest every day, which is a bit sad - you might want to change them to time only input_datetimes and then use today_at to get it changed to something you can use for a comparison, or pull the time out of the sunset_next and do the comparison on time rather than timestamp (though then you won't be able to use max/min)

naive dawn
#

Thanks for the shortcut. I am using input_datetime helpers so for each value it only looks at time and has no idea of date.

fervent briar
#

Also no need to make them timestamps. You can just work with them as a datetime object.

naive dawn
#

Thanks, I started with trying to do datetime comparisons without success thus headed down timestamp following some reading, based on the feedback, I've simplified now using input_datetime directly and have the below which works well.

{% set dusk = as_timestamp(states('sensor.sun_next_dusk'))|timestamp_custom('%H:%M:%S') %}
{% set earliest = states('input_datetime.sunset_earliest') %}
{% set latest = states('input_datetime.sunset_latest') %}

{% set result = [[dusk,latest]|min,earliest]|max %}

{{result}}```