Create a binary sensor charging/not charging by various conditions

I wish to create a binary sensor charging/not_charging for the offgrid inverter.

The charging stops when battery reaches 100%
(sensor.battery_state_of_charge =100 or >99)
The charging starts again when the battery drops below 95% (sensor.battery_state_of_charge <95).
Also charging mode change to “charging” when inverter mode changes from battery to anything else instead
sensor.device_mode != ‘Solar/Battery’

I have no idea how to start, by helper or edit directly configuration.yaml
Also I don’t understand how to put those IF’s on the State template field.

Additionally another binary sensor would be helpful:
‘charging_cycle_done’
“true” if sensor.battery_state_of_charge=100
reset to “false” by day pass (hour 00:01) or after some time (48h for example)
I am open to other ideas.

What is the point (use) of the sensor?
I try to let the battery charge from solar once /day to allow the balancing of the lithium cells.
After that I play with DHW heating elements (diverter 3x1500W) discharging and recharging between 85% and 95% SOC.

It seems after some research, trial and error I have this code which tested for TRUE and for FALSE situation (with another dummy sensor for battery state of charge).

{% set var_temp = states('sensor.battery_state_of_charge') | float %}
{% set var_temp2 = states('sensor.battery_power') | float %}
{% if var_temp < 95 or var_temp2 > 50 %} TRUE
{% endif %}
{% if var_temp == 100 %} FALSE
{% endif %}

Please format code correctly with the </> button.

Shorter version of your first bit of code:

{{ states('sensor.battery_state_of_charge')|float(0) < 95 or
   states('sensor.battery_power')|float(0) > 50 }}

Use that as the state template in a template binary sensor helper:

That’s just the first bit of your code: your template doesn’t output anything if, for example, var_temp is 98 and var_temp2 is 40.

Binary sensor templates are best set up with true/false statements, rather than ifs that return true or false.

Thanks but…

The condition <95 or >50 just triggers the binary sensor to be “set” (charging ON).
If the var is >95 or the var2 <50 does not mean the boolean should reset.
The reset is done by ==100 condition (charging off).
In the sensor dependent values moves in between, the binary sensor just keep its previous state.
So this is just two if’s not if/else.
It works as expected so far.

Am I thinking wrong?

Yes.

I’m surprised. I now see what you’re trying to do, but an empty output is an error state, not a “hold prior value” instruction.

This sounds like a job for a trigger-based template binary sensor, which is currently only available via YAML; or an automation switching an input boolean helper.

template:
  - trigger:
      - platform: numeric_state
        entity_id: sensor.battery_state_of_charge
        below: 95
        id: on
      - platform: numeric_state
        entity_id: sensor.battery_power
        above: 50
        id: on
      - platform: numeric_state
        entity_id: sensor.battery_state_of_charge
        above: 99.9
        id: off
    binary_sensor:
      - name: inverter
        state: "{{ trigger.id }}"

On the same principle, the implementation for my second request from the first post looks like this:

{% set var_temp1 = states('sensor.battery_state_of_charge') | float %}
{% set minutes = now().minute %}
{% set hours = now().hour %}
{% if hours == 0 and minutes == 1 %} FALSE
{% endif %}
{% if var_temp1 == 100 %} TRUE
{% endif %}

The entity binary_sensor.pylontech_charging_cycle_done is supposed to turn true when SOC reaches 100 and resets when the day changes (hh:mm = 00:01) or maybe I am changing the condition for “reset” action (SOC<80, timer 72h, etc.)

thanks.
I will try this also in the next days.

My second implementation behaves like you said.
Is stays True only as long as the SOC is 100.