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
Result shown on states view:
First does not work: 1562490691.062787
Second and third (which are the same?) do work
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
- 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.