At the moment I am using a set of input_datetime to charge my house battery bank between set times.
I want to be able to turn off charge if it reaches 98% as part of the action.
Will the following work as expected?
# Automation to start forcetime 1 based on time.
- alias: Start Force Time 1
trigger:
- platform: template
value_template: "{{ states('sensor.time') == (states.input_datetime.start_forcetime_1_time.attributes.timestamp | int | timestamp_custom('%H:%M', False)) }}"
condition:
condition: state
entity_id: input_boolean.timed_forcetime1
state: 'on'
action:
- service: homeassistant.turn_on
entity_id: input_boolean.solax_forcetime
- service_template: "{{ 'homeassistant.turn_off' if states.sensor.solax_battery_capacity.state == '98' }}"
entity_id: input_boolean.solax_forcetime
If the battery bank hasnât fully charged I still have a second action to turn it off.
# Automation to stop forcetime 1 based on time.
- alias: Stop Force Time 1
trigger:
- platform: template
value_template: "{{ states('sensor.time') == (states.input_datetime.stop_forcetime_1_time.attributes.timestamp | int | timestamp_custom('%H:%M', False)) }}"
condition:
condition: state
entity_id: input_boolean.timed_forcetime1
state: 'on'
action:
- service: homeassistant.turn_off
entity_id: input_boolean.solax_forcetime
- service: homeassistant.turn_off
entity_id: input_boolean.timed_forcetime1
Configuration validation isnât showing any errors and normally I would just leave to run it.
But Solar has been rubbish today so I need to charge it while at the cheapest rate during the night to get me through to tomorrow.
The automation runs without the service_template part, but I want to know if that addition will work as intended.
Did you know you can use that input datetime directly in a time trigger as of v0.115?
- alias: Start Force Time 1
trigger:
- platform: time
at: input_datetime.start_forcetime_1_time
You need an else case so the service is never empty here:
service_template: "{{ 'homeassistant.turn_off' if states.sensor.solax_battery_capacity.state == '98' }}"
Something like this would probably do:
service_template: "{{ 'homeassistant.turn_off' if states('sensor.solax_battery_capacity')|float >= '98' else 'homeassistant.turn_on' }}"
I also changed the sensor template to the preferred format that does not generate errors if the sensor is not ready and changed it to a number so you can do numerical comparisons. It was a string the way you had it, which was ok for simple digit comparisons but sill not a great habit to get into. It definitely would not work for a âgrater than or equal toâ test. I did that in case the value skips over 98.
Thanks for the tips and pointers.
So now it all becomes
# Automation to start forcetime 1 based on time.
- alias: Start Force Time 1
trigger:
- platform: time
at: input_datetime.start_forcetime_1_time
condition:
condition: state
entity_id: input_boolean.timed_forcetime1
state: 'on'
action:
- service: homeassistant.turn_on
entity_id: input_boolean.solax_forcetime
- service_template: "{{ 'homeassistant.turn_off' if states('sensor.solax_battery_capacity')|float >= '98' else 'homeassistant.turn_on' }}"
entity_id: input_boolean.solax_forcetime
I wrote the original automation a while back, so didnât really spot the change in platform: time instead of platform: template and all the mess of a value_template.
Thanks @123 the info you and @tom_l are sharing itâs very useful in helping me to understand these new features better!
I was wondering if I could use a condition to effectively end the automation like my second automation did. Makes for a lot cleaner and compact automationâs without having a start automation and a stop automation!
I take it that the [:5] refers to the time set in my input_datetime say for example 06:00 (_time)
I also didnât realise you could stack entity_id: like entity_id: input_boolean.solax_forcetime, input_boolean.timed_forcetime1
I actually have two of these automations with two set of times (There isnât always a solid block of cheap electric where I can fully charge the bank in one go, so I have to split it sometimes)
I know in automationâs you can tell what triggered the automation ie input_datetime.start_forcetime_1_time but can you then get it to react only to input_datetime.stop_forcetime_1_time and somehow not to input_datetime.start_forcetime_2_time based on using say [:4] in there?
Like check contents of [:4] (1 or 2) of what triggered the start and react to a stop_forcetime that matches the same start number?
Unfortunately when I left it to run @tom_l example from the second post it failed during the night.
service_template: "{{ 'homeassistant.turn_off' if states('sensor.solax_battery_capacity')|float >= '98' else 'homeassistant.turn_on' }}"
Start Force Time 1: Error executing script. Unexpected error for call_service at pos 2: â>=â not supported between instances of âfloatâ and âstrâ
While executing automation automation.start_force_time_1
I should have quickly tried it before going to bed.
Just quickly tested @123 post 4 and it started ok! But electric is expensive at the moment so I didnât let it run itâs full course.
Iâll have a proper look at your second method later, as when I tried it earlier it gave Invalid config for [automation]: Expected a dictionary @ data['action'][0]['choose'][0]['conditions'][0]. Got None. (See ?, line ?).
So probably just need the formatting correcting somewhere?
Iâll take a look when electric is cheaper.
I have noticed bit of a strange problem with your examples @123
Say for example itâs 08:00 and I set the input_datetimeâs to 09:00 and 10:00 and turn the input_boolean on.
The automation works as expected, it turns the battery on at 09:00 and off at 10:00
But say itâs 22:00 I am about to go to bed, I set the two input_datetimeâs to 03:00 and 04:00 and turn the input_boolean on.
But I wake in the morning, the battery has not charge and the input_boolean.time_forcetime1 is still showing as on. So it never got triggered.
Is the automation getting confused by (trigger.now.time()|string) as the time I turn on the input_boolean is the day before I want the times to trigger?
So I set the times on Thurs 15th Oct and turn the input_boolean on.
But I am expecting the times to trigger early hours of 16th Oct, but it doesnât as the date stamp for the times being set is the 15th?
trigger.now is the time when the automation is triggered. It is a datetime object but the template uses the objectâs time() method to return just the time.