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?
aceindy
(Aceindy)
June 5, 2023, 6:26pm
3
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 %}
123
(Taras)
June 5, 2023, 6:52pm
4
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) }}
123
(Taras)
June 5, 2023, 7:58pm
6
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 %}
123
(Taras)
June 6, 2023, 11:48am
9
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?
spopinski
(Spopinski)
June 7, 2023, 12:57pm
10
Yes it should. How do I fix this?
123
(Taras)
June 7, 2023, 1:24pm
11
Adjust the configuration values of the Trend Sensor. It may take some experimentation to get the best results.