The EPIC Time Conversion and Manipulation Thread!

hmm, not sure to be honest. I am used to the data my Solar system provides me, and have that ‘last year’ value. Guess it wouldn’t be too difficult to publish that at 12-31 midnight, and keep it for eternity by recording that sensor in Recorder.

If you require the specific api call for the command_line sensor, you can always ask the main dev at Iungo to assist. Rodney is very helpful in that regard.

You might also have a look at the utility meter integration that can make yearly sensors for you

still, don’t you have these sensors in your system? Depending on your manufacturer, there are several nice integrations in HA, mine is Solaredge, and I dropped the Api calls to the SE server altogether in favor of the integration.

Ping me, or open a dedicated topic for the mqtt publish of the sensor, and I can help you out with the scripts I use for recording the daystart values. (to keep this thread here on-topic)

as for the timestamp, you could try something like {{as_timestamp('2022-12-31 23:59:59')}} in de tools template editor, and enter that into the command_line?

see what that does:

{{as_timestamp(now())}}
{{as_timestamp('2022-09-27 22:37:30')}}

I can use the HACS Zonneplan integration for my system, but unfortunately the yearly yield value is not one off the options. Now I put the timestamp 1640991599 in de command line so I get the yield of last year. Would be nice to make this dynamic, so that I do not have to changes this every year. But for the future I can give the utility meter a try.

I have a template sensor showing how long my garage door has been opened, using timestamps provided by an automation with two input_datetime actions.

{{ (state_attr('input_datetime.gate_closed','timestamp') - state_attr('input_datetime.gate_open','timestamp')) | timestamp_custom('%H:%M:%S', False) }}

This gives me:

00:00:37

Is there a way I can make the template to not show hours and minutes when the result is below 60 seconds, and to not show hours when the result is below 60 minutes?


    {%- set closed = state_attr('input_datetime.gate_closed','timestamp') %}
    {%- set open = state_attr('input_datetime.gate_open','timestamp') %}
    {% set diff = closed - open %}
    {% set sec = diff %}
    {% set min = (diff / 60)|round(0, default=0) %}
    {% set h = diff |timestamp_custom('%H:%M:%S', False) %}
    {% set time = sec if diff < 60 else min if diff < 3600 else h %}
    {% set unit = (' sec.' if sec < 60 else ' min.') if diff < 3600 else '' %}
    {{ time ~ unit }}

This is great, and a lot to learn from. The output of my template was

00:00:46

and the output of this template is

46.17343306541443 sec.

I thing I can turn that into 46 sec. if I change

{% set time = sec if diff < 60 else min if diff < 3600 else h %}

for this:

{% set time = (sec if diff < 60 else min if diff < 3600 else h) |round(0, default=0) %}

But please tell me if I’m wrong. Now the problem is when the gate stays open for more than 60 seconds (and maybe 60 minutes, but haven’t tried that). For 81 seconds opened, with my template I get:

00:01:21

with yours:

1 min.

Any tips on how to turn it into 1 min. 21 secs.?

Thanks.

Then other solutions may be easier:

https://community.home-assistant.io/t/template-sensor-template-variable-warning-with-ha-2021-4/297237/9

https://community.home-assistant.io/t/the-epic-time-conversion-and-manipulation-thread/85786/238

