State_Filter and dates

Hey folks,

I want a card on my lovelace be setup so that, when I wake up in the morning and view my control panel, if my motion sensor went off in the middle of the night, I want there to be a card there to tell me. But otherwise I don’t want to see it.

I have a sensor called sensor.aarlo_last_kitchen_camera whose value is the last date/time the motion sensor went off in the kitchen camera (e.x. ‘11-06 2:21’). My thinking is that I can use the entity_filter card with the state_filter attribute telling it somehow that the value has to be greater than 24 hours ago (e.x. > ‘11-05 2:21’). I see > as an option, but I don’t know how to tell it to use 24 hours ago’s date (and have that dynamically change every date). Is this possible? If so, how would I do it?

Thanks!!!

Is there perhaps a variable for current date that I can use and somehow subtract 24 hours from that?

you could create a template binary sensor that uses the sensor value for your last motion, converts it to a timestamp, adds 86400 seconds to it (24 hours in seconds), compares that to the current time and then use the result of that in your state filter.

you will need to try to get the year added into your last motion sensor for it to more easily work So the format of the sensor needs to be “11-06-2019 2:21”.

then you could do this:

binary_sensor:
  - platform: template
    sensors:
      motion_plus_twenty_four_hours:
        value_template: >
          {% set time = strptime(states(sensor.aarlo_last_kitchen_camera), '%m-%d-%Y %H:%M') %}
          {{ as_timestamp(now()) < as_timestamp(time) + 86400 }}

then if the binary sensor is true that means the last motion has changed in the last 24 hours and you can then show the card.

you can easily change the time frame by just changing the number of seconds (8 hours ago = 28800 seconds, etc)

1 Like

Awesome! Thank you! With a little tweaking to handle the year, I got it:

binary_sensor:
  - platform: aarlo
    monitored_conditions:
    - motion
  - platform: template
    sensors:
      recent_motion_on_kitchen_camera:
        value_template: >-
          {% set camtime = strptime(now().strftime('%Y') + '-' + states('sensor.aarlo_last_kitchen_camera'), '%Y-%m-%d %H:%M') %}
          {{ as_timestamp(camtime) > (as_timestamp(now()) - 86400) }}

Combined with this card in lovelace, it only shows up when I want to:

          - card:
              entity: camera.aarlo_kitchen_camera
              name: Kitchen Camera Caught Motion
              show:
                - captured_today
                - motion
                - snapshot
                - battery_level
                - signal_strength
                - image_date
              top_date: true
              top_status: true
              top_title: true
              type: 'custom:aarlo-glance'
            entities:
              - binary_sensor.recent_motion_on_kitchen_camera
            show_empty: false
            state_filter:
              - 'on'
            type: entity-filter

Thanks again!

1 Like