Template: last_changed to specific state?

Example:

  The lamp changed state {{ relative_time(states.light.lamp.last_changed) }} ago .

But this will show last_changed, how can I show the last time it has changed to something specific? (just on, or jus off ect)

You need an automation or a sensor to do that.

I use the following trigger based template sensor for my lounge lighting:


template:
  - trigger:
      - platform: state
        entity_id: light.lounge
        from: 'off'
        to: 'on'
    sensor:
      - name: "The lounge lighting was last turned on on…"
        state: "{{ (as_timestamp(states.light.lounge.last_changed)) | timestamp_custom('%d.%m.%Y at %R', now() ) }}"

EDIT: corrected typo

1 Like

how do you create this? do i create a date/time helper called states.light.lounge then create an automation with those codes?

You don’t need a helper for it.
last_changed is an entity attribute (compare Developer Tools —> States). You are able to access it by using states.light.lounge.last_changed .

sorry, I see has been a long time but I am in a deep search for templating time

how do you write down this template to a helper? I can find how…?

It’s a trigger based template sensor, you can only add those to your configuration using YAML.
Alternative would be to create an automation with the same trigger and write it to an input_text (or just the date and time to an input_datetime).

ok, thank you.

is it doable, teo get the time of a specific change into a conventional template?

i am templating a sensor like this

{{ as_datetime(states.switch.ladron_pasillo_0_enchufe_1.last_changed) }} 

but i want only when it changes to on

Well, not if you want it to survive HA restarts. With a normal sensor (helper) you will loose the state after a restart of HA.

BTW, no need to use as_datetime() on that, it’s already a datetime.

I am just trying to get that time to use it in the styling of an attribute, I supose it is not important if it is lost at a restart, isn´t it?

Anyway, how can I aproach this, then?

BTW, no need to use as_datetime() on that, it’s already a datetime.

as you can see I know almost nothing of programming :grinning:

What if your light was turned on in the morning, and after that you will restart HA. You will loose that information then, and won’t know the last time it was turned on anymore.
Depending on the state of the light when HA restarts the new state of your sensor will then either be the time HA started, or unknown

I’m not sure how important this information is for you.

Anyway, this will do what I described above:

{% set entity = 'switch.ladron_pasillo_0_enchufe_1' %}
{% set current = as_datetime(this.state, default=none) %}
{{ states[entity].last_changed if is_state(entity, 'on') else current }}

But I would advice to use the trigger based approach and create the template sensor in YAML, or if you really don’t want to use YAML use the automation. If you really want a sensor and not an input_datetime, you can create a sensor using the state of the input_datetime.

# automation
triggers:
  - trigger: state
    entity_id: switch.ladron_pasillo_0_enchufe_1
    from: "off"
    to: "on"
actions:
  - action: input_datetime.set_datetime
    target:
      entity_id: input_datetime.last_time_turned_on
    data:
      datetime: "{{ trigger.to_state.last_changed }}"

template for sensor helper
{{ states('input_datetime.last_time_turned_on') }}

as I say, I am in the process of learning things here.

The purpose of me to know the time this state changed from off to on is merely cosmethic, as I am using it to do a ping effect on the icon on a card, actually i will be using it on a presence sensor, though I am now using a light because you can´t fake the state of the presence sensor, at least I am unable to. Thast said my intention is to ping with a frequency the first 2 seconds, another between 2-4 and so on; and this is how I got into this trouble hehehehehheh

what I mean, is that is not very important for me, anyway, maybe doing right from the start is the best practice, as experience tells, so I am diving in the seconf part of your answer.

I will come back later,

thank you very very much

no need to state wich entity ?

I am sorry but really very confused, the template sensor screen offers so many fields to fill up

Hi again really don´t exactly know wich is best, so to say that I have come to this

this is the code

{{ as_datetime(is_state('switch.ladron_pasillo_0_enchufe_1', 'on')) }}

and seems to be giving me what I want, well it exceeds my requirements, in fact gives a weird date on 1970 as you can see, why is that?

Anyway, as I am only conunting some seconds from every reading, it solves my problem so far.
these are the readings

please give your opinion about it.

that should be datetime: "{{ ...

1 Like

You seem to me mixing stuff up.

Option 1:

Use this template in a template helper

{% set entity = 'switch.ladron_pasillo_0_enchufe_1' %}
{% set current = as_datetime(this.state, default=none) %}
{{ states[entity].last_changed if is_state(entity, 'on') else current }}

In the template helper GUI you can give it the device class timestamp

Advantages:

  • easy to setup

Disadvantages:

  • not accurate after a restart of HA

Option 2

Use a trigger based template sensor in YAML. Edit your configuration.yaml and add the following YAML:

template:
  - trigger:
      - platform: state
        entity_id: switch.ladron_pasillo_0_enchufe_1
        from: 'off'
        to: 'on'
    sensor:
      - unique_id: template_sensor_last_turned_on
        name: "The switch was last turned on…"
        state: "{{ trigger.to_state.last_changed  }}"
        device_class: timestamp

Advantages:

  • Only one entity required
  • also accurate after a restart of HA

Disadvantages:

  • requires YAML setup in configuration.yaml

Option 3

Create an input_datetime helper and an automation to update the helper. Requires a separate input_datetime for each entity you want to track. Set the input datetime helper to date and time
For the example below the input datetime with entity id input_datetime.last_time_turned_on needs to be created. That input datetime will then track the time this specific switch has been turned on.

# automation
triggers:
  - trigger: state
    entity_id: switch.ladron_pasillo_0_enchufe_1
    from: "off"
    to: "on"
actions:
  - action: input_datetime.set_datetime
    target:
      entity_id: input_datetime.last_time_turned_on
    data:
      datetime: "{{ trigger.to_state.last_changed }}"

Advantages:

  • No changes to configuration.yaml required
  • Can be created in the automations editor
  • also accurate after a HA restart

Disadvantages

  • requires an automation and a helper
  • will have the date and time in an input_datetime helper, not a sensor

Optional extra sensor for option 3

With option 1 and 2, the date and time will be stored in a sensor, with option 3 this will be an input_datetime. If you want the date and time stored in a sensor, you can create a template helper sensor which copies the state from that input_datetime helper using this template:
{{ states('input_datetime.last_time_turned_on') }}
In the template helper GUI you can give it the device class timestamp

Advantages:

  • Everything from option 3

Disadvantages

  • requires an automation, an input_datetime helper and a template helper

also input_datetime.set_datetime instead of input.datetine.set_value :slight_smile:
I’ve updated my posts which had the wrong action and key

1 Like

This converts True to a datetime (where then 1 is used to represent true)
So it converts the timestamp 1 to a datetime, meaning the result is 1st of January at 00:00:01 UTC (1 second after 1st of January 1970, which is timestamp 0)

1 Like

well, I am sticking with option one, and gonna test how to introduce it into a card. let´s see what happens, don´t discard me back :upside_down_face: with upcoming questions.

That´s nice, one lessons learned, so when it dettects a on state, as true, it outputs 00.00.01 01.01.1970.

is_state('some.entity', 'some_state') returns true or false no other options. Even when the entity doesn’t exist it will return false

as_datetime() converts a value to a datetime object. In case the input value is numeric, it will treat it as a timestamp. In case the input value is a boolean, it will treat it as the numeric counterpart of that boolean value (so true as 1, false as 0).

so as_timestamp(is_state('some.entity', 'some_state')) will either return 1970-01-01 00:00:01+00:00 (for true/1) or 1970-01-01 00:00:00+00:00 (for false/0)

1 Like

understood, thank you