Saving state into a variable

I’m attempting to customize an existing BluePrint to leverage an existing HACS adding for sending notifications.

Here is the code thus far: GarbageCollection.yaml (github.com)

The blueprint imports fine, but when I kick it off, there are 2 issues that i’m having trouble figuring out. I’m relatively new to YAML and JINJA formatting, so bare with me.

The first issue is that line 34 should be saving a query into a variable:

  sensors_tomorrow: "{{ states.sensor | selectattr('attributes.device_class', 'equalto', 'garbage_collection__schedule') | selectattr('attributes.days', 'equalto', day_offset) | map(attribute='attributes.friendly_name')| list | join(', ') }}"

I can run the above as a tempalte with the variable day_offset replaced with a static number and I get the expected values, but when i call this from the automation leveraging this blueprint, it does not return any text. What am I missing here?

Here is what i’d expect that to look like based on the template tool:

Next issue is related to the above. There is a condition which should control if this automation should even run. E.g. if you get no results on the above query, do not run, but it seems to still be running and just sending empty text. Here is the condition statement:

condition:
  condition: template
  value_template: "{{ sensors_tomorrow != '' }}"

Any pointers on the above would be much apprecaited. I feel like i’m very close and pulling my hair out over something trivial, so hoping to use this as a learning oppurtunity.

Thanks!

For the first template, could it be due to a type incompatibility? Perhaps the day_offset variable is being handled as a string whereas attributes.days is a number. Therefore the comparison performed by selectattr always fails because it’s comparing a number to a string.

As an experiment cast the variable to integer.

selectattr('attributes.days', 'equalto', day_offset|int(0))

Thank you @123, that fixed that issue. so now just the conditional issue left. if I change the date offset to out of range where the values returned to be null or empty, its still triggering when testing.

When it should report an empty string, look at the automation’s trace and examine the value of sensors_tomorrow to see what it actually reports.

@123 , I did check there. Now that its working, i can actually see the values change, but still doesn’t totally explain why the condition isn’t working.

Here it is when its working:

Here it is when its not:

I suggest this:

  value_template: "{{ sensors_tomorrow | length > 0 }}"

This did indeed fix it. I was digging through the forums and was looking at something similar you had posted for someone else when you posted this. So thank you for your contributions here!

You’re welcome!

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions. For more information, refer to guideline 21 in the FAQ.