Timestamp if value < X

Hi. I’m trying to adapt a sensor for another purpose.
I should create a timestamp when the sensor drops below the value of 5 ( < 5 ).

This is the sensor from which I started:

    - name: "Ultimo Backup"
      unique_id: "ultimo_backup"
      icon: mdi:cloud-upload
      state: >
         {% if states('sensor.backup_state') == 'backed_up' %}
           {% set timestamp = as_timestamp(states.sensor.backup_state.attributes.last_backup) | timestamp_custom(" %d/%m/%Y - %H:%M ") %} {{ timestamp.lstrip("0") }}
         {% else %}
           Sconosciuto
         {% endif %}

This is the sensor I would like to create, but what should I put in place of {% set timestamp = as_timestamp(states.sensor.backup_state.attributes.last_backup)

    - name: "Ultimo Blackout"
      unique_id: "ultimo_blackout"
      icon: mdi:voltage
      state: >
         {% if states('sensor.ups_tensione_di_ingresso') | float <= 5 %}
           {% set timestamp = as_timestamp( ??? ') | timestamp_custom(" %d/%m/%Y - %H:%M ") %} {{ timestamp.lstrip("0") }}
         {% else %}
           Sconosciuto
         {% endif %}

Thanks

{{ (as_timestamp(now()) | timestamp_custom(" %d/%m/%Y - %H:%M ")).lstrip("0") }}

so:

- name: "Ultimo Blackout"
      unique_id: "ultimo_blackout"
      icon: mdi:voltage
      state: >
         {% if states('sensor.ups_tensione_di_ingresso') | float <= 5 %}
           {{ (as_timestamp(now()) | timestamp_custom(" %d/%m/%Y - %H:%M ")).lstrip("0") }}
         {% else %}
           Sconosciuto
         {% endif %}

Thanks for your help.
The sensor works when the value falls below 5, but when it returns above 5 the value becomes unknown…

paste this into the developer tools template editor and see what it shows:

{% if states('sensor.ups_tensione_di_ingresso') | float <= 5 %}
   {{ (as_timestamp(now()) | timestamp_custom(" %d/%m/%Y - %H:%M ")).lstrip("0") }}
{% else %}
  Sconosciuto
{% endif %}

When the sensor.ups_input_voltage sensor is below the value of 5, the template indicates the date and time.

When the sensor.ups_input_voltage takes on a value greater than 5, the template indicates “sconosciuto”

That’s what it should do.

I’m not sure what the reason is it’s saying unknown as a sensor.

is that just a translation of the sensor used in the template or is that really the sensor entity_id?

Sconosciuto = unknow (italian)
The sensor assumes the unknown / unknow value because the value sensor.ups_tensione_di_ingresso is above 5. I don’t want the unknown / unknow value, I would like it to undermine the timestamp value it assumes when the sensor sensor.ups_tensione_di_ingresso is below the value of 5

Then it’s working as expected.

I’m not sure what the question is?

In the template sensor I would like to see only the timestamp, I never want to see other states (for example I don’t want to see unknow)

try this:

{% if states('sensor.ups_tensione_di_ingresso') | float <= 5 %}
   {{ (as_timestamp(now()) | timestamp_custom(" %d/%m/%Y - %H:%M ")).lstrip("0") }}
{% else %}
  {{ states('sensor.ultimo_blackout') }}
{% endif %}

also I’m not really sure what the “.lstrip(“0”)” is supposed to be doing. So you might be able to leave that off.

Ok that’s almost perfect!

Now when the sensor ups is below the value 5, the template sets the timestamp in the sensor.

The only “inconvenience” is that every minute that passes with the value below 5, the timestamp changes the value by increasing by 1 minute.

Is it possible to set a fixed timestamp only when the ups sensor drops below the value 5 and not every minute in which the ups sensor remains below the value 5?

use a triggered template sensor instead of just a simple template sensor. make the trigger be the value going below 5. then the new sensor will only ever update when the value goes below 5.

then you can get rid of the if-else template structure entirely.

i’m not very sure

- template:
    - trigger:
        - platform: state
             entity_id: 'sensor.ups_tensione_di_ingresso'
             below: '5'
          sensor:
            - name: "Ultimo Blackout"
              unique_id: "ultimo_blackout"
              icon: mdi:flash-alert
              state: >
                 {{ (as_timestamp(now()) |   timestamp_custom(" %d/%m/%Y - %H:%M ")) }}

I think this would do it:

  template:
    - trigger:
        - platform: numeric_state
          entity_id: 'sensor.ups_tensione_di_ingresso'
          below: '5'
      sensor:
        - name: "Ultimo Blackout"
          unique_id: "ultimo_blackout"
          icon: mdi:flash-alert
          state: >
             {{ (as_timestamp(now()) |   timestamp_custom(" %d/%m/%Y - %H:%M ")) }}
1 Like