I could use some guidance on trying to make a sensor to display the earliest time each day that a binary sensor was tripped. I don’t need the most recent but rather when it was first tripped in the current day. I initially thought about doing a SQL query on sqllite but I kept getting malformed data errors (even when I copied and pasted the example code from the HA page). If I need to move HA to mariadb, I suppose I could do that if this was the best path forward.
I’m using Node Red, so I thought about the get history node, but it ends up producing 35 array items. There may be a way to programmatically go through them to determine the first “on” state from today, but I have yet to figure that out. I also can’t seem to find a pattern of the first or last array item being the earliest as they often capture previous day readings.
If you have done this, I’d love some guidance. I may be overengineering this, so I am hoping someone can push me in the right direction.
template:
- trigger:
- platform: state
entity_id: binary_sensor.your_sensor_here
to: 'on'
sensor:
- name: Sensor Daily First On Time
state: >
{% if has_value(this.state) %}
{% if states.binary_sensor.your_sensor_here.last_changed.day > this.state.last_changed.day %}
{{ states.binary_sensor.your_sensor_here.last_changed }}
{% else %}
{{ this.state }}
{% endif %}
{% else %}
{{ states.binary_sensor.your_sensor_here.last_changed }}
{% endif %}
This is untested. And the for the first day it will have the date and time of the last time the door was opened. The following day it should be correct. Let me know how it goes.
I’ve viewed it this morning, and it is showing the most current timestamp, but not the earliest in the day.
A related question: does this have anything to do with timestamps being in UTC? Likely not, but wanted to confirm. If it’s looking at a current day, and I’m five hours behind UTC time (east coast in US), would that affect the template?
has_value needs an entity_id as input, not a state.
Based on your input I came to this.
template:
- trigger:
- platform: state
entity_id: binary_sensor.your_sensor_here
to: 'on'
sensor:
- name: Sensor Daily First On Time
state: >
{% set opened = states[trigger.entity_id].last_changed %}
{% if as_datetime(this.state) is not none and as_datetime(this.state).date() != opened.date() %}
{{ opened }}
{% else %}
{{ this.state }}
{% endif %}
device_class: timestamp
I’m getting the following error in the logs: sensor.great_room_sensor_daily_first_on_time rendered invalid timestamp: unknown
I didn’t make the same mistake as above, and I triggered the great_room_motion_sensor after the restart, but I’m still seeing an unknown state. My code is as follows just in case I’ve screwed something up in the transfer over:
template:
- trigger:
- platform: state
entity_id: binary_sensor.great_room_motion_sensor
to: 'on'
sensor:
- name: Great Room Sensor Daily First On Time
state: >
{% set opened = states[trigger.entity_id].last_changed %}
{% if as_datetime(this.state) is not none and as_datetime(this.state).date() != opened.date() %}
{{ opened }}
{% else %}
{{ this.state }}
{% endif %}
device_class: timestamp
Sorry, my bad. I shouldn’t have included not in the 2nd line.
Should have been
template:
- trigger:
- platform: state
entity_id: binary_sensor.your_sensor_here
to: 'on'
sensor:
- name: Sensor Daily First On Time
state: >
{% set opened = states[trigger.entity_id].last_changed %}
{% if as_datetime(this.state) is none or as_datetime(this.state).date() != opened.date() %}
{{ opened }}
{% else %}
{{ this.state }}
{% endif %}
device_class: timestamp
That worked in that it captured the first trigger of the motion sensor, and it has kept that same entry even when it was tripped multiple times. Thank you so much for the help as this was beyond my capability.
If I was looking for the time of day it was first triggered (rather than number of minutes or hours ago), would I pipe ( | ) an as_local in the line where the opened variable is set?
But he wants a time string like "07:45" as the state, not a full datetime (forgot to remove the state_class from my last version, removed it now)
You could use today_at() on the state and compare it to now() but that could cause to false negatives of the door is opened later than the time it was opened first the day before. So you need the date as well