Using a state attribute in an automation action

Hi,

I’m trying to use a state attribute and strip it to use in a TTS action after a trigger in an automation. I am able to get the default ‘state’, but I’m trying to get the ‘Game’ attribute under this state.

service: tts.google_translate_say
data:
  entity_id: media_player.google_home
  language: nl
  message: >-
    De status is nu:
    {{states.sensor.XXXXXX.state}}

I’ve tried state.Game, state|Game, but none of them seem to work and searching for state attributes only gives me examples of how to use attributes in a trigger or condition. Any help appreciated :slight_smile:

{{ state_attr('sensor.XXXX', 'Game') }}

Also for reference the recommended way to get to the state of an entity in a template is like this:

{{ states('sensor.XXXX') }}

See the warning here for why (as well as all the other stuff you can do with states in templates).

2 Likes

Hi @CentralCommand ,

Thanks for pointing me to that page, that makes sense :slight_smile:

With this i’m getting the current state of the sensor:

{{ states('sensor.discord_user_XXXXXX') }} 

However, with this, i’m getting ‘None’ as output:

{{ state_attr('sensor.discord_user_XXXXXX', 'Game') }}

The ‘Game’ attribute does exist under the sensor pulldown menu ‘Attributes’ and it is filled. Also changed it to another att. which exists, and it has the same outcome

Check Developer tools → States. HA is case sensitive, so if the attribute is really game it won’t find one called Game

That was it :slight_smile: The attributes are lowercase in the states. The UI shows them with uppercase first :slight_smile:
Amazing, I think I can work this out from here. Thanks!

Yes, dashboards are coded to make things look pretty. Among other things, applying a device class to a binary sensor can change the state from on to all these displayed values. The only place to find the actual state is in Developer Tools > States.