So I’ve got a PIR motion sensors in each room of my home. I wanted a single sensor reading to show which PIR triggered last. This is the config yaml I kludged to make it work:
platform: template
sensors:
template_pir_motion:
friendly_name: 'Motion Location'
value_template: >
{% for pir in [states.binary_sensor.pir_living_room, states.binary_sensor.pir_office_room, states.binary_sensor.pir_bed_room] %}
{% if as_timestamp(pir.last_changed) == as_timestamp([states.binary_sensor.pir_living_room, states.binary_sensor.pir_office_room, states.binary_sensor.pir_bed_room] | map(attribute='last_changed') | max) %}
{{ pir.name }}
{% endif %}
{% endfor %}
I’m new to Jinja templating. Can anyone point out what might be done better?
It works well enough, but I’m thinking this might be better done with a custom component…
Here is a small enhancement you can do to prevent two duplicate lists of pirs:
platform: template
sensors:
template_pir_motion:
friendly_name: 'Motion Location'
value_template: >
{%- set pirs = [states.binary_sensor.pir_living_room, states.binary_sensor.pir_office_room, states.binary_sensor.pir_bed_room] %}
{% for pir in pirs %}
{% if as_timestamp(pir.last_changed) == as_timestamp(pirs | map(attribute='last_changed') | max) %}
{{ pir.name }}
{% endif %}
{% endfor %}
You can probably also loop trough all states available and extract any values that contain pir_ to have it always up-to-date if you add more PIR’s to your home.
Ah, excellent note. I was so confounded with not being able to persist variables set in the for block scope… didn’t think to pull the list out.
Yeah, I guess that wouldn’t be too unreasonable to loop through all the states. I made a group of them, hoping I could iterate through just the group, but haven’t seen a way to do that yet.
I’ve done this by using an input_slider basically each time a PIR is triggered I set the input_slider to a preset value. 1 for bedroom, 2 for living room and so on, but this method looks much cleaner. Nice work!
No straightforward way so far as I can see, but it can indeed all be done through a combination of sensor configs / value_templates. As you can see in my config above, you can pull the sensor state’s last change from the state object itself (docs on that here: https://home-assistant.io/docs/configuration/state_object/)
Thanks, has been working well except in the situation where I move quickly from one room to a an adjacent room. What happens is: the room I walk to updates my template_pir_motion field before the room I walked from switches off, which then becomes the “last_changed” state when it receives that off message…
suppose I could only have the sensors report when the motion is first seen… I just wouldn’t get an on/off indication.
Or as I’ve been thinking in the back of my mind all along, this may be a situation where it’s better to write a custom component. Been fun to learn about the templating and see how far I could get with just them alone.
My way of ignoring the off messages is to have an automation for each binary sensor that triggers when the motion sensor triggers to the on state. That then checks which motion sensor was triggered and sets an input_slider to a predefined integer value for the specific motion sensor that was triggered.
Then another automation gets called when the input_slider moves from one integer to another and that is what triggers automations to turn on/off lights depending on my location.
automation:
- alias: Store Motion Sensor 1
trigger:
- platform: state
entity_id: sensor.sn1_pir
from: 'standby'
to: 'motion detected'
action:
service: input_select.select_option
data:
entity_id: input_select.last_room_with_motion
option: Livingroom
- alias: Store Motion Sensor 2
trigger:
- platform: state
entity_id: sensor.sn2_pir
from: 'standby'
to: 'motion detected'
action:
service: input_select.select_option
data:
entity_id: input_select.last_room_with_motion
option: Bedroom
There’s another way to do this by logging timestamps of motion detectors and comparing those, though it’s more complicated and I don’t know how to up-scale past 2 sensors with that method. (this method can up-scale though)
Now I’m looking for info to transform the timestamp ( 2018-05-22 16:19:45.654266+00:00 ) to
22-05 18:19:45
DD-MM HH-MM-SS (time in UTC+2 (summertime for UCT+1)).
Sorry for the n00b question, I want to show the value of the input-select, but without the controls, so that it just looks like: Lost motion triggered and the value the input-select value.
I can show the input-select on the frontpage, but still can change the input.
Hope somebody can show me in the direction, just started using HASS.io.