Template tweak & counter state assistance please

I am making a template to try and help the family understand approximately how long the house batteries take to recharge from different appliance uses.
So if something takes 4 amp hours from the battery bank then:

{{ (4 * (2000) / ((states('sensor.cerbo_pv_ac') | round(2)) | round(2))) |round (0) }} minutes.

The above template works for me, although is probably messier than it needs to be.
This one doesn’t:

{{ (states('sensor.cerbo_battery_consumed_ah') * (2000) / ((states('sensor.cerbo_pv_ac') | round(2)) | round(2))) |round (0) }} minutes.

FYI, the sensor.cerbo_battery_consumed_ah value is negative, which is possibly also a problem, don’t know how to fix that at the moment.

Separate idea, I have a counter helper that I was trying to see if I could use as a trigger if it hasn’t been changed in the last hour. But this doesn’t seem to be working, is this a possibility and I’m just doing it wrong or ???

platform: state
entity_id: counter.daily_tank_full
for:
  hours: 1
  minutes: 0
  seconds: 0
  milliseconds: 0

Thanks for your time.

The state value for all entities is a string. That means this reports a string:

states('sensor.cerbo_battery_consumed_ah')`

The reported value may look like a number but it’s a string. Because it’s a string, you cannot use it to do arithmetic. That’s why this fails:

states('sensor.cerbo_battery_consumed_ah') * (2000) 

To do arithmetic with it you first have to convert it from a string to an integer (int) or floating-point number (float). Which conversion you choose depends on whether you only want whole values (int) or with a decimal point (float).

I’m not sure why you are employing multiple intermediate round(2) filters within the calculation when you ultimately filter the final value with round(0).

Copy-paste this into the Template Editor and see if it works for you:

{{ (states('sensor.cerbo_battery_consumed_ah') | float * 2000 / states('sensor.cerbo_pv_ac') | float) | round(0) }} minutes.

Thank you Taras, that works, and helped explain to me what some of it means. As for why the multiple filters… cause I’m making it up as I go and don’t really know what I’m doing.

Any ideas for the counter not changed for x time?

After you created the automation containing that State Trigger, did the counter’s value remain unchanged or did it change and then hold its new state for over an hour?

Here’s my understanding of how it works:

If it didn’t change, in other words it had the same state value before and after you created the automation, I don’t see how it could trigger the State Trigger. A State Trigger is triggered by a state-change. The one you created looks for any change in the entity’s state value followed by holding that new value for 1 hour. If there’s no state-change, there’s nothing to start the 1-hour countdown.

Ah! Understanding the subtle differences.

I was hoping the trigger would be: “if this hasn’t changed in the last hour, then trigger.”
which I could then enable checking at certain times of the day

whereas it is more like: “trigger one hour after it last changed”
meaning in my use case, if the counter changed more than 1 hour before my time window then it doesn’t trigger.

Thanks.

It’s more like “trigger after it has maintained its most recent state for 1 hour”

What you were trying to do can be accomplished with a template trigger:

platform: template
value_template:  "{{ as_timestamp(now()) - as_timestamp(states.counter.daily_tank_full.last_changed) >= 3600}}"

1 Like

Thank you very much.

For future reference, you aren’t obligated to convert to timestamps and can perform the subtraction directly as datetime objects.

value_template: "{{ now() - states.counter.daily_tank_full.last_changed >= timedelta(hours=1) }}"

Be advised that an entity’s last_changed is affected by a restart (i.e. it’s set to the startup time).

1 Like

That’s very useful to know, thanks