Moving custom sensor to MQTT sensor for 2022.12 upgrade

I have created a sensor that warns me when an entity becomes unavailable.
It shows the count of the missing entities, as well as the list of the names.
It’s useful when you have a fairly sized network, with some unreliable devices (Aquara!).

The config panel informs me that:

Manually configured MQTT sensor(s) found under platform key sensor.
Please move the configuration to the mqtt integration key and restart Home Assistant to fix this issue. See the documentation, for more information.

Of course i tried to port the sensor to the new MQTT sensor syntax, however they’re not 1-to-1 compatible. Specifically, there’s no attribute_templates. Is this replaced by json_attribute_topic?
Also, what should I use as state_topic?

Here’s how it’s configured:

sensor:
  - platform: template
    sensors:
      unavailable_entities:
        friendly_name: Unavailable Entities
        unit_of_measurement: entities
        icon_template: "{{ 'mdi:check-circle' if is_state('sensor.unavailable_entities','0') else 'mdi:alert-circle' }}"
        value_template: >
          {{ states|selectattr('state','in',['unavailable','unknown','none'])
            |rejectattr('domain','eq','group')
            |map(attribute='entity_id')
            |select('match','^sensor')
            |reject('match','.*charger_type$')
            |reject('match','.*zigbee2mqtt_networkmap$')
            |reject('match','.*sht_w')
            |list|count }}
        attribute_templates:
          entities: >
            {{ states|selectattr('state','in',['unavailable','unknown','none'])
              |rejectattr('domain','eq','group')
              |map(attribute='entity_id')
              |select('match','^sensor')
              |reject('match','.*charger_type$')
              |reject('match','.*zigbee2mqtt_networkmap$')
              |reject('match','.*sht_w')
              |list }}

Thanks in advance for any help.

That isn’t a MQTT sensor.

That is a legacy template sensor. I don’t think the legacy format requires changing atm.

Thanks for the tip, @HasQT.
Here’s the updated configuration, should anyone need it in the future:

template:
  - sensor:
      - name: "unavailable entities"
        unique_id: "unavailable_entities"
        unit_of_measurement: entities
        icon: "{{ 'mdi:check-circle' if is_state('sensor.unavailable_entities','0') else 'mdi:alert-circle' }}"
        state: >
          {{ states|selectattr('state','in',['unavailable','unknown','none'])
            |rejectattr('domain','eq','group')
            |map(attribute='entity_id')
            |select('match','^sensor')
            |reject('match','.*zigbee2mqtt_networkmap$')
            |reject('match','.*sht_w')
            |list|count }}
        attributes:
          entities: >
              {{ states|selectattr('state','in',['unavailable','unknown','none'])
              |rejectattr('domain','eq','group')
              |map(attribute='entity_id')
              |select('match','^sensor')
              |reject('match','.*zigbee2mqtt_networkmap$')
              |reject('match','.*sht_w')
              |list }}
1 Like