Create sensor from a state of other sensor

Hi, I have a battery system which I successfully connected to a DC meter (PZEM-017).
But this sensor only allow positive values so I only have an increasing kWh value from the energy sensor.

Now I want to create a new sensor based on the following logic:

if: sensor.battery_voltage is equal/increasing in the past 30 or 60 seconds AND the time is between 6AM to 5PM then =charging is TRUE

Then based on that sensor I want to create another sensor that will record my sensor.battery_energy.
This will be my battery energy input of the day.
Is this possible?

Thanks!

I got this far:

{{ states('sensor.battery_voltage') | float >=
      states('sensor.battery_voltage') | float - 10}}

What is the unit of “-10” here? seconds? minutes?
Edit: I guess this just compare values not values in time series…

And here is part of the code of the time range:

{% if now().strftime('%T') > '07:00:00' and now().strftime('%T') < '17:00:00' %}
        {{ (states('sensor.battery_current')|float)|round }}
      {% else %}
        0
      {% endif %}

Both tested good in the templates tester. Now how do I combine it to achieve my objective?

something like this?

template:
  - sensors:
    - name: sensor recorder
      state: >
          {% if now().strftime('%T') > '07:00:00' and now().strftime('%T') < '17:00:00' and (states('sensor.battery_voltage') | float(0) >= states('sensor.battery_voltage') | float(0) - 10}) %}
            {{ (states('sensor.battery_current')|float)|round(0) }}
          {% else %}
            0
          {% endif %}

What do you think that template does? Because regardless of the numerical value of sensor.battery_voltage, the template will always report true.

Yes I realised that and already discarded the idea
What I mean is something like this but I don’t know the code:

{{ states('sensor.battery_voltage') | float >= states('sensor.battery_voltage') | - (10 seconds ago) }}

There’s nothing available in Home Assistant’s templating language to report an entity’s value from X seconds ago.

When an entity’s value changes, you can compare it to its previous value (but not a value before that).

If you want to know if an entity’s value is increasing/decreasing, you should consider using the Trend Sensor integration.

Aha!

Thanks for the insight.

I managed to get the binary sensor working

binary_sensor:
  - platform: trend
    sensors:
      v_falling:
        entity_id: sensor.battery_voltage
        friendly_name: "Battery Discharging"
        sample_duration: 120
        max_samples: 40
        min_gradient: -0.00083
        device_class: battery_charging

      v_rising:
        entity_id: sensor.battery_voltage
        friendly_name: "Battery Charging"
        sample_duration: 120
        max_samples: 40
        min_gradient: 0.00083
        device_class: battery_charging

Now the states is alternating on and off.

Can you point me the direction on how to combine this with my time range?

{% if now().strftime('%T') > '07:00:00' and now().strftime('%T') < '17:00:00' %}
        {{ (states('sensor.battery_current')|float)|round }}
      {% else %}
        0
      {% endif %}

Is this about right?

state: >
      {% if now().strftime('%T') > '07:00:00' and now().strftime('%T') < '17:00:00'
      and is_state('binary_sensor.v_rising', 'on') %}
        {{ (states('sensor.battery_current')|float)|round }}
      {% else %}
        0
      {% endif %}

According to the Logbook, it’s alternating every 3 seconds. Shouldn’t it turn on, and stay on, during the entire time when battery voltage is increasing?

Yes it should. How do I fix this?

Adjust the configuration values of the Trend Sensor. It may take some experimentation to get the best results.