Sensor that displays the last time a switch was 'on'

Hey All, I just want to create a sensor that shows the time (12 hour?) the last time a switch was ‘on’. I searched and found History Stats but it doesn’t seem to work the way I thought.

I have this in my sensor page:

- platform: history_stats
  name: switch_a_last_on
  entity_id: switch.tuya_f_socket_1
  state: 'on'
  type: time
  end: "{{ now().replace(hour=0, minute=0, second=0) }}"
  duration:
    hours: 24

This is what shows:
image

This is the actual stats:
image

Does anyone know a better way that I can do this?

You could just setup a template sensor, that get’s changed by an automation, triggered by the state change of the original sensor to: on. The last update timestamp of your template sensor should be the time, the original sensor was turned ‘on’ the last time. Depending on your system, it might be off by at most a few seconds. :slight_smile:

EDIT: I always forget, we now have trigger based template sensors, that should work even better! :slight_smile:

EDIT2: Something like this, not tested and written without checking the documentation, and from the top of my head, I’m not savy yet with these new template sensors. :rofl: So don’t be mad, if it doesn’t work from the beginning. :rofl: :rofl:

template:
  - trigger:
      - platform: state
        entity_id: switch.tuya_f_socket_1
        to: 'on'
    sensor:
      - name: your_timestamp_on
        state: "{{ now() }}" 
1 Like

Sorry, @paddy0174 I said it didn’t work but it did. It just took a long time before it did for some reason after restarting.

It had a lot of data so I simplified it for my needs. My next step is to figure out how to put it on my sensor page.

This is the finished product if anyone stumbles on this in the future:
image

Configuration.yaml:

template:
  - trigger:
      - platform: state
        entity_id: switch.tuya_socket_1
        to: 'on'
    sensor:
      - name: pump_on
        state: "{{ now().timestamp() | timestamp_custom('%I:%M %p %a') }} " 
  - trigger:
      - platform: state
        entity_id: switch.tuya_socket_1
        to: 'off'
    sensor:
      - name: pump_off
        state: "{{ now().timestamp() | timestamp_custom('%I:%M %p %a') }} " 
1 Like

Yeah, that looks a little more sophisticated than my one :rofl:

Good you got it working! :slight_smile: :+1:

1 Like

Can you please help me? :grinning:

I have a switch which stays ON only for 1 seconds and then goes OFF automatically.

I need to calculate the time difference between “now” and “the time it went ON”. So I can write it as info on the cards. For example: “34 minutes ago”

I managed it with below code but every time HA restart timer is reset to HA start time

template:

  - trigger:

      - platform: state

        entity_id: switch.driveway

        to: 'on'

    sensor:

      - name: driveway_on

        state: "{{ now().timestamp() | timestamp_custom('%I:%M %p %a') }} "

Card info

{{ relative_time(states.sensor.light_on.last_changed) }}

Thanks.