Turn off switch when battery level drops? (ChatGPT failed)

Hello,

With the following automation, I can switch on my electric hot water heater when the forecast for solar generation is good. As soon as the forecast becomes bad, the heating is switched off. That works fine too.

Now I would like to add that the heating is also switched off if my battery level has dropped by 5% since the heating was switched on. The battery level sensor is sensor.envoy_122206102883_total_battery_percentage

Here is the code so far:

alias: Warmwasserheizung steuern
description: ""
trigger:
 - platform: numeric_state
   entity_id: sensor.solar_prognose_verbrauchersteuerung
   for:
     hours: 0
     minutes: 1
     seconds: 0
   above: 27000
   id: Warmwasserheizung Auslöser zum Einschalten
 - platform: numeric_state
   entity_id: sensor.solar_prognose_verbrauchersteuerung
   for:
     hours: 0
     minutes: 1
     seconds: 0
   below: 25000
   id: Warmwasserheizung Auslöser Solarfaktor zum Ausschalten
 - platform: state
   entity_id:
     - sensor.solar_prognose_verbrauchersteuerung
   to: unavailable
   for:
     hours: 0
     minutes: 5
     seconds: 0
   id: Warmwasserheizung Auslöser Nicht verfügbar  zum Ausschalten
 - platform: state
   entity_id:
     - sensor.solar_prognose_verbrauchersteuerung
   to: unknown
   for:
     hours: 0
     minutes: 5
     seconds: 0
   id: Warmwasserheizung Auslöser Unbekannt  zum Ausschalten
condition: []
action:
 - if:
     - condition: or
       conditions:
         - condition: trigger
           id: Warmwasserheizung Auslöser Solarfaktor zum Ausschalten
         - condition: trigger
           id: Warmwasserheizung Auslöser Nicht verfügbar  zum Ausschalten
         - condition: trigger
           id: Warmwasserheizung Auslöser Unbekannt  zum Ausschalten
   then:
     - type: turn_off
       device_id: 990c1fa02f5750c74695320f637ed36c
       entity_id: switch.warmwasser_heizung_ss00_blau_tz3000_gjnozsaz_ts011f_switch_2
       domain: switch
 - if:
     - condition: trigger
       id: Warmwasserheizung Auslöser zum Einschalten
   then:
     - type: turn_on
       device_id: 990c1fa02f5750c74695320f637ed36c
       entity_id: switch.warmwasser_heizung_ss00_blau_tz3000_gjnozsaz_ts011f_switch_2
       domain: switch
mode: single

Can someone help me (ChatGPT couldn’t :wink: )?

Thanks

Greeting Werner

Replace this:

 - if:
     - condition: trigger
       id: Warmwasserheizung Auslöser zum Einschalten
   then:
     - type: turn_on
       device_id: 990c1fa02f5750c74695320f637ed36c
       entity_id: switch.warmwasser_heizung_ss00_blau_tz3000_gjnozsaz_ts011f_switch_2
       domain: switch
mode: single

with this:

 - if:
     - condition: trigger
       id: Warmwasserheizung Auslöser zum Einschalten
   then:
     - service: switch.turn_on
       target:
         entity_id: switch.warmwasser_heizung_ss00_blau_tz3000_gjnozsaz_ts011f_switch_2
     - variables:
         battery_level: "{{ states('sensor.envoy_122206102883_total_battery_percentage') | float(0) }}"
         threshold: "{{ battery_level - (battery_level * 0.05) }}"
     - wait_template: "{{ states('sensor.envoy_122206102883_total_battery_percentage') | float(0) < threshold }}"
       timeout: '00:25:00'
       continue_on_timeout: false
     - service: switch.turn_off
       target:
         entity_id: switch.warmwasser_heizung_ss00_blau_tz3000_gjnozsaz_ts011f_switch_2
mode: restart

Adjust the value of timeout to whatever makes sense for your application (it should be longer than the time it normally takes the battery level to decrease by 5%).

1 Like

Thank you very much for the detailed information.

Before I adjust it, a comment on the project.

In principle, the hot water heating should only be operated if the self-produced electricity would otherwise be fed into the grid.
I generally achieve this via the yield forecast. While the hot water heater is running, there should generally not be any consumption from the battery, as it is also being charged (unless it is already full).
That’s why it was important to me that the battery level shouldn’t drop during the heating period.
I just want to build in some safety because sometimes the forecast is pretty wrong.

