Template sensor problem with the use of timestamp on zwave

Hi,

I am having trouble with this template:

  - platform: template
    sensors:
      branndetektor_kjokken_template:
        friendly_name: 'Branndetektor Kjøkken'
        value_template: >-
         {%- if is_state("sensor.brannvarsler_kjokken_smoke", "2") -%}
          Brann
         {%- elif is_state("sensor.brannvarsler_kjokken_smoke", "3") -%}
          Brann Test
         {%- elif states('sensor.timestamp_now') | int - as_timestamp(states.zwave.brannvarsler_kjokken.last_updated) | int > (5000) -%}
          Error
         {%- else -%}
          Ok
         {%- endif -%}
        icon_template: >-
         {%- if is_state("sensor.brannvarsler_kjokken_smoke", "2") -%}
          mdi:alert-octagram
         {%- elif is_state("sensor.brannvarsler_kjokken_smoke", "3") -%}
          mdi:alert-octagram
         {%- elif (states.sensor.timestamp_now.state | float - as_timestamp(states.zwave.brannvarsler_kjokken.last_updated)) | float > (5000) -%}
          mdi:alert-octagram
         {%- else -%}
          mdi:fire
         {%- endif -%}

The sensor timestamp_now is a command_line sensor that updates every 60 seconds.
The template shows error if my device has not updated in the last 5000 seconds, like I want it too.

The problem is, since zwave is not ready at startup, it throws a couple of errors like this during startup:

Could not render template Branndetektor Kjøkken, the state is unknown.

Could not render icon template Branndetektor Kjøkken, the state is unknown.

Does anyone know how to fix this?

You’ll have to present an alternative to the template result, even before it can report “unknown”. Here’s an example:

{% if states.binary_sensor.fibaro_system_fgms001zw5_motion_sensor_sensor.last_updated %}
  {% if as_timestamp(now()) - as_timestamp(states.binary_sensor.fibaro_system_fgms001zw5_motion_sensor_sensor.last_updated)  > 120 %}true{%else%}false{%endif%}
{%else%}
   n/a
{%endif%}

Thank you. I got it with just the timestamp, I do it something similar on other templates, but can’t figure out to do it with elif states. Can you please show me how it would look with my template with elif states?

best would be if you copy/paste your code and then explain what you want to achieve. To make it simple, you could simply exchange the “else” by another “elif” in order to insert more conditions.

The code for my template is in my first post.
It is a fibaro smoke sensor. What I want to achive, is when the sensor report the number 2, it reports Fire, the number 3, fire test, and if it has not been updated for 5000 seconds, it reports error.
The template works, but as mentioned earlier, it reports fault at startup when I add this line:

 {%- elif states('sensor.timestamp_now') | int - as_timestamp(states.zwave.brannvarsler_kjokken.last_updated) | int > (5000) -%}
  Error

If I remove that part, I just use is_state for the number reports, and therefor no errors at startup.

This fault error at startup may be related to the fact that there’s no value available yet. Especially zwave tends to take some time after startup, before it reports any values to HASS. I don’t believe there’s a solution, just ignore the errors. Further information here:

states.zwave.brannvarsler_kjokken.attributes.last_updated

The belows are my sensors similar to yours.
It works well and I hope it will help you

  • platform: command_line
    name: timestamp_now
    command: “date +%s”
    scan_interval: 60

  • platform: template
    sensors:
    no_presence:
    value_template: ‘{% if states.sensor.timestamp_now.state | int - as_timestamp(states.binary_sensor.motion_sensor_158d000120cb77.last_changed) > 1800
    and states.sensor.timestamp_now.state | int - as_timestamp(states.device_tracker.af950833.last_changed) > 1800
    and states.sensor.timestamp_now.state | int - as_timestamp(states.device_tracker.sunbeach.last_changed) > 1800
    and is_state(“device_tracker.af950833”, “not_home”)
    and is_state(“device_tracker.sunbeach”, “not_home”) %}Away
    {% else %}Home
    {% endif %}’

