Add Custom Sensor to existing Device

Hello everyone,

I bought a Tuya socket with consumption measurement. It works, but the power information is in the attributes and not as separate sensors. I have created sensors for the information I need. However, these are “deviceless” and only exist as entities. Is it possible to add sensors to existing devices, similar to how the BatteryNotes integration does it?

The socket is controlled via LocalTuya.
Sensor Code:

  - platform: template
    sensors:
      waschmaschine_power:
        friendly_name: "Waschmaschine Stromverbrauch"
        unique_id: "waschmaschine_power"
        entity_id: switch.waschmaschine
        value_template: "{{ state_attr('switch.waschmaschine', 'current_consumption') }}"
        unit_of_measurement: 'W'
        #device_class: apparent_power
      waschmaschine_status:
        friendly_name: "Waschmaschine Status"
        unique_id: "waschmaschine_status"
        value_template: >-
          {% set power = states('sensor.waschmaschine_power')|float %}
          {% if power == 0 %}
            aus
          {% elif power > 1 and power < 10 %}
            wartend
          {% elif power > 10 %}
            läuft
          {% endif %}

There’s a HACS integration that does this, but it’s still in the early stages, so use with care. I haven’t tried adding a sensor to an existing device, but I’ve used it to create “virtual devices” to group template sensors in the UI - that works well.

Edit: To get a template sensor to appear in the UI you have to assign it a unique id - for example:

  - sensor:
      - name: "Roof space battery level"
        unit_of_measurement: "%"
        unique_id: 763dbfbd-73e1-41b0-9404-2c4acf2b14bd
        state: '{{ state_attr("binary_sensor.roof_space_low_battery", "battery_level") | float(0) }}'
        device_class: battery

I’ve been getting my unique ids from here:

1 Like