I have a PIR sensor I intend on placing in a Cat Kennel… I’d like to be able to see the last time this sensor triggered to on/detected so I can get a good idea on if the cats in the kennel or not.
I’m feeling really stupid right now, i cannot figure out how best to do this. It seems pretty simple comparing to some of my other config.
I’ve been trying to figure out the same thing. The PIR sensors I have don’t have an attribute of last_changed or last_updated. However it’s in the front end. I want to use it for my garage door to close after X minutes of no activity.
Modify as need, but this works for me with my motion sensor. The “input_boolean.garage_changed_with_ha” is something I use to diferenate between manually opening and opening via automation
- alias: 'Garage Door Close with No Motion for Three Minutes'
trigger:
- platform: state
entity_id: input_boolean.garage_changed_with_ha
from: 'off'
to: 'on'
for:
minutes: 3
- platform: state
entity_id: binary_sensor.motion_sensor_garage_motion
from: 'on'
to: 'off'
for:
minutes: 3
condition:
- condition: state
entity_id: binary_sensor.motion_sensor_garage_motion
state: 'off'
for:
minutes: 2
- condition: state
entity_id: input_boolean.garage_changed_with_ha
state: 'on'
action:
- service: cover.close_cover
entity_id: cover.garage_door_opener
- service: homeassistant.turn_off
entity_id: input_boolean.garage_changed_with_ha
My Z-Wave sensors don’t have last_changed or last_updated attributes, so I’m unable to pull this into a template. I’m still unsure on how to achieve this without getting really hacky for a seemingly simple sensor.
I believe those attributes are part of HA itself. I’m not positive but pretty sure. It’s not coming from the device itself. Have you tried it yet?
If necessary I guess you could create an input Boolean tie that Boolean to your motion sensor on/off using a template and then use that for your automations.
Thank you for the tip @ptdalen indeed they are coming from Home Assistant. Anybody who find this thread and wants similar below is the config I ended up with. Seems to be working great so far.
- platform: template
sensors:
cat_kennel_last_motion:
value_template: >-
{% set today = now().day | int %}
{% set cat_date = ((as_timestamp(states.sensor.cat_kennel_motion.last_changed) | timestamp_custom("%d")) | int) %}
{% if today == cat_date %}
{% set day = "Today" %}
{% elif (today - 1) == cat_date %}
{% set day = "Yesterday" %}
{% elif today == 1 and cat_date > 27 %}
{% set day = "Yesterday" %}
{% else %}
{% set day = as_timestamp(states.sensor.cat_kennel_motion.last_changed) | timestamp_custom("%A")%}
{% endif %}
{% set time = as_timestamp(states.sensor.cat_kennel_motion.last_changed) | timestamp_custom("%l:%M %p")%}
{{[day,time]|join(" at ")}}
entity_id:
- sensor.cat_kennel_motion
friendly_name: 'Cat Kennel Last Motion'
As part of an icon template… Basically, I have a crappy z-wave smoke alarm that doesn’t have a test mode. I have 60 seconds to test the alarm when I turn on this switch, I can test the alarm by pressing the physical test button on it and it won’t trigger all my other automations. The switch automatically turns off after 120 seconds. All of that part is working fine I’m just a bit stumped with the icon template. What am I doing wrong?
What is happening with the icon? I’m going to guess that it “works”, but never changes. Just a quick thought, you need to have <= 15, and then > 16 <=30, etc.
Not an icon template, but I have this for a sensor
{% if states("sensor.time_to_work") | int >= 32 %}
Heavy
{% elif states("sensor.time_to_work") | int < 32 and states("sensor.time_to_work") | int >= 27 %}
Moderate
{% elif states("sensor.time_to_work") | int < 27 and states("sensor.time_to_work") | int >= 2 %}
Normal
{% else %}
You're Here
{% endif %}
Hi thanks for your help yep at the moment the icon doesn’t change.
I’ve tried this as you suggested - now it updates to “mdi:circle-slice-1” when the switch turns on - and back to “mdi:access-point” when the switch turns off, either manually or automatically. But it doesn’t change to “mdi:circle-slice-2, 3, 4 etc.” as time progresses.
Ok so I’ve tested this and low and behold it should work. If I toggle the switch to on, and paste this into Developer tools > Template and I hit return every few seconds - the output cycles through each of the different icons mdi:circle-slice-1, 2, 3 etc. and back to mdi:access-point when the switch is turned off after 60 seconds.
But for some reason - this isn’t what shows up in lovelace - it only gets as far as mdi:circle-slice-1 so something else is going on @frenck any ideas?
I’m still no expert with all these scripting and I’m a little confused at the moment. I hope you can help me a little bit. I try to deactivate an automation (the notification is only an example) for one hour if a switch (switch.wz_tv) was toggled. In the comments i have the single steps what i tried within the one line above.