Activation time sensor

Hi.
I would need to create a sensor that tells me the activation time / date of a switch.
I can’t use the last-changed information, because it resets when Home Assistant is restarted.
Eg. switch.water_pump → 14:30 02/07/22

Can you help me? Thanks

Read this, you should find an answer:

I have not found simple solutions that I can do

The easiest solution I have in my mind, if you have only a few entities, is to write an automation per entity:

The trigger will be any state change of your entity
The action will be to store the current date/time in an entity (type: input_datetime)

If you are advanced in automation and template, you can even create an automation with, as trigger, a state change of all your your entities you want to track and create an action based on the entity name having triggered… Not easy but feasible I think…

Let me know if I should develop this idea…

I have tried this method, but I can’t get it to work

https://community.home-assistant.io/t/real-state-last-changed/15928/9

Create an Input Datetime, that contains date and time, called input_datetime.water_pump

Create the following automation. Each time the switch is turned on, the current date and time is saved in the Input Datetime. The Input Datetime’s value is unaffected by restarts.

alias: example
trigger:
- platform: state
  entity_id: switch.water_pump
  from: 'off'
  to: 'on'
condition: []
action:
- service: input_datetime.set_datetime
  target:
    entity_id: input_datetime.water_pump
  data:
    timestamp: '{{ now().timestamp() }}'
2 Likes

This works correctly.
But how do I insert this information as a template into a message?
e.g. Last Activation: {{info}}

The usual way, by using the states() function to get the entity’s value.

{{ states('input_datetime.water_pump') }}

but how can I change the message format?

Now I see yyyy-mm-dd hh:mm:ss, how can i display hh:mm dd/mm?

Thanks

Use the timestamp_custom() function to convert it to whatever format you prefer.

For example:

{{ state_attr('input_datetime.water_pump', 'timestamp') | timestamp_custom('%H:%M %d/%m') }}

Here is the automation (just tested for one entity but should work for more than one) with template for multiple entities in one automation:
Pre-requisit: create the input_datetime entities with the names associated with the entites you want to track.
example:

  1. your entity is “switch.chargeur_garage”
  2. you have to create an entity called (in my example but you can change the naming convention by editing the automation): “input_datetime.switch_chargeur_garage_changed”

Here is the automation:

- alias: record_changed_date_time
  initial_state: true
  trigger:
    - platform: state
      entity_id: 
        - switch.chargeur_garage
        - switch.test
        - switch.cuisine
  variables:
    entity: >-
            {% set string=trigger.entity_id %}
            {% set string=string.split('.') %}
            {% set result="input_datetime."+string[0]+"_"+string[1]+"_changed" %}
            {{ result }}
  action:
    - service: input_datetime.set_datetime
      target:
        entity_id: "{{ entity }}"
      data:
        timestamp: '{{ now().timestamp() }}'
1 Like

Thank you very much you did great, now everything is perfect!

1 Like

Thanks, if I need it I will also use your automation (for now I only needed it for one entity)

1 Like

Suggestion:

If you want the automation to handle multiple entities, it makes it easier if each Input Datetime has the same object_id as the entity it represents.

For example:

switch.water_pump
input_datetime.water_pump

Both entities share the same object_id (water_pump).

The automation’s service call is simply this:

  action:
    - service: input_datetime.set_datetime
      target:
        entity_id: 'input_datetime.{{ trigger.to_state.object_id }}'
      data:
        timestamp: '{{ now().timestamp() }}'

But if the Input Datetime must end with “_changed” then it’s easy to accomodate in the template, provided the rest of the string is common to both entities.

        entity_id: 'input_datetime.{{ trigger.to_state.object_id }}_changed'
2 Likes

Yours is great and simpler but does not allow to have entities from different types (switch, sensor, …) with the same object_id… I am too complicated I know… :grin:

It does because the entity’s domain (switch, binary_sensor, etc) plays no part in the associated Input Datetime’s name. Only the object_id is used.

domain.object_id
       ^^^^^^^^^

switch.water_pump
binary_sensor.garage_door
sensor.temperature_outside

input_datetime.water_pump
input_datetime.garage_door
input_datetime.temperature_outside

I think they meant that if you have both a switch and a binary sensor for a water pump, then your solution won’t cater for both, since the more complex solution encodes the monitored entity’s domain in the object ID. Granted, the change values for the two in this case would be the same if one assumes it’s for the same pump and this automation gets triggered on both (don’t see why one would do that though). Personally I’d stick to the simpler version and change the object IDs if not referring to the same device to remove any ambiguity.

I agree with you, nevertheless it was a good exercise to use variables and templates and create something very flexible… :grinning:

1 Like