Template in config not working < not supported 'str' and 'int'

This is a Testing Version with minutes of iphone battery being lower than 50, resetting until it goes under it again. In the original its days since vacuum robot level has fallen under 50. To get a integer of how many days its been since it has cleaned basically.

So i think i almost got it about working, when i paste the template into the template checker i get this error:

TypeError: ‘<’ not supported between instances of ‘str’ and ‘int’

My code in config.yaml is:

  - platform: template
    sensors:
      minutes_since_iphone_battery_low:
        friendly_name: Minutes Since iPhone Battery Low
        unit_of_measurement: minutes
        value_template: >
          {% if states.sensor.pp_iphone_battery_level.state < 50 %}
            {% set now = as_timestamp(now()) %}
            {% set pp_iphone_low_battery_time = states.sensor.pp_iphone_low_battery_time.state %}
            {% set pp_iphone_low_battery_time = as_timestamp(pp_iphone_low_battery_time) %}
            {% set minutes_since_iphone_battery_low = (now - pp_iphone_low_battery_time) // 60 %}
            {{ minutes_since_iphone_battery_low }}
          {% else %}
            0
          {% endif %}

I setup the datetime helper which seems to be working since the automation is successfully writing to it


image

Automation:

 alias: Update iphone low battery time
 description: ""
 trigger:
   - platform: state
     entity_id:
       - sensor.pp_iphone_battery_level
     to: "50"
     from: "51"
 condition: []
 action:
   - service: input_datetime.set_datetime
     data:
       time: "{{ now().strftime(\"%H:%M\") }}"
     target:
       entity_id: input_datetime.pp_iphone_low_battery_time
 mode: single

When i look at the history of the sensor i created in the config it doesn’t get updated. So i the issue has to be in the Template. Anyway if anyone could help me out.

image

all states are strings.

so this is comparing a string (from the state) to an int and that can’t work:

 {% if states.sensor.pp_iphone_battery_level.state < 50 %}

it needs converted to an int or float before the comparison:

 {% if states.sensor.pp_iphone_battery_level.state | int < 50 %}

also it’s better to use the states() method instead of the state object method you are using:

 {% if states('sensor.pp_iphone_battery_level') | int < 50 %}
1 Like

thanks, it cetrainly got me on the right track. And i noticed i don’t even need any of this since the automation is already doing it.
I went with:

- platform: template
    sensors:
      minutes_since_iphone_battery_low2:
        value_template: "{{ ((as_timestamp(now()) - as_timestamp(states('input_datetime.pp_iphone_low_battery_time'))) / 60) | round(1) }}"
        unit_of_measurement: "minutes"

could you help me understand why it is better to use states() method instead of states?

It’s explained in the documentation:

Reference: Templating - States

1 Like

i know this is kind of old but i’m hoping someone can point me in the right direction or give me some guidance…

{{ expand(‘group.birthdays’) | selectattr(‘attributes.countdown’, ‘lt’, ‘30’) | list }}

TypeError: ‘<’ not supported between instances of ‘int’ and ‘str’

i’m not sure why… the countdown attribute within each of the sensors is a numeric value… and i’m going to do more work with this but for some reason it’s erroring already and i can’t understand why…

so for instance i’ll have a sensor, we’ll call it ‘sensor.dad’

the state is the ‘year-month-day’ of their birthday and the attributes are templated to create the age and a countdown to their birthday

sensor.birthday_dad
state=1979-08-29
friendly_name: Birthday Dad
age: 44
countdown: 16

Remove the quotes around the value so that it is a number not a string:

{{ expand('group.birthdays') | selectattr('attributes.countdown', 'lt', 30) | list }}

i knew it was something simple but i just couldn’t wrap my head around it. thank you so much!