I’ve been playing with it and got it to work. There’s only one thing I’m stuck with; I now have 2 nameless sensors. How can I get them to have a name containing the id?
I expected to see unique_id: in100_0001_battery and unique_id: in100_0002_battery in Development Tools, States, but that isnt the case. I guess the unique_id’s are for internal reference only.
Also, id: must be a string, a numerical name isnt accepted. Placing i.e. 0001 in single or double quotes throws an error.
no? they have names as is clearly shown in your screenshot?
unique_id indeed is an internal identifier used by the system. as long as its unique, you can make it anything, there are online aid generators you can use, I always like to use the same as my object_id, which is also unique, and makes for clear yaml configs
not sure what you are saying here, because using single or double quotes would make not a string, not a numerical
yaml really only ‘requires’ quotes around boolean values, because is you dont use quotes there, yaml interpreted them as booleans. (on/off, true/false, yes.no)
anything else can be used without quotes. Unless of course inside the jinja templates like the .get('id'). if you’d use .get(id) it would see id as a variable that should have been defined before using that
if you need a number and its the output of a template (which is always a string) you need to ‘cast’ it to |int or |float, to make those numbers again and use numerical operations on them
I’d like the sensors to have a more comprehensive name. So in stead of sensor.battery_id_none I’d like it to be named battery_bthome_sensor_0001, or something that makes it easier to link to the id I set in the sensor definition. Where does this sensor.battery_id_none come from anyway?
Same goes for sensor.battery_none, where is this name set?
As for id:
As per your example I first put id: 0001, but HA didnt like that and returned
I’m sure I missed something trivial here, but if you can help me, please do
Ok, didn’t know that. I use Studio Code Server and thought that it checked the syntax. I’ve set it to 0001 and indeed it works
Remains the question where the sensor labelcome from. And by that I mean sensor.battery_none, not the friendly name.
it comes from this, because the id doesn’t exist on creation and then creates it with the default output of the .get(), which will be None. That’s then slugified, making it none.
I’m currently in the process of DRYing my packages using YAML anchors.
That works unless I try to reduce the (MQTT) sensor definitions even more by faciliating Marius’ attributes way:
Checking my configuration throws this error: extra keys not allowed @ data[‘mqtt’][0][‘sensor’][0][‘attributes’]. Got {‘id’: 1}
together with other errors because the referenced ‘id’ never got populated.
As @petrostated maybe the ‘id’ isn’t available upon creation of the sensor, but why does Marius’ code work while Patrick’s and mine do not?
At first I suspected this could be because mine are no template sensors but then again, at least Patrick’s should have worked like Marius’.
@Mariusthvdb how would your battery_test_anchor_kelder_aanbouw sensor be named? Perhaps I’m using the Jinja2 {{this.attributes.get('id')}} wrongly?
Because Marius most likely created this entity multiple times and the template integration restored the previous state.
This will not work for the MQTT integration because it does not have attributes as an available key for the yaml configuration. You can’t just make up YAML, you have to follow the documentation’s yaml fields. The error you’re getting is telling you that attributes isn’t valid for MQTT.
I managed to customize the id attribute into the sensors and restarted HA but the error stays: Invalid config for [mqtt]: MQTT topic name/filter must not contain control characters. for dictionary value @ data[‘mqtt’][0][‘sensor’][0][‘availability_topic’]. Got ‘“allg/steckdosen/A1”{{this.attributes.get(‘id’)}}"/tele/LWT"\n’
2 lessons learned:
Petro is right (as nearly always): no attribute in mqtt integration (worth a feature request if you ask me) and
Marius might be a wizard (at least one could get the notion looking at his posts and github).
your issue isnt the id itself, its the fact using those jinja templates in the config like you do. check the docs for usage of templates, your options are not among those…
you can not just copy the template sensor config onto an Mqtt sensor config
Yep, did that already.
The four sensors per plug used to have 72 lines; thanks to the anchors I’m down to 30.
I’ll be content with this; that package is working now and will possibly grow very old before I feel the need to tamper with it again.
But after adding and deleting several sensors I got tired of having to move those anchors also, I decided to create them atop and never need to think of them afterwards . Just inject wherever.
We don’t need to, but we can. It’s just a matter of what s most convenient
Ok, got it working for the battery part.
But the sensors also report temp and humidity. To calibrate them I’d like to add a slider. Is it possible to use anchors with an input_number? I’d like to avoid making 20 nearly identical input_numbers.
Can you help me make one input_number, just like we’ve done with the battery template sensor? Tried searching, but found nothing useful. Should there be docs or topics similar to this, then a link is also welcome.
Afaik input_numbers don’t have unique_id’s (or something alike)
Well just put everything you want to be the same under the <<: & define anchor and paste that with the <<: *
Just like the post above does. Remember, this is a yaml thing not Ha. So as long as the yaml is valid you’re good to go. Backend/frontend no matter where
Configuration invalid!
Error loading /config/configuration.yaml: while constructing a mapping in
"/config/configuration.yaml", line 49, column 5 expected a mapping or
list of mappings for merging, but found scalar in "/config/configuration.yaml",
line 50, column 9