Use last changed time in automation

Hi,
I finally managed to get the time for the last motion sensor trigger through this code

template:
  - trigger:
      - platform: state
        entity_id: binary_sensor.lumi_lumi_sensor_motion_aq2_iaszone_2
        to: 'on'
    sensor:
      - name: "Last Movement Overloop"
        unique_id: Last_movement_overloop
        state: "{{ now().strftime('%H:%M') }}"

This works and yields the time like 10:00. Now I want to run a piece of code when motion sensor is triggered and the time of the trigger is later than 08:00 but I’m stuck. Can someone help me with this?

Please clarify your question. Is the motion sensor in question the same one that triggers the template sensor? By “time of the trigger” do you mean the current trigger event or the previous trigger event? Or is this unrelated to the template sensor?

yes it is the same sensor. So if the sensor is triggered at for instance 12:30 the config yaml populates the “Last Movement Overloop” with 12:30. then the automation triggered because of this same motion sensor I want an condition that runs a code when this 12:30 is later than 8:00. if the sensor is trigger before 08:00 I dont want that action to run. So I need the piece of code for the condition

What you want is still unclear to me… you haven’t explained how the sensor and automation linked.

Why do you need the trigger-based template sensor for this? What you have described is a basic time condition… no additional sensor or templating needed:

trigger:
  - platform: state
    entity_id: binary_sensor.lumi_lumi_sensor_motion_aq2_iaszone_2
    to: 'on'
condition:
  - condition: time
    after: "08:00:00"
action:
  - YOUR ACTION

What I basically want is to disarm my alarm in the morning automatically. I have 3 motion sensors for which I store the latest movement time. So if all the stored motion times are later than 08:00 I want the alarm to be disabled. So I figured if I know how to evaluate this stored last movement time against a fix time like 08:00 I can work from there on. The automation is triggered by any of the motion sensors. So if I go to the bathroom when I wake up later than 08:00 I trigger only one sensor. The automation starts and finds only one sensor with a time after 08:00 so the alarm stays on. When I go downstairs and triggered all 3 motion sensors and the time is later than 08:00 the alarm is disabled. Unless you know a different way to disarm the alarm not at a fixed time.

Ok, now we know what the goal is!

For your goal and proposed method I would suggest you alter your trigger-based template sensors to include an attribute.

While the time string is easy for people to read, having a datetime object available makes it easier to use some of the available filters to quickly sort and process the information.

template:
  - trigger:
      - platform: state
        entity_id: binary_sensor.lumi_lumi_sensor_motion_aq2_iaszone_2
        to: 'on'
    sensor:
      - name: "Last Movement Overloop"
        unique_id: Last_movement_overloop
        state: "{{ now().strftime('%H:%M') }}"
        attributes:
          datetime: "{{ now() }}"

Once you have the change shown above instituted, instead of triggering off the states of the motion sensors, you can trigger your automation when the oldest datetime attribute is after today at “08:00”.

trigger:
  - platform: template
    value_template: >
      {% set times =  expand('sensor.last_movement_overloop', 
      'sensor.last_movement_2', 'sensor.last_movement_3') 
      | map(attribute='datetime') | list %}
      {{ times | sort | first > today_at('08:00') }}
condition:
  - alias: "At least 1 known person is Home"
    condition: numeric_state
    entity_id: zone.home
    above: 0
action:
  - YOUR DISARM ACTION

Depending on your normal daily routine, you will likely need to figure out conditional logic that will guard against edge cases where you don’t want the alarm to be disarmed.

Thank you for your help. Really appreciate it. I find the syntax really difficult compared to domoticz. What if I simply want to evaluate one sensor time against the time value.

Now I have
value_template: "{{% sensor.last_movement_hal > today_at("08:00:00") }}"
I tried many variants but none work.

  1. You have an extraneous %
  2. You are attempting to perform a math comparison between the string “sensor.last_movement_hal” and a datetime object. That doesn’t work, you have to compare apples to apples.

If sensor.last_movement_hal is set up the same way as the example you give above for “Overloop” its state is a string like “13:25”. This has a number of drawbacks compared to the datetime attribute-based solution:

  1. It requires one or more conversions from a string to some other data type. (Apples to apples)
  2. Since it doesn’t have a date value, you don’t know if you’re comparing today to today… yesterday … or three weeks ago.

A couple options:

value_template: >
  {% set hm = states('sensor.last_movement_hal').rsplit(':') | map('int', 0) | list %}
  {{ (hm[0], hm[1])  >= (8, 0) }}
value_template: >
  {% set time = strptime(states('sensor.last_movement_hal'), "%H:%M").replace(year=now().year, month=now().month, day=now().day) %}
  {{ time  >= today_at('08:00') }}

Again thanks for your help. Not quite get it to work yet. Is there a way that I store the time not as string but as actual time. Is het then easier to compare? You see I’m quite a noob still. Did not know that the sensor was a string and thought it was the actual time.

Hi.

I found out that I can test code in HA development tools. this really helped me learning the syntax and could really play around. I managed to get it working. Probably it can be coded more efficiently by using entity types instead of individual code but it works. Below the code I used

{% set MotionWoonkamer = as_timestamp(states.binary_sensor.woonkamer_motion_sensor_motion.last_updated)| timestamp_custom('%H:%M') %}
{% set MotionHal = as_timestamp(states.binary_sensor.motion_sensor_hal_motion.last_updated)| timestamp_custom('%H:%M') %}
{% set MotionOverloop = as_timestamp(states.binary_sensor.lumi_lumi_sensor_motion_aq2_iaszone_2.last_updated)| timestamp_custom('%H:%M') %}
{{ MotionOverloop > states('input_datetime.resetalarmtijd') and MotionOverloop < '14:00' and MotionHal > states('input_datetime.resetalarmtijd') and MotionHal < '14:00' and  MotionWoonkamer > states('input_datetime.resetalarmtijd') and MotionWoonkamer < '14:00' and (states('input_boolean.jandirk') == 'on' or states('input_boolean.carolien') == 'on')}}

when a motion sensor is triggered this condition is checked. If true the alarm is turned off. Now I used an input_datetime as min time. next step is to link this to my iPhone alarm clock. Thanks for all the help didgeridrew. Really appreciate it.