Template help string

Hi there, running in some issue where i am not able to get the correct info to show up

i have tried the template tool (under dev tools) and it seems to work there, however it does not work when i actually put it under my value template in my config file

The sensor work and output this string

16:33:00 up 1 day, 4:36, load average: 0.11, 0.18, 0.14

Im trying to change it so that it shows:
Router is up for 1 day, 4 hours and 36 min

My config file:

  - platform: command_line
    command: "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i /config/ssh/ssh_asus_private_key '[email protected]' /usr/bin/uptime"
    name: Router Uptime
    value_template: "Router is up for {{ states.sensor.router_uptime.state.split(' ')[2]}} {{ states.sensor.router_uptime.state.split(' ')[3]}} {{ states.sensor.router_uptime.state.split(' ')[4].split(':')[0]}} hours and {{ states.sensor.router_uptime.state.split(':')[3].split(',')[0]}} minute"

Can anyone help?

Try this:

    value_template: >
      {% set x = states('sensor.router_uptime') | regex_findall_index(".* up (\d+) day, (\d+):(\d+),") %}
      {% if x | length == 3 %}
        Router is up for {{x[0]}} days, {{x[1]}} hours and {{x[2]}} minutes
      {% else %}
        Error
      {% endif %}

If you want it to indicate day instead of days when it’s only 1 day then you can use this:

        Router is up for {{x[0]}} day{{'s' if x[0]|int > 1 else ''}}, {{x[1]}} hours and {{x[2]}} minutes

What I posted assumes the value reported by sensor.router_uptime is always in this format:

NN:NN:NN up NN day, NN:NN,  <whatever>

If the router’s uptime is less than a day and it reports it like this:

12:42:10 up 0 day, 10:09, etc

then template will continue to work. However, the template will fail if the value is reported like this (without days):

12:42:10 up 10:09, etc
1 Like

Thanks, really appreciate, i now have a better understanding of templating

however upon using your suggestion, this is throwing out an error in my logs and is still not reporting

command_line: Error on device update!
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 312, in _async_add_entity
    await entity.async_device_update(warning=False)
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 476, in async_device_update
    await self.hass.async_add_executor_job(self.update)
  File "/usr/local/lib/python3.7/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/usr/src/homeassistant/homeassistant/components/command_line/sensor.py", line 126, in update
    value, STATE_UNKNOWN
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 251, in render_with_possible_json_value
    error_value,
  File "/usr/local/lib/python3.7/concurrent/futures/_base.py", line 435, in result
    return self.__get_result()
  File "/usr/local/lib/python3.7/concurrent/futures/_base.py", line 384, in __get_result
    raise self._exception
  File "/usr/src/homeassistant/homeassistant/util/async_.py", line 49, in run_callback
    future.set_result(callback(*args))
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 276, in async_render_with_possible_json_value
    return self._compiled.render(variables).strip()
  File "/usr/local/lib/python3.7/site-packages/jinja2/environment.py", line 1090, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.7/site-packages/jinja2/environment.py", line 832, in handle_exception
    reraise(*rewrite_traceback_stack(source=source))
  File "/usr/local/lib/python3.7/site-packages/jinja2/_compat.py", line 28, in reraise
    raise value.with_traceback(tb)
  File "<template>", line 1, in top-level template code
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 863, in regex_findall_index
    return re.findall(find, value, flags)[index]
IndexError: list index out of range

Change it to this:

    value_template: >
      {% set x = value | regex_findall_index(".* up (\d+) day, (\d+):(\d+),") %}
      {% if x | length == 3 %}
        Router is up for {{x[0]}} days, {{x[1]}} hours and {{x[2]}} minutes
      {% else %}
        Error
      {% endif %}

Unfortunately same error :frowning:

  - platform: command_line
    command: "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i /config/ssh/ssh_asus_private_key '[email protected]' /usr/bin/uptime"
    name: Router Uptime
    value_template: >
      {% set x = value | regex_findall_index(".* up (\d+) day, (\d+):(\d+),") %}
      {% if x | length == 3 %}
        Router is up for {{x[0]}} days, {{x[1]}} hours and {{x[2]}} minutes
      {% else %}
        Error
      {% endif %}

So what happens when it’s just this?

  - platform: command_line
    command: "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i /config/ssh/ssh_asus_private_key '[email protected]' /usr/bin/uptime"
    name: Router Uptime
    value_template: "{{ value }}"

If that still fails to work, try removing the outer single-quotes for the command line. That seemed to fix the problem in this post.

    command: ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i /config/ssh/ssh_asus_private_key '[email protected]' /usr/bin/uptime
1 Like

Thanks,

im out for a while so i wont be able to t est, but i did manage to make it work by creating a 2nd sensor (and making it refer to the 1st sensor)
Im not sure why it worked but it looks like by having it ‘self-referencing’ was causing an issue

All sensors can refer to their received data using the value variable or, if the data is in JSON format, value_json.