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.
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.
EDIT: I always forget, we now have trigger based template sensors, that should work even better!
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. So don’t be mad, if it doesn’t work from the beginning.
template:
- trigger:
- platform: state
entity_id: switch.tuya_f_socket_1
to: 'on'
sensor:
- name: your_timestamp_on
state: "{{ now() }}"
My code below for determining when my Back Garden Irrigation Valve was last on.
# Template Time Since Last ON Back Garden
template:
- trigger:
- platform: state
entity_id: switch.0xa4c13839477a051c_state
to: 'on'
sensor:
- name: back_garden_valve_last_on
state: "{{ now().timestamp() | timestamp_custom('%H:%M %a %b %d') }} "