Last-changed value of a sensor

yah, that looks like it would work. the min() func from python isnā€™t in jinja but the min filter is.

still unkownā€¦ with both optionsā€¦


  - platform: template
    sensors:
      hygrothermo_1_lastchanged:
        value_template: >
          {% set values = [
            as_timestamp(states.sensor.hygrothermo_1_temperature.last_changed),
            as_timestamp(states.sensor.hygrothermo_1_humidity.last_changed),
            as_timestamp(states.sensor.hygrothermo_1_battery.last_changed), ] %}
          {{ values | min }}
        device_class: timestamp

image

try adding | timestamp_local to the end. It may require ()

  - platform: template
    sensors:
      hygrothermo_1_lastchanged:
        value_template: >
          {% set values = [
            states.sensor.hygrothermo_1_temperature.last_changed,
            states.sensor.hygrothermo_1_humidity.last_changed,
            states.sensor.hygrothermo_1_battery.last_changed, ] %}
          {{ values | min }}
        device_class: timestamp

Tested in Template Editor:

When implemented as a Template Sensor:

Screenshot%20from%202019-07-05%2008-25-59

as long as itā€™s a date time object. as_timestamp converts it to a float and will always work even if last_changed is the date time as a string. I.E. through restart.

Nuts! I keep forgetting that (even when I have no reason to forget it). Hopefully pnbrucknerā€™s correction is included in 0.96 to make this bug disappear into the dustbin of history.

Hold on ā€¦ thatā€™s for last_triggered whereas this is last_changed. Is this also affected by the deserialization bug?


EDIT
I donā€™t think the deserialization bug affects last_changed. The example I posted above was produced on my test system which I just turned on this morning. The last_changed for all three entities is a datetime object so the restoration feature performed the deserialization correctly.

Yah, itā€™s annoying. Iā€™ll be happy when we can just deal with datetimes everywhere. So we never have to convert

Yep, itā€™s safe to assume that a string will go in there without tests.

Hhm, seems not so easy to figure out, when a sensor with multiple values has last submitted his valueā€¦

From my point of view, this should be easier than creating a complicate template for each device

if this is a single device, chances are that they all update at the same time. You are trying to get last changed. Itā€™s a bit different because last_changed tells you the last value change. I donā€™t even know why youā€™d want last_changed for 3 different sensor values. To me, that value would be pointless.

Maybe sensor is missleading.
I would like to know the last value submitted of a device having 3, 5 or six sensors included.
In my case the sensors are Xiaomi weather sensors, one via Bluetooth and the others via zigbee2mqtt.


The three devices on the right side are connected via the same zigbee2mqtt

did you try @123ā€™s template? it should work. The one I provided with the | timestamp_local() should also work.

Have tried to put | timestamp_local() at the end, but wasnā€™t sure, what you meant.
Tried {{ values | min }} | timestamp_local and device_class: timestamp | timestamp_local in the template editor and both hasnā€™t changed the displayed result.

The screenshot? Did not realize, that that should do the sameā€¦

  - platform: template
    {% set a = states.sensor.hygrothermo_1_temperature.last_changed %}
    {% set b = states.sensor.hygrothermo_1_humidity.last_changed %}
    {% set c = states.sensor.hygrothermo_1_battery.last_changed %}
     
    a: {{a}}
    b: {{b}}
    c: {{c}}
    {{ c < b < a }}
    {% set values = [ a, b, c] %}
    {{ values | min }}

Shows following result in template editor, which is the same as your template, but with this HA does not restartā€¦

- platform: template
    
    
    
     
    a: 2019-07-06 06:36:13.009130+00:00
    b: 2019-07-06 06:53:30.013765+00:00
    c: 2019-07-05 14:42:51.066747+00:00
    False
    
    2019-07-05 14:42:51.066747+00:00

filters always go inside the jinja templates.
so this:

 - platform: template
    sensors:
      hygrothermo_1_lastchanged:
        value_template: >
          {% set values = [
            as_timestamp(states.sensor.hygrothermo_1_temperature.last_changed),
            as_timestamp(states.sensor.hygrothermo_1_humidity.last_changed),
            as_timestamp(states.sensor.hygrothermo_1_battery.last_changed), ] %}
          {{ values | min | timestamp_local() }}
        device_class: timestamp

