Usual question, can't find the typo in sensor and getting errors in converting seconds to minutes, hours, days

Dear all,

I’ve spent some time in reading this post Convert seconds to days, hours, minutes and of course Template Sensor documentation.

Unfortunately I still get error messages when checking my config.

So I assume there is just a typo, wrong " ’ quotes, or indentation, I just can’t find it.

Help is highly appreciated.
Ralf

  - platform: fritzbox_netmonitor
    host: my.box.ip.addr
    name: fritzbox
  - platform: template
    sensors:
      fritzbox_is_linked:
        friendly_name: 'Physikalisch verbunden'
        value_template: >-
          {% if is_state_attr('sensor.fritzbox', 'is_linked', 'True') %}
            ja
          {% else %}
            prüfen
          {% endif %}
      fritzbox_is_connected:
        friendly_name: 'Mit Internet verbunden'
        value_template: >-
          {% if is_state_attr('sensor.fritzbox', 'is_connected', 'True') %}
            ja
          {% else %}
            prüfen
          {% endif %}
      fritzbox_wan_access_type:
        friendly_name: 'Verbindungsart'
        value_template: '{{ states.sensor.fritzbox.attributes["wan_access_type"] }}'
      fritzbox_external_ip:
        friendly_name: 'Externe IP Adresse'
        value_template: '{{ states.sensor.fritzbox.attributes["external_ip"] }}'
      fritzbox_uptime:
        friendly_name: 'FB-Uptime'
        value_template: >-
          {% set fbuptime = state_attrib('sensor.fritzbox', 'uptime') | int %}
          {% set fbminutes = ((fbuptime % 3600) /60) | int %}
          {$ set fbhours = ((fbuptime % 86400) / 3600) | int %}
          {% set fbdays = (fbuptime / 86400) | int) %}
          {% if fbuptime < 60 %}
            unter 1 Minute
          {% else %}
            {{ fbdays }} d : {{ fbhours }} h : {{ fbminutes }} m
          {% endif %}

The sensors fritzbox_is_linked, fritzbox_is_connected, fritzbox_wan_access_type and fritzbox_external_ip are all working fine.
When adding the FB-uptime sensor I get a bunch of error messages:

➜  /workspace hassio ha check  
Processing... Done.

Error: Testing configuration at /config
INFO:homeassistant.util.package:Attempting install of fritzconnection==0.8.4
Failed config
  sensor.template: 
    - Invalid config for [sensor.template]: invalid template (TemplateSyntaxError: unexpected ')') for dictionary value @ data['sensors']['fritzbox_uptime']['value_template']. Got "{% set fbuptime = state_attrib('sensor.fritzbox', 'uptime') | int %} {% set fbminutes = ((fbuptime % 3600) /60) | int %} {$ set fbhours = ((fbuptime % 86400) / 3600) | int %} {% set fbdays = (fbuptime / 86400) | int) %} {% if fbuptime < 60 %}\n  unter 1 Minute\n{% else %}\n  {{ fbdays }} d : {{ fbhours }} h 
: {{ fbminutes }} m\n{% endif %}". (See ?, line ?). Please check the docs at https://home-assistant.io/integrations/sensor.template/
    - platform: template
      sensors: [source /config/configuration.yaml:201]
        fritzbox_external_ip: [source /config/configuration.yaml:221]
          friendly_name: Externe IP Adresse
          value_template: {{ states.sensor.fritzbox.attributes["external_ip"] }}
        fritzbox_is_connected: [source /config/configuration.yaml:210]
          friendly_name: Mit Internet verbunden
          value_template: {% if is_state_attr('sensor.fritzbox', 'is_connected', 'True') %}
  ja
{% else %}
  prüfen
{% endif %}
        fritzbox_is_linked: [source /config/configuration.yaml:202]
          friendly_name: Physikalisch verbunden
          value_template: {% if is_state_attr('sensor.fritzbox', 'is_linked', 'True') %}
  ja
{% else %}
  prüfen
{% endif %}
        fritzbox_uptime: [source /config/configuration.yaml:224]
          friendly_name: FB-Uptime
          value_template: {% set fbuptime = state_attrib('sensor.fritzbox', 'uptime') | int %} {% set fbminutes = ((fbuptime % 3600) /60) | int %} {$ set fbhours = ((fbuptime % 86400) / 3600) | int %} {% set fbdays = (fbuptime / 86400) | int) %}
 {% if fbuptime < 60 %}
  unter 1 Minute
{% else %}
  {{ fbdays }} d : {{ fbhours }} h : {{ fbminutes }} m
{% endif %}
        fritzbox_wan_access_type: [source /config/configuration.yaml:218]
          friendly_name: Verbindungsart
          value_template: {{ states.sensor.fritzbox.attributes["wan_access_type"] }}

Successful config (partial)
  sensor.template:
  1. state_attrib should be state_attr
  2. You have an $ instead of an % in the fbhours defintion
  3. You’re missing an open bracket in the fbdays definition.

You can simply copy and paste the whole value_template in Dev Tools/templates and play with it.

The first templates are clean and neat, the last one is a bit messy, it needs cleaning up a bit - it’s harder to read, so I’m surprised VDR found your errors so quickly and concisely - but then watching VDR at work is a pleasure in itself :smiley:

Wow!

Extremely fast and precise, but the 3. one was one unneccesary close bracket .

It’s always the same, you write code, re-read, re-read, re-read and don’t find these obvious things.

THANKS
Ralf