At the end of the day you’re going to need to use a loop since you don’t know how many results will come back requiring notifications. So I don’t see how a single line template is going to help you out.
If you still wanted a single line template anyway, my guess as to what you’d want this template to output (since you haven’t specified) would be a dictionary with each main item that matches as the key
, and then the notify number as the value
.
Here’s what I’m using as input to my template, since I don’t have your sensor and I am trying to include the various possibilities:
Click here to show template input
{% set devices =
{
"f07903c4e39c4d2f8d4ea3ac900fb6bb": {
"config": {
"notify": "4567776771",
"opt": "on",
"location": "1"
},
"switch1": {
"start": "",
"notified": ""
},
"switch2": {
"start": "",
"notified": ""
}
},
"a31203c4e39c4d2f8d4ea3ac900fb6bb": {
"config": {
"notify": "4565576771",
"opt": "off",
"location": "2"
},
"switch1": {
"start": "2024-08-12 12:54:56-05:00",
"notified": ""
},
"switch2": {
"start": "",
"notified": ""
}
},
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa": {
"config": {
"notify": "1111111111",
"opt": "off",
"location": "2"
},
"switch1": {
"start": "",
"notified": ""
},
"switch2": {
"start": "2024-08-12 13:04:56-05:00",
"notified": ""
}
},
"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb": {
"config": {
"notify": "2222222222",
"opt": "off",
"location": "2"
},
"switch1": {
"start": "",
"notified": ""
},
"switch2": {
"start": "2024-08-12 12:24:56-05:00",
"notified": ""
}
}
}
%}
{% set time_compare = (('2024-08-12 14:00:00-05:00' | as_datetime).replace(microsecond=0) + timedelta(hours=-1)) | string %}
Note that essentially it means now()
is always assumed to be 2024-08-12 14:00:00-05:00
And if that is the input, this is what I’m assuming you’d want as the output:
{
"a31203c4e39c4d2f8d4ea3ac900fb6bb": "4565576771",
"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb": "2222222222"
}
But that result is not possible without looping anyway (at least not until the zip()
PR is approved).
If you merely wanted a list of the relevant notifiers (which would be ['4565576771', '2222222222']
from the template input specified earlier) then that would be possible with this template:
{{ devices.values() | rejectattr('switch1.start','eq','') | selectattr('switch1.start','lt', time_compare) | map(attribute='config.notify') | list +
devices.values() | rejectattr('switch2.start','eq','') | selectattr('switch2.start','lt', time_compare) | map(attribute='config.notify') | list }}
In the end I think what you should do is post your automation and ask for help simplifying it. I don’t expect the answer to be a single template, but there may be template improvements (or other logic improvements) that you can make.