Page 1 of 1

Help With Helpers!

Posted: Fri Jan 10, 2025 12:28 pm
by HughInDevon
A bit of background. I am trying to make an automation to force discharge any remaining energy in my battery before the start of the forced charge period. For me, on Octopus Intelligent Go, that means exporting the residual energy before 23:30 when the low rate tariff starts and I recharge the battery ready for the next day.

Basically the automation will check at a specified start time (say 19:00) how much energy (in kWh) is left in the battery and hence how long it will need to export this at my G99 export limit (4 kW). The time interval between the time of the check and the start of the low-tariff period is the window for exporting. Then if the export time needed is hours less than the window, the automation sets a new check time nearer to the low-tariff time and exits. This will repeat until the export time is within say 30 minutes of the window. Then the forced discharge will be triggered.

To do this automation I have created a few Helpers to let me input some relevant data. Specifically, I have helpers to manually input the time when the low tariff period starts, FD_SetWindowEndTime and the time when I want to start checking, FD_WindowStartTime. I created these using Settings, Devices and Services, Helpers, Create Helper then selecting Date and/or Time, inputting in the name and selecting time as the chosen input. Hit create and voila, I have a helper. I click on the name and enter a time in hh:mm and it accepts it.

But--- when I go to Developer Tools, Template and enter {{FD_SetWindowEndTime}} in the Template Editor it says 'FD_SetWindowEndTime' is undefined.

Obviously I am doing something wrong but I have tried everything I can think of and I can't get it to show the string with the numbers I have input. I would really like some help here.
Thanks in advance, Hugh

Re: Help With Helpers!

Posted: Fri Jan 10, 2025 12:42 pm
by marcus
this might help

Code: Select all

{{ states('input_datetime.FD_SetWindowEndTime') }}

Re: Help With Helpers!

Posted: Fri Jan 10, 2025 1:36 pm
by HughInDevon
Yea!

Many thanks.

Hugh :D

Re: Help With Helpers!

Posted: Fri Jan 10, 2025 2:18 pm
by HughInDevon
OK, now I want to extract just the hours part from the string result. I have tried:

{{ states('input_datetime.FD_SetWindowEndTime').hour }} = 'str object' has no attribute 'hour'
{{ states('input_datetime.FD_SetWindowEndTime'.hour) }} = 'str object' has no attribute 'hour'
{{ states('input_datetime.FD_SetWindowEndTime.hour') }} = unknown

Not sure what else I can try.

Thanks, Hugh

Re: Help With Helpers!

Posted: Fri Jan 10, 2025 5:22 pm
by Dave Foster
HughInDevon wrote: Fri Jan 10, 2025 2:18 pm OK, now I want to extract just the hours part from the string result. I have tried:

{{ states('input_datetime.FD_SetWindowEndTime').hour }} = 'str object' has no attribute 'hour'
{{ states('input_datetime.FD_SetWindowEndTime'.hour) }} = 'str object' has no attribute 'hour'
{{ states('input_datetime.FD_SetWindowEndTime.hour') }} = unknown

Not sure what else I can try.

Thanks, Hugh
Unlike the now() time function the helpers don't have an object state of hour such as now().hour but what they do have is an attribute of hour i.e.

{{ state_attr('input_datetime.FD_SetWindowEndTime','hour') }}

Re: Help With Helpers!

Posted: Fri Jan 10, 2025 5:57 pm
by HughInDevon
Hi Dave,
Many thanks for that. I had tried state.attr - but I misspelt it as states.attr. I also used it with .hour, rather than , 'hour'. Tricky stuff this mixture of Python, yaml and jinja.

Re: Help With Helpers!

Posted: Fri Jan 10, 2025 6:33 pm
by Dave Foster
I think I replied just as you typed the other post :)

It’s a tricky learning curve, i’ve sat for hours trying to do something before getting it right - then next time all I have to do is remember how I did it ;)

I tend to use templates in automations when testing times, as the test proceeds if true and you can go on to the next test

For example this template will return ‘true’ if the time now is equal to your time (to the minute) being tested.

Code: Select all

{% if now().hour==state_attr('input_datetime.yourname','hour') %}
  {% if now().minute==state_attr('input_datetime.yourname','minute') %}
    {% set comment = 'start time is equal to now do something' %}
    true
  {% endif %}
{% endif %}

Slightly more complex where you want to work out the precise time and get the difference in seconds returning true if precisely the same time.

Code: Select all

{% set hour = now().hour %}
{% set min  = now().minute %}
{% set sec  = now().second %}
{% set nowtimevalue = sec + (min*60) + (hour*3600) %}
{% set hour = state_attr('input_datetime.yourname','hour') %}
{% set min = state_attr('input_datetime.yourname','minute') %}
{% set sec = state_attr('input_datetime.yourname','second') %}
{% set thistimevalue = sec + (min*60) + (hour*3600) %}
{% set difference=nowtimevalue-thistimevalue %}
{% if difference==0 %}
	true
{% endif %}

Re: Help With Helpers!

Posted: Sat Jan 11, 2025 11:39 am
by HughInDevon
Hi Dave,
Thanks very much for both your replies. They did indeed cross in the post as it were! I am heartened to learn that I am not alone in struggling with coding in HA.

The examples you sent are spot on. I was awake for an hour or so last night thinking about how to use the actual time (now()) to trigger an automation. I was worried that it would miss given that now() is only updated every minute. As is usual with these things, once someone has explained how to do it, it seems obvious!

So, thanks again and probably brace yourself for the next question!

Hugh