Finally found the solution to getting no errors! :grinning:

  - platform: template
    sensors:
      branndetektor_kjokken_template:
        friendly_name: 'Branndetektor Kjøkken'
        value_template: >-
         {% if states.sensor.brannvarsler_kjokken_smoke.last_updated %}
           {%- set brann = states.sensor.brannvarsler_kjokken_smoke.state -%} 
             {%- if brann == '2' -%}
              Brann
           {%- else -%}
             {%- if brann == '3' -%}
              Brann Test
           {%- else -%}
             {%- if states('sensor.timestamp_now') | int - as_timestamp(states.zwave.brannvarsler_kjokken.last_updated) | int > (5000) -%}
              Error
         {%- else -%}
          Ok
               {%- endif -%}
             {%- endif -%}
           {%- endif -%}
         {%- endif -%}
        icon_template: >-
         {% if states.sensor.brannvarsler_kjokken_smoke.last_updated %}
           {%- set brann = states.sensor.brannvarsler_kjokken_smoke.state -%} 
             {%- if brann == '2' -%}
              mdi:alert-octagram
           {%- else -%}
             {%- if brann == '3' -%}
              mdi:alert-octagram
           {%- else -%}
             {%- if states('sensor.timestamp_now') | int - as_timestamp(states.zwave.brannvarsler_kjokken.last_updated) | int > (5000) -%}
              mdi:alert-octagram
         {%- else -%}
          mdi:fire
               {%- endif -%}
             {%- endif -%}
           {%- endif -%}
         {%- endif -%}

Man oh man, it may work, but reading that is painful. You also have some conditions that may return in ‘no-icon’ and ‘no-state’. You should utilize elif statements as well. See below:

  - platform: template
    sensors:
      branndetektor_kjokken_template:
        friendly_name: 'Branndetektor Kjøkken'
        value_template: >
         {% if states.sensor.brannvarsler_kjokken_smoke.last_updated %}
           {% set brann = states.sensor.brannvarsler_kjokken_smoke.state %} 
           {% if brann == '2' %}
              Brann
           {% elif brann == '3' %}
              Brann Test
           {% elif states('sensor.timestamp_now') | int - as_timestamp(states.zwave.brannvarsler_kjokken.last_updated) | int > (5000) %}
              Error
           {% endif %} # WHAT HAPPENS HERE IF THIS IF STATEMENT FAILS???
         {% else %}
           Ok
         {% endif %}
        icon_template: >
         {% if states.sensor.brannvarsler_kjokken_smoke.last_updated %}
           {% set brann = states.sensor.brannvarsler_kjokken_smoke.state %} 
           {% if brann == '2' %}
              mdi:alert-octagram
           {% elif brann == '3' %}
              mdi:alert-octagram
           {% elif states('sensor.timestamp_now') | int - as_timestamp(states.zwave.brannvarsler_kjokken.last_updated) | int > (5000) %}
              mdi:alert-octagram
           {% endif %} # WHAT HAPPENS HERE IF THIS IF STATEMENT FAILS???
         {% else %}
           mdi:fire
         {% endif %}

I also noticed that you used {%- and -%} all over the place. The - is only used when you want to remove the lines whitespace when executing. That’s really only needed when dealing with list items. You aren’t listing items out so you shouldn’t need them.

Thank you so much for your info, much appreciated.
But when I try your template, I don’t get an icon or state if the first statement fails.
If I use my template, I get status ok and fire-icon.

Edit: With a little adjustment it works, and looks a little better:

value_template: >
 {% if states.sensor.brannvarsler_kjokken_smoke.last_updated %}
   {% set status = states.sensor.brannvarsler_kjokken_smoke.state %} 
   {% if status == '2' %}
      Brann
   {% elif status == '3' %}
      Brann Test
   {% elif states('sensor.timestamp_now') | int - as_timestamp(states.zwave.brannvarsler_kjokken.last_updated) | int > (5000) %}
      Error
 {% else %}
  Ok
   {% endif %}
 {% endif %}
