How to easily define 10 identical sensors

@Mariusthvdb

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?

template:
  - sensor:
      - unique_id: in100_0001_battery
        attributes:
          id: bthome_sensor_0001
        <<: &in100_batt_sensor
          name: >
            Battery: {{this.attributes.get('id')}}
          unit_of_measurement: "%"
          device_class: battery
          state: >
            {% set id = this.attributes.get('id') %}
            {% set batt_volts = states('sensor.'~id~'_count')|float(0)/32 %}
            {% set batt_pct = 55.55 * batt_volts - 83.31 %} 
            {% set batt_pct_clamped = ([0, batt_pct, 100]|sort)[1]|int %} 
            {{batt_pct_clamped}}

      - unique_id: in100_0002_battery
        attributes:
          id: bthome_sensor_0002
        <<: *in100_batt_sensor

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

Let me rephrase;

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

image

I’m sure I missed something trivial here, but if you can help me, please do :slight_smile:

that’s not HA that doesn’t like that. That’s whatever addon you’re using. It has no idea what the value should be, ignore it.

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 :slight_smile:
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.

Makes sense. Is there a way to give the sensor a more meaningful name?

it has a unique_id, name it whatever you want in the ui

1 Like

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 Mariusattributes way:

mqtt:
  sensor:
# Server & Internet
    - unique_id: nousa101_power
      attributes:
        id: 01
      <<: &power
        object_id: >
          nousa1{{this.attributes.get('id')}}_power
        state_topic: >
          "allg/steckdosen/A1"{{this.attributes.get('id')}}"/tele/SENSOR"
        availability_topic: >
          "allg/steckdosen/A1"{{this.attributes.get('id')}}"/tele/LWT"
        name: power #"nousA101 Power"
        value_template: '{{ value_json["ENERGY"]["Power"] }}'
        unit_of_measurement: "W"
        device_class: power
        state_class: measurement
        payload_available: "Online"
        payload_not_available: "Offline"

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 @petro stated 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.

You could set that attribute in customize probably, I have that in more than 1 spot where the official integration doesn’t support it

homeassistant:

  customize:

    binary_sensor.watermeter_leak_detected:
      id: watermeter

##########################################################################################
# Binary Mqtt sensor
##########################################################################################

  binary_sensor:

    - unique_id: watermeter_smart_gateways_leak_detect
      state_topic: watermeter/reading/leak_detect
      payload_on: 'true'
      payload_off: 'false'
      device_class: moisture

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:

  1. Petro is right (as nearly always): no attribute in mqtt integration (worth a feature request if you ask me) and
  2. 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

you could drop this in an anchor:

        value_template: '{{ value_json["ENERGY"]["Power"] }}'
        unit_of_measurement: "W"
        device_class: power
        state_class: measurement
        payload_available: "Online"
        payload_not_available: "Offline"

on all of the entities that use that

You’d get something like:

homeassistant:

  customize:

##########################################################################################
# Anchors
##########################################################################################

    node.anchors:

      amp: &amp
        unit_of_measurement: A
        device_class: current
        state_class: measurement

      energy: &energy
        unit_of_measurement: kWh
        device_class: energy
        state_class: total_increasing
#         <<: &value
#           value_template: >
#             {{value|round(2,default=none)}}

      power: &power
        unit_of_measurement: W
        device_class: power
        state_class: measurement

      volt: &volt
        unit_of_measurement: V
        device_class: voltage
        state_class: measurement

      kpower: &kpower
        unit_of_measurement: kW
        device_class: power
        state_class: measurement
#         value_template: >
#           {{value|round(3,none)}}

mqtt:

  sensor:

    - unique_id: zp_import
      state_topic: '70:B3:31:F2/energy-solar-sdm630-modbus/f339a2fb/import'
      <<: *energy

I know its not the same, but it does minimize your yaml very nicely

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.

You also don’t need to do that at all and you can define them in place.

mqtt:

  sensor:

    - unique_id: zp_import
      state_topic: '70:B3:31:F2/energy-solar-sdm630-modbus/f339a2fb/import'
      <<: &energy
        unit_of_measurement: kWh
        device_class: energy
        state_class: total_increasing
      
    - unique_id: zp_import_2
      state_topic: '70:B3:31:F2/energy-solar-sdm630-modbus/f339a2fb/import2'
      <<: *energy

Sure had that before .

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)

input_number:
  temp_corr:
    name: Temp correction 001
    unit_of_measurement: "°C"
    min: -10
    max: 10
    step: 0.1
    icon: mdi:pencil-outline
    mode: slider

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

Tried it, but must be doing something wrong.

image

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

Is <<: &tag_temp_corr placed wrongly?

Got it working. Apparently Yaml expects the anchor code to be indented:


input_number:
  tag_0001_temp_corr:
    name: "Temperatuur correctie 1"
    <<: &tag_temp_corr
      unit_of_measurement: "°C"
      min: -10
      max: 10
      step: 0.1
      icon: mdi:pencil-outline
      mode: slider