or

  - platform: template
    sensors:
      hygrothermo_1_lastchanged:
        value_template: >
          {% set values = [
            states.sensor.hygrothermo_1_temperature.last_changed,
            states.sensor.hygrothermo_1_humidity.last_changed,
            states.sensor.hygrothermo_1_battery.last_changed, ] %}
          {{ values | min }}
        device_class: timestamp

should work.

ā€¦ because ā€œthisā€ (what was shown in the screenshot) was exclusively for use in the Template Editor (it was for demonstration purposes only).

If you added it to your configuration, it will definitely fail because itā€™s not a valid sensor definition, it was for use within the Template Editor only.

What I offered for use within your configuration was the first thing shown before the screenshot, namely this:

  - platform: template
    sensors:
      hygrothermo_1_lastchanged:
        value_template: >
          {% set values = [
            states.sensor.hygrothermo_1_temperature.last_changed,
            states.sensor.hygrothermo_1_humidity.last_changed,
            states.sensor.hygrothermo_1_battery.last_changed, ] %}
          {{ values | min }}
        device_class: timestamp
2 Likes

Result shown on states view:
First does not work: 1562490691.062787
Second and third (which are the same?) do work :grinning:
BUT: To get the last update, it has to be {{ values | max }}.

  - platform: template
    sensors:
      hygrothermo_1_lastchanged:
        value_template: >
          {% set values = [
            states.sensor.hygrothermo_1_temperature.last_changed,
            states.sensor.hygrothermo_1_humidity.last_changed,
            states.sensor.hygrothermo_1_battery.last_changed, ] %}
          {{ values | min }}
        device_class: timestamp

How would I post a sensor? Thatā€™s the template to correct the value:


  - platform: template
    sensors:
      corr_hygrothermo_1_humidity:
        friendly_name: "corr. Hygrothermo 1 Humidity"
        unit_of_measurement: '%'
        value_template: "{{ states('sensor.hygrothermo_1_humidity') | float + 0.5 }}"

I guess there was a misinterpretation of the word ā€˜lastā€™. Given three sensors, you want the newest datetime which will be found using the max filter. The min filter will select the oldest datetime.

Glad to hear itā€™s working for you now.

I have an additional question: I would like to know the maximum value of a some last_changed values. Some of my xiaomi low cost sensors need a manual button press from time to timeā€¦

Itā€™s not as easy as above because a sensor has multiple values and i.e. the battery value is submitted only once a day, but the temperature value multiple times an hour. My idea is to reuse the lastchanged from above and get the maximum value of all my lastchanged sensors.
Here an example of a lastchanged sensor template of one of my sensors

  - platform: template
    sensors:
      xiaomiaqara_1_lastchanged:
        value_template: >
          {% set values = [
            states.sensor.xiaomiaqara_1_temperature.last_changed,
            states.sensor.xiaomiaqara_1_humidity.last_changed,
            states.sensor.xiaomiaqara_1_pressure.last_changed,
            states.sensor.xiaomiaqara_1_linkquality.last_changed,
            states.sensor.xiaomiaqara_1_voltage.last_changed,
            states.sensor.xiaomiaqara_1_battery.last_changed, ] %}
          {{ values | max }}
        device_class: timestamp

This sensor results in a value like: 2020-02-03 10:59:24.090187+00:00
What I would like to have is the oldest last changed. My idea was to reuse the stuff from above, but that does not work :roll_eyes:

  - platform: template
    sensors:
      xiaomi_lastchanged:
        value_template: >
          {% set values = [
            states.sensor.hygrothermo_1_last_changed,
            states.sensor.xiaomismart_1_last_changed,
            states.sensor.xiaomiaqara_1_last_changed,
            states.sensor.xiaomiaqara_2_last_changed, ] %}
          {{ values | min }}
        device_class: timestamp

The result is unavailableā€¦ Same result without the states prefixā€¦

Could I please get some help with this super simple question;

  • Sensor
    sensor.vardagsrum_motion

I want to evaluate by using a if statement the following
If
sensor.vardagsrum_motion.last_changed to ā€œonā€ < 120 seconds
<>
else
<>
endif

but I cant find the answerā€¦
all help appreciated.

Hello folks, I have a workaround which can be used for your case.
You can create a helper (Configuration ā†’ Helpers) to combine states of more sensors, however this time you will use just your 1 entiry without adding another one with statistic characteristic ā€œmost recently updatedā€.

That will basically do the trick and will create a helper which will store the last updated value. It works for me in all historical graphs too. Iā€™m using it for humidity & temperature sensors which are usually idle (or unavailable) and then just pop up now and then.
Hope it helps.

1 Like