Templating/jinja2 coding help

any jinja2 gurus out there could help me out, I just can’t wrap my head around this language.

state_attr(‘sensor.NAME’,‘ATTRIB’) is a , separated list

i want to iterate through the list and take every 2nd entry and subtract it from sensor.NAME if its >=1 increment a counter by 1 and then return the counter as the template value

(sensor.NAME’s default value is a string i can cast as an int)

just trying to explain in a text/coding way below

i=1

loop till end of state_attr(‘sensor.NAME’,‘ATTRIB’)

if (sensor.Name - state_attr(‘sensor.NAME’,‘ATTRIB’)[ i]) >=1

counter++

i+2

end loop return counter

TYIA

Post an example of the list in the sensor’s attribute.

Also, is it a true list or a string with comma-delimited numbers?

"blocks": ["453451303ba99fa66d77e2be35927baa5f393546fc9bc4f968a6683f9128123f:1619425208:305376244662:461252743467:0:1081584146136", "2347674", "55648171982dab4bf3bca91d5df49ca7dc36bb44d44c94cac9ac1af5bf5572af:1619415056:292354865858:370113923990:0:1051322634107", "2347562", "fd117f84f340c56c5e1340826da7c7a418de4ec3a8e07142c31d87ac9fed40c4:1619405343:292951499099:90901650764:0:1053290005580", "2347482", "600c13fe67553a81fb7c6d59545f7c6ad2ff7bcc0e3ec38fe11017c1c83ab493:1619402959:295008779986:63666215440:0:1052327046040", "2347464", "fa0da095edf39255a79082aad02f10959acff91c1a2eb49f662ba9794bd26be8:1619401303:291704133169:690175999956:0:1051680258020", "2347453", "3d3e33be7ca0e3f29ff1c852e896cba629babf91ce4fc3b1f6dab783e655166c:1619384319:292111575573:77797045835:0:1054856852826", "2347299", "0966888c939129452383d3c8291357f115d59bbf00857b894ea61c0bea65596a:1619382676:291541617037:698842749578:0:1075851326969", "2347292", "1d26b21db5b6e87670b639bf41fe8c52e97bd0721f3d83375a44aeb260bf04cd:1619369197:291766844491:151358541367:0:1053005329986", "2347177", "fef7a087ceac17b5f6976bc909667e085e0177a0962f8ac2dc433656b2852eae:1619366319:285096052312:97081817355:0:1052171110815", "2347151", "d5c6cbbe90a1a25fb4077205de444f48ab703881dcd2bee42772b858836fe330:1619364461:284082811080:277968573023:0:1055672882193", "2347134", "d93d73f6bef1dac1da5cbff0298723add9d625bac58db5b16488ed708141ff08:1619359047:284961310218:111103310897:0:1052316447936", "2347092", "1f2476c9209552a51dd62bea3355c615380e95b73e38d49e4d14ef717e3ff529:1619356820:284720877958:182742035192:0:1052580140788", "2347069", "6bf4247cef06c49055b92257cc940509f218b5a723ba682933ef85e428b0a567:1619353093:281015163005:100879308334:0:1053294608937", "2347029", "cfd5b9948044f7f54ee28faf67535f68fa2f2f574ed11045cfa587a3bf276ec2:1619350980:275419840891:141740198640:0:1053047647397", "2347006", "cdb7f73c5d34093e34bef2f735b367878968ad591656de63649be33ce2cf7ac5:1619347956:275493910813:672469670791:0:1053410712861", "2346983", "f31dd3af312bc7a7165cd4500b25799ac07a94cc7949899b62c041a44d29c53f:1619331628:272250655063:415526595209:0:1053270175594", "2346856", "2038bdddfa53ac13440c1af1d44f40bec58841633edbf2be0ab354593b6430ee:1619319783:271352350997:123730518885:0:1053102637353", "2346752", "256bd8ec088ac3e86ad04e2299c047a4e77b6df9a93b8cbba5c954b30f7cc0ee:1619316292:267354542947:4243781363:0:1055287048137", "2346718", "b599a069513574c52602c3b97e20873fd7297295c87ecad61df5a44b63a642cc:1619316174:267113235881:12035161410:0:1053018426610", "2346717", "52282d357db660978827aeb2cfa18abdf4b392a62749841f954076d909d08b26:1619315838:267379571972:545509070077:0:1053151022053", "2346714", "651521175653d2ddd184b734a5eb9243e5456cc0345523fa4253a159c12922da:1619301757:257373147691:110833141358:0:1053297836679", "2346583", "770aea1eb8767aa73ccf04a39dc3591acbe04cdd44ac45a343b44c78608d52b8:1619299222:264078716631:577731421803:0:1053374071672", "2346557", "d3a831446ab32c9e17b008a0ea79e6270b6c7a5772409d0a888102258dab6308:1619287447:258626413483:364073186382:0:1053881912423", "2346453", "657f342bc6ce84223e60cda617be46c941dc794922e03d6ed6496b2541eeff21:1619280415:260289013230:68484531890:0:1054260184018", "2346381", "3f3aecf8b0d55b6489cdadf6de47b03c4ba36bbc58b486b039db04ed69feb597:1619279086:261479855980:203229403220:0:1056695086072", "2346367", "3fb1f0e08f128ec757c739db661e8557920791f8e11d6f44302bd5708467baab:1619275135:266862643614:215425673807:0:1053817352513", "2346340", "4231562c8256802a426dd12dd524e06f7609e51f57220f434f55abe2e3704189:1619270852:261358718865:387247067388:0:1056101061958", "2346307", "216b6d7ec611302ad77e8feeb49463fa7930ba5b94ba3a9cbde318a940eeee04:1619262792:264839485715:420836188212:0:1054674082993", "2346242", "a8963fc5bf61cc98ee6d741ee039761fc3d6777e88f6d8b3cf0282dfeaf1c9a2:1619253430:265624255994:156367681964:0:1055805983937", "2346168", "5266ecbf1a28a3033e2de000b05c8db0d403bdbf2e664e538a7ec1711fbe2e62:1619249684:263101488411:244182191303:0:1056834248719", "2346137"]
```![blocks|256x500](upload://tdIGCMGz8UqNZlHQv6tOCsJRmw7.png) 

thanks for taking the time

Welcome to the community. Hopefully your future requests will be easier. :crazy_face: Pop this in Developer Tools / Templates to see what’s going on.

{% set blocks = ["453451303ba99fa66d77e2be35927baa5f393546fc9bc4f968a6683f9128123f:1619425208:305376244662:461252743467:0:1081584146136", "2347674", "55648171982dab4bf3bca91d5df49ca7dc36bb44d44c94cac9ac1af5bf5572af:1619415056:292354865858:370113923990:0:1051322634107", "2347562", "fd117f84f340c56c5e1340826da7c7a418de4ec3a8e07142c31d87ac9fed40c4:1619405343:292951499099:90901650764:0:1053290005580", "2347482", "600c13fe67553a81fb7c6d59545f7c6ad2ff7bcc0e3ec38fe11017c1c83ab493:1619402959:295008779986:63666215440:0:1052327046040", "2347464", "fa0da095edf39255a79082aad02f10959acff91c1a2eb49f662ba9794bd26be8:1619401303:291704133169:690175999956:0:1051680258020", "2347453", "3d3e33be7ca0e3f29ff1c852e896cba629babf91ce4fc3b1f6dab783e655166c:1619384319:292111575573:77797045835:0:1054856852826", "2347299", "0966888c939129452383d3c8291357f115d59bbf00857b894ea61c0bea65596a:1619382676:291541617037:698842749578:0:1075851326969", "2347292", "1d26b21db5b6e87670b639bf41fe8c52e97bd0721f3d83375a44aeb260bf04cd:1619369197:291766844491:151358541367:0:1053005329986", "2347177", "fef7a087ceac17b5f6976bc909667e085e0177a0962f8ac2dc433656b2852eae:1619366319:285096052312:97081817355:0:1052171110815", "2347151", "d5c6cbbe90a1a25fb4077205de444f48ab703881dcd2bee42772b858836fe330:1619364461:284082811080:277968573023:0:1055672882193", "2347134", "d93d73f6bef1dac1da5cbff0298723add9d625bac58db5b16488ed708141ff08:1619359047:284961310218:111103310897:0:1052316447936", "2347092", "1f2476c9209552a51dd62bea3355c615380e95b73e38d49e4d14ef717e3ff529:1619356820:284720877958:182742035192:0:1052580140788", "2347069", "6bf4247cef06c49055b92257cc940509f218b5a723ba682933ef85e428b0a567:1619353093:281015163005:100879308334:0:1053294608937", "2347029", "cfd5b9948044f7f54ee28faf67535f68fa2f2f574ed11045cfa587a3bf276ec2:1619350980:275419840891:141740198640:0:1053047647397", "2347006", "cdb7f73c5d34093e34bef2f735b367878968ad591656de63649be33ce2cf7ac5:1619347956:275493910813:672469670791:0:1053410712861", "2346983", "f31dd3af312bc7a7165cd4500b25799ac07a94cc7949899b62c041a44d29c53f:1619331628:272250655063:415526595209:0:1053270175594", "2346856", "2038bdddfa53ac13440c1af1d44f40bec58841633edbf2be0ab354593b6430ee:1619319783:271352350997:123730518885:0:1053102637353", "2346752", "256bd8ec088ac3e86ad04e2299c047a4e77b6df9a93b8cbba5c954b30f7cc0ee:1619316292:267354542947:4243781363:0:1055287048137", "2346718", "b599a069513574c52602c3b97e20873fd7297295c87ecad61df5a44b63a642cc:1619316174:267113235881:12035161410:0:1053018426610", "2346717", "52282d357db660978827aeb2cfa18abdf4b392a62749841f954076d909d08b26:1619315838:267379571972:545509070077:0:1053151022053", "2346714", "651521175653d2ddd184b734a5eb9243e5456cc0345523fa4253a159c12922da:1619301757:257373147691:110833141358:0:1053297836679", "2346583", "770aea1eb8767aa73ccf04a39dc3591acbe04cdd44ac45a343b44c78608d52b8:1619299222:264078716631:577731421803:0:1053374071672", "2346557", "d3a831446ab32c9e17b008a0ea79e6270b6c7a5772409d0a888102258dab6308:1619287447:258626413483:364073186382:0:1053881912423", "2346453", "657f342bc6ce84223e60cda617be46c941dc794922e03d6ed6496b2541eeff21:1619280415:260289013230:68484531890:0:1054260184018", "2346381", "3f3aecf8b0d55b6489cdadf6de47b03c4ba36bbc58b486b039db04ed69feb597:1619279086:261479855980:203229403220:0:1056695086072", "2346367", "3fb1f0e08f128ec757c739db661e8557920791f8e11d6f44302bd5708467baab:1619275135:266862643614:215425673807:0:1053817352513", "2346340", "4231562c8256802a426dd12dd524e06f7609e51f57220f434f55abe2e3704189:1619270852:261358718865:387247067388:0:1056101061958", "2346307", "216b6d7ec611302ad77e8feeb49463fa7930ba5b94ba3a9cbde318a940eeee04:1619262792:264839485715:420836188212:0:1054674082993", "2346242", "a8963fc5bf61cc98ee6d741ee039761fc3d6777e88f6d8b3cf0282dfeaf1c9a2:1619253430:265624255994:156367681964:0:1055805983937", "2346168", "5266ecbf1a28a3033e2de000b05c8db0d403bdbf2e664e538a7ec1711fbe2e62:1619249684:263101488411:244182191303:0:1056834248719", "2346137"] %}
{% set sensor_name = 2347000 %}
{% set blocks_values = blocks|map('int')|select("greaterthan", 0)|list %}
{{ blocks_values }}
{{ blocks_values|select("<=", sensor_name - 1)|list }}
{{ blocks_values|select("<=", sensor_name - 1)|list|length }}

It assumes that the even-index items ([0], [2] etc) cannot be converted to int, and that the values will always be greater than zero. You only need the last line for the final template — I just left the two lines before in so you can see what’s happening.

EDIT: I think I had the test the wrong way around. lessthan is what you want for the last two selects, I think, but you can take it from here.

EDIT AGAIN: I hate inequalities (both societal and arithmetic). It’s < that I need, without the -1. Thanks to @123 for spotting below.

Ultimately reduces to:

{{ blocks|map('int')|select("greaterthan", 0)|select("<", sensor_name)|list|length }}
3 Likes

Thank you so much!! now I have a starting point!!

Nicely done! I was going to use this to select every second item in the list:

blocks[1::2] 

However, I didn’t think of leveraging the fact that the other items in the list are non-numeric and so would not be converted by map('int').

@optikkore

Don’t forget to mark Troon’s post with the Solution tag; it deserves it.

1 Like

Of course. Even shorter solution, then, and without the requirement that the “unwanted” items can’t convert to int:

{{ blocks[1::2]|map('int')|select("lessthan", sensor_name - 1)|list|length }}

Code golf, anyone?

Yup, added advantage of using blocks[1::2] is that it will work even if the undesired list items are numeric.

@123 @Troon Thank you guys seriously

Now to load the data dynamically and fix the counting (should have been more transparent about what i was trying to make) want to count if sensor_name - blocks_values = 60 > 1 (over 60 is invalid data and negative means it was already found) which represents a pending block in my mining pool, pending blocks = reward so wanted a count of blocks pending

Question for you:

Are you certain this:

select("lessthan", sensor_name - 1)

implements this:

if (sensor.Name - state_attr('sensor.NAME','ATTRIB')[ i]) >=1

The test is “greater than or equal to”. In other words it’s acceptable for the difference to be equal to 1 or less. In contrast “lessthan” won’t accept a difference equal to 1 (only less than).

Perhaps it should be this?

select("<=", sensor_name - 1)
1 Like

Yes, you’re right: well spotted, and I didn’t know about the “symbolic” select possibilities. New even smaller solution:

{{ blocks[1::2]|map('int')|select("<=",sensor_name-1)|list|length }}

I will update the solution above for those reading in future.

Actually, if it’s integers-only, then:

select("<=",sensor_name-1)

is the same as

select("<",sensor_name)

so:

{{ blocks[1::2]|map('int')|select("<",sensor_name)|list|length }}
1 Like

@123 @Troon

Ok So I do believe I have it working the way I want it in case you guys were wondering

{% set blocks = state_attr('sensor.monerohash_block','blocks')  %}
{% set sensor_name = states('sensor.monerohash_block')|int %}
{{ blocks[1::2]|map('int')|select(">=",sensor_name - 60)|list|length }}

current block height 2347854 found block list 2347853, 2347674, 2347482,…etc returns 1 until the current block height goes to 2347914 reports 0 pending no new blocks are found

1 Like