Need help with a template that compares two numbers

Hello there,
I want my home assistant to start an automation as soon as the battery limit is reached.

ValueError: Template error: int got invalid input ‘unknown’ when rendering template ‘{{ states(‘sensor.opel_combo_battery’) | int = states(‘sensor.opel_combo_battery_charging_limit’) | int }}’ but no default was specified

My sensor seems to read unknown, although when I check it gives me a value

This is my template, which is supposed to trigger the automation as soon as the limit is reached or exceeded:

{{ states(‘sensor.opel_combo_battery’) | int >= states(‘sensor.opel_combo_battery_charging_limit’) | int }}

Your second example with the >= looks ok but this one probably needs two equals to be a comparison operator ( == ) instead of the single one you have. A single = is an assignment operator, not comparison.

Also, I usually do int(0) instead of just int, to provide a 0 as a default value in the case of an error.

Good luck.

looks like you have quotes . Has to be like this.

{{ states('sensor.opel_combo_battery') | int >= states('sensor.opel_combo_battery_charging_limit') | int }}

or with default of 0 if unknown

{{ states('sensor.opel_combo_battery') | int(0) >= states('sensor.opel_combo_battery_charging_limit') | int(0) }}

Blacky :grinning:

What does it say for the value in the developers tools->states table (not the entity details as you show)?

Have you tried the template in the dev tools template editor to see where it’s failing?

Instead of a Template Trigger, I suggest you use a Numeric State Trigger.

In the Automation editor’s Visual mode, the Numeric State Trigger would look like this.

Ignore the “Unknown entity selected” message. That’s because your sensors don’t exist on my system.

The same trigger in the Editor’s YAML mode looks like this (short and simple).

trigger: numeric_state
entity_id:
  - sensor.opel_combo_battery
above: sensor.opel_combo_battery_charging_limit

However, if you prefer to use a Template Trigger then here is the corrected version (same as Blacky’s second example):

{{ states('sensor.opel_combo_battery') | int(0) >= states('sensor.opel_combo_battery_charging_limit') | int(0) }}

It will trigger when the battery charge meets or exceeds the charging limit. Both values are converted to integers. If either value cannot be converted to an integer, a default value of zero is used.

The error message above is the output of the template thing in the developer tools. I guess the missing ‘>’ in ‘>=’ is a result of the markdown, as it does appear as ‘>=’ in the error message on my end. Sorry about that.

The developer tools- states gives me the following output,


so it should be 46 or whatever percentage is there. The sensor also has been available all the time in the timeline.

@blacky your line is identical to mine on my end, and it gives the same error message in developer tools - templates.

@123 I’d love to use the numeric state trigger, but that can only be set to a entity value and above instead of above and equal to as far as I’m aware. So I’d have to make another sensor that always detracts 1 from the battery limit and then set above that as trigger value, that seems really convoluted though.

Thank you all for your suggestions. Maybe I’m still reading the output of the states wrong?

OK copy this exactly into the template editor and tell us the results:

{{ states('sensor.opel_combo_battery') | int >= states('sensor.opel_combo_battery_charging_limit') | int }}

{{ states('sensor.opel_combo_battery') }} 

{{ states('sensor.opel_combo_battery_charging_limit') }}

{{ states('sensor.opel_combo_battery') | int(0) >= states('sensor.opel_combo_battery_charging_limit') | int(0) }}

I just found the solution, the hint with the states developer tool helped.
The problem was the charging limit sensor, as that had two states (on, 80). number.opel(…)limit gave me the correct result instead of sensor.opel(…)limit.
Technically, sensor never existed, the entities were always called number.opel_combo_battery_limit and switch.opel_combo_battery_charging_limit

Thank you guys very much!
@123 @finity @Blacky

For completeness sake, my new template and the output of the states tool for the battery charging limit sensor:
{{ states(‘sensor.opel_combo_battery’) | int >= states(‘number.opel_combo_battery_charging_limit’) | int }}

1 Like