If I understand the timeout correctly, after the set time has elapsed, it turns off the heating anyway. So I could set this to 10 hours, for example, because the sun doesn’t usually shine for longer than that.

Did I understand that correctly?

And thanks again.

It doesn’t.

If you want it to behave like that, change the value of continue_on_timeout to true.

timeout is a time limit for the wait_template. Instead of allowing wait_template to wait forever, it sets a limit. If it reaches the limit, the next decision is should it continue executing any other actions or should it stop immediately. That’s what continue_on_timeout determines.

In addition, changing mode to restart allows the automation to be triggered while it’s busy waiting for the wait_template. In other words, it ceases waiting immediately and proceeds to act on the trigger event.

1 Like

I’m having some problems with the translation.

If I only want the shutdown if the battery level has dropped by at least 5% for at least 2 minutes since the heater was switched on. Then what would I have to do?
The aim is to operate the heating system using only solar energy. Not or only very slightly from the battery.

What I suggested was in response to your original requirement:

It appears that you have changed the original requirement:

What other modifications to the original requirement do you have in mind?

1 Like

Please excuse me.
I only added the 2 minutes in case the transmission was disturbed by the battery level.
Otherwise does not change.
Sorry again.

To fulfill the additional requirement, we will need a slightly different approach that requires the use of a Trigger-based Template Sensor and wait_for_trigger.

Create the following Trigger-based Template Sensor:

template:
  - trigger:
      - platform: state
        entity_id: switch.warmwasser_heizung_ss00_blau_tz3000_gjnozsaz_ts011f_switch_2
        from: 'off'
        to: 'on'
    sensor:
      - name: Solar Battery Threshold
        state: >
          {% set battery_level = states('sensor.envoy_122206102883_total_battery_percentage') | float(0) %}
          {{ battery_level - (battery_level * 0.05) }}

Initially, it will report unknown but that will change when it’s triggered for the first time. It’s triggered whenever the switch changes from off to on. When the switch is turned on, this Template Sensor will report a value that’s 5% less than the current battery level.

Create the following automation. Instead of a wait_template it uses a wait_for_trigger.

The wait_for_trigger will trigger only when the value of:

sensor.envoy_122206102883_total_battery_percentage

decreases below the value of:

sensor.solar_battery_threshold

and remains below it for at least 2 minutes.

alias: Warmwasserheizung steuern
description: ""
trigger:
  - id: Warmwasserheizung Auslöser zum Einschalten
    platform: numeric_state
    entity_id: sensor.solar_prognose_verbrauchersteuerung
    above: 27000
    for:
      minutes: 1
  - id: Warmwasserheizung Auslöser Solarfaktor zum Ausschalten
    platform: numeric_state
    entity_id: sensor.solar_prognose_verbrauchersteuerung
    below: 25000
    for:
      minutes: 1
  - id: Warmwasserheizung Auslöser Nicht verfügbar  zum Ausschalten
    platform: state
    entity_id: sensor.solar_prognose_verbrauchersteuerung
    to: unavailable
    for:
      minutes: 5
  - id: Warmwasserheizung Auslöser Unbekannt  zum Ausschalten
    platform: state
    entity_id: sensor.solar_prognose_verbrauchersteuerung
    to: unknown
    for:
      minutes: 5
condition: []
action:
  - variables:
      entity: switch.warmwasser_heizung_ss00_blau_tz3000_gjnozsaz_ts011f_switch_2
  - if: "{{ 'Ausschalten' in trigger.id }}"
    then:
      - service: switch.turn_off
        target:
          entity_id: "{{ entity }}"
    else:
      - service: switch.turn_on
        target:
          entity_id: "{{ entity }}"
      - wait_for_trigger:
          - platform: numeric_state
            entity_id: sensor.envoy_122206102883_total_battery_percentage
            below: sensor.solar_battery_threshold
            for:
              minutes: 2
        timeout: '01:00:00'
        continue_on_timeout: false
      - service: switch.turn_off
        target:
          entity_id: "{{ entity }}"
mode: restart
1 Like

Thanks so much.
I’ll install and test it tomorrow because I have to go first.
I add the trigger-based template sensor to the configuration.yaml ? Correct?

Thanks :v:

Yes, unless your configuration.yaml file already contains a line like this:

template: !include templates.yaml

In that case, the Trigger-based Template Sensor’s configuration goes in the templates.yaml file.

1 Like

Hello,

thank you again.
Due to the lack of sun, I tested it by changing the state manually. Worked great.
Thank you again. :pinching_hand: :+1:

1 Like