Help about template

can any one tell me what is the problem in this template. i have other sensor named router1uptime and that sensor is running perfectly. but it gives me only seconds and i want to convert that second to days, hours, minutes and seconds.

router1uptime tsensor:

platform: snmp
name: 'router1uptime'
host: 192.168.254.254
baseoid: .1.3.6.1.2.1.1.3.0
version: 2c
accept_errors: true

r1uptime template:

platform: template
sensors:
  r1uptime:
     value_template: >
          {% set time = states.sensor.router1uptime.state %}
          {% set minutes = ((time % 3600) / 60) | int %}
          {% set hours = ((time % 86400) / 3600) | int %}
          {% set days = (time / 86400) | int %}

          {%- if time < 60 -%}
            Less than a minute
          {%- else -%}
            {%- if days > 0 -%}
              {%- if days == 1 -%}
                1 day
              {%- else -%}
                {{ days }} days
              {%- endif -%}
            {%- endif -%}
            {%- if hours > 0 -%}
              {%- if days > 0 -%}
                {{ ', ' }}
              {%- endif -%}
              {%- if hours == 1 -%}
                1 hour
              {%- else -%}
                {{ hours }} hours
              {%- endif -%}
            {%- endif -%}
            {%- if minutes > 0 -%}
              {%- if days > 0 or hours > 0 -%}
                {{ ', ' }}
              {%- endif -%}
              {%- if minutes == 1 -%}
                1 minute
              {%- else -%}
                {{ minutes }} minutes
              {%- endif -%}
            {%- endif -%}
          {%- endif -%}

and here is the error:

2017-09-06 07:45:02 ERROR (MainThread) [homeassistant.helpers.entity] Update for sensor.r1uptime fails
Traceback (most recent call last):
  File "/usr/lib/python3.6/site-packages/homeassistant/helpers/entity.py", line 223, in async_update_ha_state
    yield from self.async_update()
  File "/usr/lib/python3.6/asyncio/coroutines.py", line 210, in coro
    res = func(*args, **kw)
  File "/usr/lib/python3.6/site-packages/homeassistant/components/sensor/template.py", line 145, in async_update
    self._state = self._template.async_render()
  File "/usr/lib/python3.6/site-packages/homeassistant/helpers/template.py", line 102, in async_render
    return self._compiled.render(kwargs).strip()
  File "/usr/lib/python3.6/site-packages/jinja2/asyncsupport.py", line 76, in render
    return original_render(self, *args, **kwargs)
  File "/usr/lib/python3.6/site-packages/jinja2/environment.py", line 1008, in render
    return self.environment.handle_exception(exc_info, True)
  File "/usr/lib/python3.6/site-packages/jinja2/environment.py", line 780, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/lib/python3.6/site-packages/jinja2/_compat.py", line 37, in reraise
    raise value.with_traceback(tb)
  File "<template>", line 1, in top-level template code
TypeError: not all arguments converted during string formatting

Where’s the rest of the template?

remove the ‘-’ after the ‘>’

not working

Can you correct the formatting please, so it’s all in the code block (see the big blue box at the top)

If you start with a timestamp, this code works fine.
My guess is that your sensor doesn’t return a numeric value.
Try this:

{{ states.sensor.router1uptime.state > 0 }}

If it returns blank, then your sensor is not a number.

i updated the main post with log details

hacom

here what i am seeing

I think when you’re assigning a value to time it’s setting it as a string and it needs to be an int

change
{% set time = states.sensor.router1uptime.state %}
to
{% set time = states.sensor.router1uptime.state | int %}

1 Like

thumbs up. its works dude.

stupid question, but what does the “>” do in the template?

It just means that you’re putting in multi-line input. instead of

{% if states.sensor.sensorname.attributes.state == 'home' %} home {% elif states.somesensor.sensorname.atrributes.state == 'away' %} away {% else %} Lost {% endif %}

you can write it as

value_template: >
  {% if states.sensor.sensorname.attributes.state == 'home' %}
    home 
  {% elif states.somesensor.sensorname.atrributes.state == 'away' %}
    away 
  {% else %}
    Lost
  {% endif %}

it’s clean and easy to understand

1 Like

thank you for the explanation!