Assistance with Sensor filter when state is non standard date/time

Hi.
i am trying to use the following code to select an Arlo camera that has had it’s object detected sensor updated in the last 5 minutes. This will later be used for a custom entity as part of a larger automation.

My issue is that the aarlo API returns the date time in the following format
13-12 11:00
In the above example it would be 13th December 11:00am

Here is the partial code I am using to select the sensors that are active.

	{% set time = now() - timedelta(minutes = 5) %}
	{{ states.sensor
		| rejectattr('attributes.brand', 'undefined')
		| rejectattr('attributes.object_type', 'undefined')
		| selectattr('attributes.brand', 'eq', 'Netgear Arlo')
		| selectattr('state', '<', time)
		| list
		| count
	}}

This throws an error because the state is returned as a string.
I was hoping to use something like this code to convert, but I don’t know how to use this in combination of the above code.

{{ strptime(states.sensor.aarlo_last_zone_1_floodlight_1.state, '%m - %d %-H : %M')}}

I think you’re going to need a for loop… The now() function you are using to define your time variable returns a datetime object with a year and timezone. That is what you need to provide for an accurate comparison. If you do not provide those data points the strptime() function will use its defaults. The defaults are 1900 for the year and UTC for the timezone.

{% set time = now() - timedelta(minutes = 5) %}
{% set times = states.sensor
    | rejectattr('attributes.brand', 'undefined')
    | rejectattr('attributes.object_type', 'undefined')
    | selectattr('attributes.brand', 'eq', 'Netgear Arlo')
    | map(attribute='state') | list
%}
{%- set ns = namespace(count_times=[])%}
{%- for t in times %}
{%- set x = now().year~"-"~t~now().strftime("%z") %}
{%- set y = strptime( x,'%Y-%d-%m %H:%M%z', now() + timedelta(days=1) )%}
{%- if y < time %}
{%- set ns.count_times = ns.count_times + [y] %}
{%- endif %}{%- endfor %}
{{ ns.count_times | count }}
1 Like