icon_template: >-
 {% if states.sensor.brannvarsler_kjokken_smoke.last_updated %}
   {% set status = states.sensor.brannvarsler_kjokken_smoke.state %} 
   {% if status == '2' %}
      mdi:alert-octagram
   {% elif status == '3' %}
      mdi:alert-octagram
   {% elif states('sensor.timestamp_now') | int - as_timestamp(states.zwave.brannvarsler_kjokken.last_updated) | int > (5000) %}
      mdi:wifi-strength-alert-outline
 {% else %}
    mdi:fire
   {% endif %}
 {% endif %}

Thanks again :slight_smile:

Your endif’s are in the wrong spot, and you keep falling for it. End if’s need to go hand in hand with the if statement. Take a look at the following code and note the outside if statement and the inside if statement:

{% if (STATEMENT_OUTSIDE) %} <------  'START OF MAIN IF'
  {% if (STATEMENT_INSIDE) %} <--------  'START OF NESTED IF'
    {{ DO_SOMETHING_1 }}
  {% elif (SOME_OTHER_STATEMENT) %} <--  'ELSE IF FOR NESTED IF'
    {{ DO_SOMETHING_2 }}
  {% else %} <-------------------------  'ELSE FOR NESTED IF'
    {{ DEFAULT_TO_THIS_WHEN_NESTED_IF_FAILS }}
  {% endif %} <------------------------  'END OF NESTED IF'
{% else %} <------------------------  'ELSE OF MAIN IF'
  {{ DEFAULT_TO_THIS_WHEN_MAIN_IF_FAILS }}
{% endif %} <-----------------------  'END OF MAIN IF'

Now apply that to your code, take note, You do not have a ‘DEFAULT_TO_THIS_WHEN_NESTED_IF_FAILS’ in your code at all, even with the updates below:

    value_template: >
     # MAIN IF STATEMENT
     {% if states.sensor.brannvarsler_kjokken_smoke.last_updated %}
       {% set status = states.sensor.brannvarsler_kjokken_smoke.state %} 
       # NESTED IF STATEMENT
       {% if status == '2' %}
          Brann
       # NESETED ELSE IF 1
       {% elif status == '3' %} # 
          Brann Test
       # NESTED ELSE IF 2
       {% elif states('sensor.timestamp_now') | int - as_timestamp(states.zwave.brannvarsler_kjokken.last_updated) | int > (5000) %}
          Error
       # ***THIS IS WHERE DEFAULT_TO_THIS_WHEN_NESTED_IF_FAILS WOULD GO***
       # END OF NESTED IF
       {% endif %}
     # MAIN ELSE STATEMENT
     {% else %} 
      Ok
     # END OF MAIN IF STATEMENT
     {% endif %}
    icon_template: >-
     {% if states.sensor.brannvarsler_kjokken_smoke.last_updated %}
       {% set status = states.sensor.brannvarsler_kjokken_smoke.state %} 
       {% if status == '2' %}
          mdi:alert-octagram
       {% elif status == '3' %}
          mdi:alert-octagram
       {% elif states('sensor.timestamp_now') | int - as_timestamp(states.zwave.brannvarsler_kjokken.last_updated) | int > (5000) %}
          mdi:wifi-strength-alert-outline
       {% endif %}
     {% else %}
        mdi:fire
     {% endif %}

Ok, so it should look like this then?

    value_template: >
     {% if states.sensor.brannvarsler_kjokken_smoke.last_updated %}
       {% set status = states.sensor.brannvarsler_kjokken_smoke.state %} 
       {% if status == '2' %}
          Brann
       {% elif status == '3' %}
          Brann Test
       {% elif states('sensor.timestamp_now') | int - as_timestamp(states.zwave.brannvarsler_kjokken.last_updated) | int > (5000) %}
          Error
       {% else %}
        Ok
       {% endif %}
     {% else %}
      Not Ready
     {% endif %}

It works.
I see your point, then I also get a state if the smoke sensor is not ready. Thanks!

You got it. Looks good now!

Thanks for the time on your detailed explanation, great to know that when working on templates.

1 Like