Trigger Variable not returning expected results in a notification

I don’t really know how to ask this question here it is and with examples. I am trying to get a specific state_attr in a notification message and I can only assume I have a formatting problem.

This works and does return the expected value: (this is value I am trying to get in my notification)

{{ (state_attr('light.in_wall_paddle_dimmer_no_ntrl_700s_3', 'brightness') | float(0) / 255 * 100) | round(0) }} %

These also work and do return the expected values:

{{ trigger.to_state.attributes.friendly_name }}
{{ trigger.to_state.attributes.supported_features }}

These do not work as they return no value:

{{ trigger.to_state.attributes.brightness }}
{{ trigger.to_state.attributes.color_mode }}
{{ trigger.to_state.attributes.brightness | float(0) / 255 * 100) | round(0) }} %

If it returned the expected value, the last example was my original goal.

Try getting the attribute from the trigger.entity_id rather than the to_state. E.g

"{{ ( 100 * state_attr(trigger.entity_id,'brightnes') / 255 ) | round(0) }} %"
1 Like

Please show the automation as well.
Depending on the entity that triggered it, the state will be different, ofc.

From entitiy_id? Strange…

EDIT:

Oh, and I think those attributes “disappear” of an OFF light, or at least on some of them


image

What is starange about using the entity id in state_att() ?

It is always the first argument.

You are correct about the brightness attribute not existing when off though.

Ok, so @tom_l, yes that works although my attempt starting working also, but only when the value is >0. Now that both are working, is one of them more correct? I can use either … Also, as you and @koying have pointed out, the attribute disappears when the state is OFF. Which means 50% of my tests were not going to work anyway.

Now that I know that, can you tell me how best way to write this? An example of the message I’m hoping to receive is:

Title: Foyer Lights
Message: Outside Light is ON at 15 %

and I guess for states other than “ON”, I should get a message like this:

Message: Outside Light is OFF

alias: Foyer Lights
description: 
trigger:
  - platform: state
    entity_id:
      - light.in_wall_paddle_dimmer_no_ntrl_700s_3
    to: null
    id: Outside Light

if:
  - condition: trigger
    id:
      - Outside Light
then:
  - service: notify.pushover
    data:
      title: "{{ this.attributes.friendly_name }}"
      message: >-
        {{ trigger.id }} is {{ trigger.to_state.state | upper }} at {{ ( 100 *
        state_attr(trigger.entity_id,'brightness') / 255 ) | round(0) }} %

Ow, ofc. Somehow missed the state_attrs() :roll_eyes:

      message: >-
        {%- set b = (100 * state_attr(trigger.entity_id, 'brightness') | default(0, true) / 255 ) | round(0) -%}
        {{ trigger.id }} is {{ trigger.to_state.state | upper }}{{ ' at ' ~ b if b > 0 else '' }}

On a separate note, what’s the reason for using trigger.id to report the light as Outside Light in message? Why not use the light’s friendly_name?

I’m missing the percent symbol in the message when the value is >0. Where would I put that in the provided statement?

Also, for your question, the naming convention or structure I use for my device names isn’t always a friendly_name when read in a message. I name my devices based on type.area.sub-area.name which helps when searching/sorting/etc… Using the trigger.id allows me a bit of customization for readability of the message.

      message: >-
        {%- set b = (100 * state_attr(trigger.entity_id, 'brightness') | default(0, true) / 255 ) | round(0) -%}
        {{ trigger.id }} is {{ trigger.to_state.state | upper }}{{ ' at ' ~ b ~ ' %' if b > 0 else '' }}

OK.

1 Like

That’s it, Thank You to everyone that helped resolve my problem.

1 Like