How to automatically retrieve device name from entity_id?

How many of these device/entities do you need to make translations for?

You could just add an associative array/list in the function node.
Pseudo:

array  = ['entity_id' = 'living room light', 'other_entity_id' = 'some other light']

message = array[entity_id] + ' is now ' + msg.topic;

As I said, NodeRED is not for me, I personally find it to complicated. In my case an automation does the job. :slight_smile: But thatā€™s me, everybody has to find itā€™s own method to work with HA. :slight_smile:

The documentation is not the best (in terms of readability and ā€œfind-what-youā€™re-looking-forā€), but it is a start.
Trigger: Automation Trigger - Home Assistant
Trigger variables: Automation Trigger Variables - Home Assistant <= what trigger entries are available
Templating: Templating - Home Assistant <= here you find how, where and why to use templates (meaning the brackets are explained there)

Under the templating docu see the examples at the end of the page. :wink:

The brackets is templating. Home Assistant uses Jinja 2 pretty much everywhere so thatā€™s what youā€™re seeing there. The Jinja engine processes strings like that and anything which isnā€™t in brackets is just treated as plain old text. Anything in brackets is processed as detailed their in their docs. Home Assistant also has a guide specifically on templating within HA here.

Node RED does something similar btw. It seems like you are dropping into function nodes a lot which is fine but often just ends up requiring a lot more coding work on your part. You can accomplish quite a lot without function nodes at all by combining other nodes into flows and leveraging jsonata for small bits of processing. That is the templating engine Node RED has chosen. Itā€™s quite powerful but not as approachable as Jinja in my opinion, thereā€™s a bit too many ā€œunique to jsonataā€ operators. But you can do a lot with a little if you take the time to go through their docs and the built in tester for jsonata expressions is fantastic.

Patrick just listed out above most of the other docs I was going to link for explaining the trigger object in such in Home Assistant so I wonā€™t repeat.

For the node red side I could link docs but for now Iā€™ll just say the debug node should be your best friend. You can drop it in and attach it at literally any point in your flow and print out the entire message object as Node RED sees it. That is more helpful then any documentation for figuring out what you can work with at that point. And then you can can copy that json into an Inject node and make isolated test cases until things work the way you want to.

This functionality of being able to isolate, test and iterate on small pieces of a large complex automation is probably the single reason I do most of my automation in Node RED. That and its easier to see the big picture when you have a piece of functionality that spans multiple automations/scripts since you can organize all of them into one flow/tab.

Thank you all for the great links. I have started reading them and will try to learn a bit more about the syntax used.
I do like the idea of being able to write the code rather than relying on the abilities of a UI. I do the same for other languages, but jinja and json are not mine (yet :wink: ).

By the way, I tried

message: "Shelly {{ state_attr('trigger.entity_id', 'friendly_name') }} seems to have a problem."

and it sent

Shelly None seems to have a problem."

Hi,
just as an update, despite me being the only noob here :smiley:

    data_template:
      message: >-
        {{ trigger.to_state.attributes.friendly_name }}: {{
        trigger.to_state.state }}

This is the correct syntax to get the friendly name of the entity that triggered the notification.

4 Likes

So, I have to say I like the idea of direclty using code rather than Node Red. It seems more straight forward.
I will be opening a new thread about automations looking for some help with my syntax. I am a bit ā€œstuckā€ with my code right now. HA isnā€™t accepting it and hence not saving it as automation.
I have opened a new thread here in the forum Syntax questions for automations

I have to say, I feel much more comfortable ā€œcodingā€ it myself even though I am still learning about the correct syntax. So much easier in other languages :smiley:

2 Likes

hey,

this is pretty cool. thanks much for sharing.

one question, the area_id for entities donā€™t seem to populate but they do populate for devices. is this expected?

thank you.