Automation of battery discharge

I’m new to HA.
I’m trying to create a script that will discharge my battery above 30% to the Grid between 16:00-19:00. I really wanted a script along the lines:

If battery>30% and (timestamp>=16:00 and timestamp<19:00) then start discharge
If (battery <30% or timestamp>=19:00) and discharging then stop discharge
However, with the HA Automation I don’t seem to have this flexibility.
Instead I’ve made it really basic with a very simple start script - the system crafted YAML:

id: ‘1744822807422’
alias: Discharge Libbi
description: Start discharge of Libbi @16:00
triggers:

  • trigger: time
    at: ‘16:00:00’
    conditions:
  • type: is_battery_level
    condition: device
    device_id: 2da904fc876fe9986c9e71d1c454427f
    entity_id: e6b531030f9934a1c871568521505159
    domain: sensor
    above: 30
    actions:
  • device_id: 2da904fc876fe9986c9e71d1c454427f
    domain: select
    entity_id: cca0015f5c0143c73f80f4b6369847cb
    type: select_option
    option: Export
    mode: single

This seems to work (though I haven’t tested when the battery % is below 30).

I have 3 scripts to stop the discharge - one to run at 17:00, 18:00 and 19:00 (though the 19:00 just stops the discharge irrespective of the storage capacity). Here is the 18:00 system crafted YAML:

id: ‘1744823758157’
alias: Stop Libbi Discharge @18:00
description: If @18:00 the Libbi is below 30% then stop the discharge
triggers:

  • trigger: time
    at: ‘18:00:00’
    conditions:
  • type: is_battery_level
    condition: device
    device_id: 2da904fc876fe9986c9e71d1c454427f
    entity_id: e6b531030f9934a1c871568521505159
    domain: sensor
    below: 30
    actions:
  • device_id: 2da904fc876fe9986c9e71d1c454427f
    domain: select
    entity_id: cca0015f5c0143c73f80f4b6369847cb
    type: select_option
    option: Normal
    mode: single

When I manually force this script to run (ie not at 18:00) it always stops the discharge irrespective of the % utilisation.

Any pointers?

This is running on

  • Core 2025.4.2
  • Supervisor 2025.04.0
  • Operating System 15.2
  • Frontend 20250411.0

This is all running on a RPI3.

Please post your code as “preformatted text” (</> in the cogwheel toolbar). It’s quite difficult to follow otherwise. :grin:

1 Like