Template challenge.. how combine individual triggers in 1 notification

have this automation:

automation:
  - alias: 'Printer Ink Alert'
    trigger:
      platform: state
      entity_id:
        - binary_sensor.ink_level_black_threshold
        - binary_sensor.ink_level_magenta_threshold
        - binary_sensor.ink_level_yellow_threshold
        - binary_sensor.ink_level_cyan_threshold
        - binary_sensor.ink_level_waste_threshold
      to: 'on'
    condition:
      condition: template
      value_template: >
        {{ is_state('input_boolean.notify_system', 'on')}}
    action:
      - service: notify.notify
        data_template:
          title: 'Low ink'
          message: >
            {{ trigger.to_state.attributes.friendly_name }} is low #
      - condition: template
        value_template: >
          {{ is_state('input_boolean.persistent_notification_create', 'on')}}
      - service: persistent_notification.create
        data_template:
          title: 'Low ink'
          message: >
            {{ trigger.to_state.attributes.friendly_name }} is low
          notification_id: low-printer-ink-level-alert

Id like to add the state of the corresponding ink level sensors {{state(sensor.epson_ink_level_black)}}
which is the level in percentage, to the notification.

for that I’d need to filter the triggering binary_sensor and find the color part and fill that in the notification:

          message: >
            {{ trigger.to_state.attributes.friendly_name }} is low at {{{state(`sensor.epson_ink_level_black`)}} %

this of course isnt correct since it does not yet substitute the correct sensor. Struggling a bit here… need to use this:

{{'binary_sensor.ink_level_black_threshold'[24:-10]}} to filter out the black ,but can I do that with the trigger.to_state? like

{{ trigger.entity_id[24:-10]}} ? and then maybe use {% set color = trigger.entity_id[24:-10] %} to add that to {{state("sensor.epson_ink_level_" + color + "'")}}

this at least gives a valid config (btw even with state instead of states…), But it renders ‘is low at unknown’…

action:
  - service: notify.notify
    data_template:
      title: 'Low ink'
      message: >
        {% set color = trigger.entity_id[24:-10] %}
        {{ trigger.to_state.attributes.friendly_name }} is low at 
        {{states("sensor.epson_ink_level_" + color + "'")}}

—edit ----
took out the +"'" and it works! cool. change the title of this thread to reflect my second question:

I now receive separate notifications for each color. Would be cool if that could be grouped, whenever the same trigger is met for any combination of colors… is that possible?

please have a look?
thx!

split the entity_id via the dot and pull the last entry. Split that string via an underscore and pull out the 3rd entry. Then add it to the next entity id. Not sure why you are adding a quote in it either. Don’t add the quote.

{% set color = trigger.entity_id.split('.')[-1].split('_')[2] %}
{% set new_entity_id = 'sensor.epson_ink_level_{}'.format(color) %}
{{ trigger.to_state.attributes.friendly_name }} is low at {{ state(new_entity_id) }}

But in all honesty, if you hit the threshold, and you know the threshold, what’s the point in grabbing the level? I.E. The message occurs at 20%, well… why pull the actual percent when you know the message will get sent at 20%?

1 Like

saw the coming :wink: you’re right of course.
I do it because I like to know how to, and to be able to create as versatile notifications as possible. And, to be sure everything works as expected and the correct values are seen and passed. My way of debugging. I have an input_boolean.notify_system for that. When testing I turn that on and get all notifications, and when I am satisfied and assured all is going as I intended it, I turn it off again, relying on the innards of what Ive just created …

I adapted another automation that had the states directly in it, but then couldn’t use a input_number in the trigger section of numeric_state. I like to use these, so I can test real time, and not have to change the yaml and restart all the time.

the quotes were there because I wasn’t sure if I truly filtered out a state or a string. And thought without it it resulted in 'sensor.epson_ink_level_black and not in 'sensor.epson_ink_level_black'

was looking for the split technique indeed!
think I need {% set color = trigger.entity_id.split(‘.’)[-1].split(‘_’)[3] %} ?

57

is this really state

{{ state(new_entity_id) }} or should it be states(new_entity_id) }}
Nice ! thank you very much!

yeah it should be {{ states(new_entity_id) }}, typo!

1 Like

no I don’t… should be 2 as you said @petro, sorry for that.

now trying to understand :scratch

This properly gets the color name out of the entity_id as long as the color is always in the same spot in relation to the period and underscores. It’s a much better way than using trigger.entity_id[24:-10].

The [-1] after the first split gets the last item in the list. The [2] after the second split gets the 3rd item. Lists index off base 0, so 0 is the first item, 1 is the second, 2 is the third and so on.

1 Like

was aware of the off base 0, but forgot the [-1] took out the last, was confused with the [24:-10] where the 24 takes out all before the position wanted.

btw, I always try to consistently name entities, so the [24:-10] would apply.
like the split notation much better though, has more intelligence as opposed to the hardcoded numbers.
thanks for taking the time to explain once more :+1: