Question about templating

Hi, I’m looking for help with creating a template for entity picture in a template sensor. What I want is a sensor for an alarm. Which can show three stages. 1. Motion. 2 Post motion, which is between the motion and smaller then 10s from last changed. And 3. no motion when the time is bigger then 10s since last_changed. I made this template:

platform: template
sensors:
  pir_woonkamer:
  friendly_name: "Woonkamer"
  value_template: >-
    {% if is_state('binary_sensor.pir_woonkamer', 'on') %}
      beweging
    {% else %}
      geen
    {% endif %}
  entity_picture_template: >-
    {% if is_state('binary_sensor.pir_woonkamer', 'on') %}
      /local/icons/motion.png
    {% elif as_timestamp(now()) - as_timestamp(states.binary_sensor.pir_woonkamer.last_changed) < 10  %}
      /local/icons/post-motion.png
    {% else %}
      /local/icons/no-motion.png
    {% endif %}

The value part works and I do not have questions about that. The entity_picture template works partly and I could use some help to get it 100% working.

  1. When there is motion the picture motion.png is shown. So far so good!
  2. When the now()-last_changed from the sensor < 10 seconds it shows post-motion. This also works.
  3. But the else statement with the picture no-motion never shows. When the time after the last_changed is bigger the 10s, the picture post-motion stays.

If someone could help, that would be great!

Add the following entity ids to your template to tell it when to update (assuming you have already defined a time sensor):

platform: template
sensors:
  pir_woonkamer:
  friendly_name: "Woonkamer"
  entity_id:
    - binary_sensor.pir_woonkamer
    - sensor.time # required to update now()
  value_template: >-
    {% if is_state('binary_sensor.pir_woonkamer', 'on') %}
      beweging
    {% else %}
      geen
    {% endif %}
  entity_picture_template: >-
    {% if is_state('binary_sensor.pir_woonkamer', 'on') %}
      /local/icons/motion.png
    {% elif as_timestamp(now()) - as_timestamp(states.binary_sensor.pir_woonkamer.last_changed) < 10  %}
      /local/icons/post-motion.png
    {% else %}
      /local/icons/no-motion.png
    {% endif %}

Note the time sensor will only update the template every minute. So it will only change to your no-motion picture after a minute.

That works! Thankyou very much! Happy here :slight_smile:

That’s exactly what we need to say and illustrate here as the current state is way from palatable, isn’t it? :wink:

1 Like