#Door sensor card show last closed and opened
1 messages · Page 1 of 1 (latest)
Have you tried the 'logbook card'? You can use that to show a past amount of hours
You can maybe set up a couple helper templates (ex. for two states). Then you can create an automation for your door sensor to 'track' the last two states using those helpers. Then just display them using a markdown card. 🤷♂️
I don't think the logbook card will give the overall function of what the OP is looking for. There could be other states that pop up on the logbook (maybe the sensor goes unavailable for a moment or it reports its battery status.)
I think the best approach would be to make a template sensor. Template
The doc says it can be created through the UI, but I made one of mine by adding it to the configuration.yaml. In this example, I wanted to create a problem sensor that "watched" for various issues. The state will return either OK or Problem and then the attributes can show where the problem is. This has become useful in automations. If the problem sensor changes to Problem, then my hallway light turns red. The automation will send a notification to my phone based on which attribute has the issue.
UPDATE: This example is not completely correct. The OP needs a triggered template sensor.
UPDATE2: Triggered template binary sensor added below.
template:
- sensor:
- name: problem
unique_id: problem
device_class: enum
state: "{{ 'OK' if this.attributes.values()|reject('eq', 'OK')|list|length == 4 else 'Problem' }}"
icon: "{{ 'mdi:check' if this.attributes.values()|reject('eq', 'OK')|list|length == 4 else 'mdi:alert' }}"
attributes:
options: "{{ ['OK', 'Problem'] }}"
friendly_name: Problem Sensor
freezer: "{{ 'OK' if (states('sensor.freezer_temperature') | float < 30) else 'Freezer too warm.' }}"
refrigerator: "{{ 'OK' if (states('sensor.refrigerator_temperature') | float < 50) else 'Refrigerator too warm.' }}"
internet: "{{ 'OK' if (states('binary_sensor.internet' )== 'on') else 'Internet is down.' }}"
unraid: "{{ 'OK' if (states('binary_sensor.unraid') == 'on') else 'Unraid is down.' }}"
Note: There is a Problem Sensor that can be made through the UI, but, IIRC, this method gave me a bit more flexibility with how I wanted the sensor to actually function.
In configuration.yaml:
input_text:
last_state:
name: Last State
second_last_state:
name: Second Last Stateautomation:
- alias: Track Last Two States
trigger:
- platform: state
entity_id: sensor.your_entity_id
action:- service: input_text.set_value
target:
entity_id: input_text.second_last_state
data:
value: "{{ states('input_text.last_state') }}"- service: input_text.set_value
target:
entity_id: input_text.last_state
data:
value: "{{ trigger.to_state.state }}"
Then in a markdown card do:
type: markdown
content: |
Last State: {{ states('input_text.last_state') }}
Second Last State: {{ states('input_text.second_last_state') }}
this should do the trick.
This is what I came up with: ```yaml
template:
- trigger:
- platform: state
entity_id:- binary_sensor.living_room_door_on_off
not_to: - unavailable
- unknown
not_from: - unavailable
- unknown
binary_sensor:
- binary_sensor.living_room_door_on_off
- name: Door Tracker
unique_id: door_tracker
device_class: door
state: "{{ states(trigger.entity_id) }}"
attributes:
friendly_name: Door Tacker
last_opened: "{% if states(trigger.entity_id) == 'on' %} {{ trigger.to_state.last_changed.isoformat() }} {% else %} {{ this.attributes.last_opened }} {% endif %}"
last_closed: "{% if states(trigger.entity_id) == 'off' %} {{ trigger.to_state.last_changed.isoformat() }} {% else %} {{ this.attributes.last_closed }} {% endif %}"
- platform: state
type: markdown
content: >
## Living Room Door
Last opened: {{ as_timestamp(states.binary_sensor.door_tracker.attributes.last_opened) | timestamp_custom('%-I:%M %p') }}
Last closed: {{ as_timestamp(states.binary_sensor.door_tracker.attributes.last_closed) | timestamp_custom('%-I:%M %p') }}
{% if states('binary_sensor.door_tracker') == 'on' %}
The door is currently open.
{% else %}
The door was open for {{ (as_timestamp(states.binary_sensor.door_tracker.attributes.last_closed) - as_timestamp(states.binary_sensor.door_tracker.attributes.last_opened)) | timestamp_custom('%-S')}} seconds.
{% endif %}
```EDIT: Symplified the timestamp_custom. For the syntax, check out [Python strftime cheatsheet](<https://strftime.org/>).
EDIT2: Added how long the door was open. The Markdown card cannot do a real-time counter. When the door is open, the card will just say that it's open.