Help With Helpers!

Post Reply
User avatar
HughInDevon
Posts: 22
Joined: Wed Jul 31, 2024 4:50 pm

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
KH10 with 4 off ECS4800 batteries.
20 Jinko 435W panels in 2 strings. 10 on house, south facing, 10 on barn, east facing.
Solar installation underway. Active on 27/11/2024 (but still awaiting export MPAN)
Zappi V2 charger and 2 EVs.
Home Assistant modbus integration (Now working - ish)
marcus
Posts: 12
Joined: Thu Aug 10, 2023 8:19 am

this might help

Code: Select all

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

House
H1 Series H1-3.7-E Inverter | RS485 Modbus->HA
8x Jinko 300W Mono (3x East facing; 5x South facing - 2400W)
Workshop
H1 Series H1-5.0-E Inverter | RS485 Modbus->HA
7x HV2600 2.6 kWh (5x V2; 2x V1 (Version C)) BMS V2 using this fix
12x Canadian 430W (2 strings South facing - 5160W)
User avatar
HughInDevon
Posts: 22
Joined: Wed Jul 31, 2024 4:50 pm

Yea!

Many thanks.

Hugh :D
KH10 with 4 off ECS4800 batteries.
20 Jinko 435W panels in 2 strings. 10 on house, south facing, 10 on barn, east facing.
Solar installation underway. Active on 27/11/2024 (but still awaiting export MPAN)
Zappi V2 charger and 2 EVs.
Home Assistant modbus integration (Now working - ish)
User avatar
HughInDevon
Posts: 22
Joined: Wed Jul 31, 2024 4:50 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
KH10 with 4 off ECS4800 batteries.
20 Jinko 435W panels in 2 strings. 10 on house, south facing, 10 on barn, east facing.
Solar installation underway. Active on 27/11/2024 (but still awaiting export MPAN)
Zappi V2 charger and 2 EVs.
Home Assistant modbus integration (Now working - ish)
Dave Foster
Posts: 1470
Joined: Thu Oct 13, 2022 7:21 pm

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') }}
User avatar
HughInDevon
Posts: 22
Joined: Wed Jul 31, 2024 4:50 pm

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.
KH10 with 4 off ECS4800 batteries.
20 Jinko 435W panels in 2 strings. 10 on house, south facing, 10 on barn, east facing.
Solar installation underway. Active on 27/11/2024 (but still awaiting export MPAN)
Zappi V2 charger and 2 EVs.
Home Assistant modbus integration (Now working - ish)
Dave Foster
Posts: 1470
Joined: Thu Oct 13, 2022 7:21 pm

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 %}
User avatar
HughInDevon
Posts: 22
Joined: Wed Jul 31, 2024 4:50 pm

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
KH10 with 4 off ECS4800 batteries.
20 Jinko 435W panels in 2 strings. 10 on house, south facing, 10 on barn, east facing.
Solar installation underway. Active on 27/11/2024 (but still awaiting export MPAN)
Zappi V2 charger and 2 EVs.
Home Assistant modbus integration (Now working - ish)
Post Reply