https://community.home-assistant.io/t/hunanizing-history-stats/470978/2


         {%- set closed = state_attr('input_datetime.gate_closed','timestamp') %}
         {%- set open = state_attr('input_datetime.gate_open','timestamp') %}
         {%- set diff = closed - open %}

         {% macro phrase(name, plural_name, divisor, mod=None) %}
           {%- set value = ((diff // divisor) % (mod if mod else divisor)) |int %}
           {%- set name = plural_name if value > 1 else name %}
           {{- '{} {}'.format(value, name) if value |int > 0 else '' }}
         {%- endmacro %}

        {%- set values = [
          phrase('hour', 'hours', 60*60),
          phrase('minute', 'minutes', 60),
          phrase('second', 'seconds', 1, 60)
        ] | select('!=','') | list %}

        {{ values[:-1] | join(', ') ~ ', ' ~ values[-1] if values |length > 1 else values |first |default }}


Or, if you want the value to be displayed as in your initial question:


          {%- set closed = state_attr('input_datetime.gate_closed','timestamp') %}
          {%- set open = state_attr('input_datetime.gate_open','timestamp') %}
          {% set diff = closed - open %}
          {%- set h = diff |timestamp_custom('%H:%M:%S', False) %}

          {%- macro phrase(name, plural_name, divisor, mod=None) %}
            {%- set value = ((diff // divisor) % (mod if mod else divisor)) |int %}
            {%- set name = plural_name if value > 1 else name %}
            {{- '{} {}'.format(value, name) if value |int >=1 else '' }}
          {%- endmacro %}
          
          {%- set values = [ 
                     phrase('minute', 'minutes', 60),
                     phrase('second', 'seconds', 1, 60), 
              ] |select('!=', '') |list %}


          {%- if diff < 3600 %} 
            {{ values[:-1] | join(', ') ~ ', ' ~ values[-1] if values |length > 1 else values |first |default }}
          {%- else %} {{ h }}
          {% endif %}

@pedolsky That is just perfect. Thank you so much.

Hi, I’m sure this is simple but I’m struggling to convert a GMT datetime to BST. The timestamp is returning GMT but I’m currently one hour ahead, in BST. I only want to return the time from the datetime.

{{ as_timestamp(state_attr("sun.sun", "next_rising")) | timestamp_custom('%H:%M', local=true) }}

This currently returns 06:49, when time in BST is 07:49.

I’ve searched and scanned this thread but can’t find anything that works. Please help!

Your input timestamp is not local, so why are you setting it to true?

because I’m not familiar with how it works (I’m not a developer) and it’s only one of the things I tried. I also did it without that flag set, with the same result. What would be good would be if someone who knows how to do this could refer me to the correct way.

set it to false

Thank you. Tried that but it didn’t work. Also, I’m not sure how to test it now we’re out of daylight saving but I’d like to get it working before we go back in, in March next year! Any ideas?

Sounds like the time on your system is not set properly. Are you sure you set up time correctly during the onboarding process?

You can manipulate each data in Developer Tools → States.

Copy

{{ now().tzinfo }}

into Developer Tools → Template. Does it show the right timezone?

It was years ago but I doubt I would’ve set it up incorrectly, but I can’t be 100%, there’s always the chance. Is there any way to check or would i need to do a full rebuild?

post what @pedolsky said

Yes, it shows UTC, which is the same as GMT. I assume that is what it would show even if it was wrong (show GMT when on BST) and now we’re on GMT, I don’t know how I can test that theory

If it shows UTC, that means you didn’t set your time up properly. It should show your timezone, not UTC.

Assuming you’re in London, your TZinfo should show Europe/London

A-ha! Mystery solved, thank you. My setup was done before all the visual configuration was added, so it was disabled in the front end. I added time_zone: "Europe/London" to configuration.yaml and it’s now showing correctly. Thanks for the pointers.

Hi,

I’m trying to manage a countdown in ESPhome, I starter here

{{state_attr("timer.sprinklercountdown","finishes_at")}}
{{now()}}

{{as_timestamp(now())}}
{{as_timestamp(state_attr("timer.sprinklercountdown","finishes_at"))-as_timestamp(now())}}

{{state_attr("timer.sprinklercountdown","duration")}}

{{
  (
    as_timestamp(state_attr("timer.sprinklercountdown","finishes_at"))
    -
    as_timestamp(now())
  )
  |
  timestamp_custom('%H:%M:%S')
}}

but count “returns” +1h (it should be 01:29:09)

countdown
02:29:09
finishes_at
2022-11-09T10:00:10+00:00
now
2022-11-09 09:31:00.019906+01:00

now timestamp
1667982660.019962
finishes_at timestamp
5349.979902982712

duration
1:30:00

countdown
02:29:09

thoughts?