It’s entirely possible. If you’re just creating sensors, you can use the scripts I created in combination with an automation. It’s only built for timestamp sensors but any of the device_classes would work and adding unit_of_measurement wouldn’t be hard.
Script
mqtt_automated_states:
sequence:
- variables: &mqtt-variables
root: "homeassistant"
topic_root: >
{%- if domain is not defined or unique_id is not defined %}
{{- [ root, 'error'] | join('/') }}
{%- else %}
{{- [ root, domain, unique_id ] | join('/') }}
{%- endif %}
- service: mqtt.publish
data:
topic: "{{ topic_root ~ '/state' }}"
payload: >
{{ { 'state': state, 'attributes': attributes | default({}) } | tojson }}
retain: "{{ retain | default('true') }}"
mqtt_automated_config:
sequence:
- variables:
name: >
{{ unique_id | replace('_', ' ') | title }}
<<: *mqtt-variables
payload: >
{%- set items = [
( "name", name),
( "unique_id", unique_id),
( "state_topic", topic_root ~ "/state"),
( "value_template", "{{ value_json.state }}"),
( "json_attributes_topic", topic_root ~ "/state"),
( "json_attributes_template", "{{ value_json.attributes | tojson }}"),
( "device_class", device_class ),
] %}
{% set ns = namespace(items=[]) %}
{%- for k, v in items %}
{%- if v is defined %}
{%- set ns.items = ns.items + [ '"{}": "{}"'.format(k, v) ] %}
{%- endif %}
{%- endfor %}
{{ ("{" ~ ns.items | join(', ') ~ "}") }}
- service: mqtt.publish
data:
topic: "{{ topic_root ~ '/config' }}"
payload: "{{ payload | tojson }}"
use
On startup this creates discovery for a bunch of door timestamps so I know the last time a door opened. I’m just providing it with unique_id and the rest is set up through the defaults in the script. YOu can change the device class to temperature or anything else. Or adjust the script above for unit_of_measurement (which I can help you with) and add a unit of measurement.
- alias: Doors - MQTT Discovery on Startup
id: mqtt_store_door_states_discovery
mode: single
trigger:
- platform: homeassistant
event: start
variables:
doors:
# - test_door_last_opened
- main_door_last_opened
- sliding_door_last_opened
- garage_door_tilt_last_opened
- garage_entry_door_last_opened
- rear_garage_door_tilt_last_opened
action:
- repeat:
count: "{{ doors | length }}"
sequence:
- service: script.mqtt_automated_config
data:
domain: sensor
unique_id: "{{ doors[repeat.index - 1] }}"
device_class: timestamp
Then if you want to update the sensor from HA instead of the sensor itself you can use this service:
- service: script.mqtt_automated_states
data:
domain: sensor
unique_id: "{{ unique_id }}"
state: "{{ timestamp }}"
attributes: "{{ { 'who': who, 'count': count, 'source': source, 'count_timestamp': count_timestamp } | tojson }}"
attributes can be a yaml dictionary and you just need to provide the proper unique ID that you set on startup.