Setting overnight battery usage start time

Following on from the Setting overnight battery charge level article, this post explains how I use Home Assistant to configure the time the battery will start to be discharged. I found an issue with this code. If the forecast was greater 19 kWh then the default would kick in. This would set the discharge time to 6am. Updated code can be found here.

Over the winter I wanted the battery to be as charged as possible when the day gets started around 6am. In the summer I should be able to use the battery earlier as it should soon get topped up. This automation checks the current battery charge level at 23:30 and then depending on the Solcast forecast will set the start time to either midnight (19 kWh and above), 2am (18 or 17 kWh), 4am (16 kWh) or 6am (less than 15 kWh). If the battery charge level is below 50% then the time will be set to 6am.

I am not convinced this is worth doing as the Agile Octopus tariff is cheap during these hours, but I the geek in me says, why pay when you don’t have to?

alias: Solar - Set Overnight Battery Usage v2
description: Solar - Set Overnight Battery Usage v2
trigger:
  - platform: time
    at: "23:30:00"
condition: []
action:
  - choose:
      - conditions:
          - condition: numeric_state
            entity_id: sensor.lux_battery
            above: 50
        sequence:
          - service: number.set_value
            data:
              value: >
                {% set x = states('sensor.solcast_forecast_tomorrow') | int(0)
                %} {% set levels = { 19: 0, 18: 2, 17: 2, 16: 4 } %} {{ 6 if 0
                <= x <= 15 else levels.get(x, 0) }}
            target:
              entity_id: number.lux_force_charge_end1
          - service: notify.notify
            data:
              message: >-
                The battery will be used after {{
                states("number.lux_force_charge_end1") }} o'clock
              title: Set Overnight Battery Usage (Forecast)
    default:
      - service: number.set_value
        data:
          value: 6
        target:
          entity_id: number.lux_force_charge_end1
      - service: notify.notify
        data:
          message: >-
            The battery will be used after {{
            states("number.lux_force_charge_end1") }} o'clock      title: Set
            Overnight Battery Usage (Default)
mode: single

This isn’t very scientific, but this is what I have come up with. If the battery charge is greater than 50% then it takes tomorrow’s Solcast forecast value and makes it an integer and assigns it to X.

Forecast

19 kWh = midnight

18 kWH = 2am

17 kWh = 2am

16 kWh = 4am

and other value will set the time to 6am

The title of the message sent will either have (Default) or (Forecast) on the end. This helps with debugging which condition triggered.

 
Michael Curtis

My introduction to computers started at my middle school in 1981 when our maths teacher brought in a ZX80. That led the computer club being founded and using a Research Machine 380Z

My first computer was a 48K ZX Spectrum which I loved to programme. Once I left school I worked as a photocopier engineer, then a fax engineer and finally moving on the Apple computers.

For the next 30 years I worked as a system administrator. I now work in the cyber security industry as a Sophos Professional Services consultant

https://www.bazmac.me
Previous
Previous

Using Home Assistant to count self sufficient days

Next
Next

March’s Solar Generation Numbers