MQTT Device ID / NAME Conversion

Hi,
Trying to get a template setup working with variables as the inputs rather than hard coding anything. I’m failing to get my MQTT Switch via Selector (I know this holds ID only) to pass the Device Name so that the Topic tracks correctly:

If not obvious, I want to replace the hardcoded “Office - Switch …” with a variable relating to the Switch from the selector.

I’ve tried “{{device_attr(switch, ‘name’)}}” in various places + permutations, but all cause the blueprint to have a fit !

Any ideas please!

Hello AK,

First device_id’s do not work with templates in 99% of all cases, so use entity_id’s.

Can you share all the yaml. I don’t even know what kind of blueprint that is.
Please share the YAML and any errors so we can see what you’ve done.

You can use the </> button like this… How to format your code in forum posts
OR… Here is an example of how to fix it from the site FAQ Page.
How to help us help you - or How to ask a good question.

Hi,
Pretty much that is the entire YAML, I’ve written it from scratch as a way to try to learn the language / syntax used in HAOS.

Only thing missing is the header and a couple of dummy actions at the end.

I was leaning, towards something didn’t work right with device_id’s but wasn’t 100% as a lot of posts seemed to just say “use device_attr …”.

I think my problem is how to use entity_IDs with MQTT properly and the only entities that look like they are being exposed for use are battery and firmware update.

I’ll post the full code when I’m not on a mobile device.
Ta.