Totally inspired by the work of @RobDYI in Automating the sharing of sensors and switches between HA's using MQTT Discovery and Statestream, this blueprint creates topics compatible with MQTT Discovery for the entries generated by MQTT Statestream.
It generates entries for sensors, binary_sensors, light and switches. Furthermore, it also manages a command topic for on/off handling of lights and switches.
Due to current limitations with blueprints and MQTT automation triggers, I cannot create a parameter for the base topic to use, so it is hardcoded to “ha_stream”, and thus the “base_topic” of your mqtt_statestream must be set to that value.
Update: As of 2021.3, we can use trigger variables, so you can now specify the base topic in the blueprint
Update 27/06/2021: Now publishes attributes as well
Update 27/06/2021: bugfix
Update 27/06/2021: Optimalisation (thanks.@123 ). Does not need statestream attributes anymore.
Update 08/07/2021: bugfix (thanks @MoTarek)
Update 14/09/2021: fix for non-existing state_class
Update 18/10/2021: DEPRECATED: See MQTT DiscoveryStream integration for the custom integration equivalent
Blueprint Code
Click the badge to import this Blueprint: (needs Home Assistant Core 2021.3 or higher)
Or import this Blueprint by using the forum topic / Gist URL:
blueprint:
name: MQTT Statestream to MQTT Discovery 2021.03
description: Creates an MQTT discovery entry for every entity streamed via MQTT Statestream.
domain: automation
input:
ha_stream:
name: HA stream
description: The HA statestream topic prefix used
mode: parallel
max: 50
trigger_variables:
base_topic: !input ha_stream
trigger:
- platform: mqtt
topic: "{{ base_topic ~ '/sensor/+/state' }}"
- platform: mqtt
topic: "{{ base_topic ~ '/binary_sensor/+/state' }}"
- platform: mqtt
topic: "{{ base_topic ~ '/light/+/state' }}"
- platform: mqtt
topic: "{{ base_topic ~ '/switch/+/state' }}"
- platform: mqtt
topic: "{{ base_topic ~ '/light/+/set' }}"
- platform: mqtt
topic: "{{ base_topic ~ '/light/+/set_bri' }}"
- platform: mqtt
topic: "{{ base_topic ~ '/switch/+/set' }}"
action:
- variables:
t: "{{ trigger.topic.split('/') }}"
root: "{{ t[0] }}"
domain: "{{ t[1] }}"
entity: "{{ t[2] }}"
element: "{{ t[3] }}"
- choose:
# Create Sensor discovery config
- conditions:
- condition: template
value_template: "{{ domain == 'sensor' and element == 'state' and 'device_class' in states[domain + '.' + entity].attributes }}"
sequence:
- service: mqtt.publish
data_template:
topic: "{{ root }}/{{ domain }}/{{ entity }}/attributes"
payload: '{
{% for att in states[domain + "."+ entity].attributes %}
"{{ att }}": "{{ state_attr(domain + "."+ entity, att) }}",
{% endfor %}
"mqttstream2discover": "true"
}'
retain: true
- service: mqtt.publish
data_template:
topic: "{{ root }}/{{ domain }}/{{ entity }}/config"
payload: '{
"uniq_id": "mqtt_{{domain }}_{{ entity }}",
"name": "{{ entity| replace(''_'', '' '') | title }}",
"dev_cla": "{{ state_attr(domain + ''.'' + entity, "device_class") }}",
{% if state_attr(domain + ''.'' + entity, "state_class") != none %}
"stat_cla": "{{ state_attr(domain + ''.'' + entity, "state_class") }}",
{% endif %}
"unit_of_meas": "{{ state_attr(domain + ''.'' + entity, "unit_of_measurement") }}",
"stat_t": "{{ root }}/{{ domain }}/{{ entity }}/state",
"json_attr_t": "{{ root }}/{{ domain }}/{{ entity }}/attributes"
}'
retain: true
# Create Binary Sensor discovery config
- conditions:
- condition: template
value_template: "{{ domain == 'binary_sensor' and element == 'state' and 'device_class' in states[domain + '.' + entity].attributes }}"
sequence:
- service: mqtt.publish
data_template:
topic: "{{ root }}/{{ domain }}/{{ entity }}/attributes"
payload: '{
{% for att in states[domain + "."+ entity].attributes %}
"{{ att }}": "{{ state_attr(domain + "."+ entity, att) }}",
{% endfor %}
"mqttstream2discover": "true"
}'
retain: true
- service: mqtt.publish
data_template:
topic: "{{ root }}/{{ domain }}/{{ entity }}/config"
payload: '{
"uniq_id": "mqtt_{{domain }}_{{ entity }}",
"name": "{{ entity| replace(''_'', '' '') | title }}",
"dev_cla": "{{ state_attr(domain + ''.'' + entity, "device_class") }}",
"pl_off":"off", "pl_on":"on",
"state_topic": "{{ root }}/{{ domain }}/{{ entity }}/state",
"json_attr_t": "{{ root }}/{{ domain }}/{{ entity }}/attributes"
}'
retain: true
# Create Switch discovery config
- conditions:
- condition: template
value_template: "{{
domain == 'switch' and
element == 'state' }}"
sequence:
- service: mqtt.publish
data_template:
topic: "{{ root }}/{{ domain }}/{{ entity }}/attributes"
payload: '{
{% for att in states[domain + "."+ entity].attributes %}
"{{ att }}": "{{ state_attr(domain + "."+ entity, att) }}",
{% endfor %}
"mqttstream2discover": "true"
}'
retain: true
- service: mqtt.publish
data_template:
topic: "{{ root }}/{{ domain }}/{{ entity }}/config"
payload: '{
"uniq_id": "mqtt_{{domain }}_{{ entity }}",
"name": "{{ entity| replace(''_'', '' '') | title }}",
"pl_off":"off", "pl_on":"on",
"state_topic": "{{ root }}/{{ domain }}/{{ entity }}/state",
"json_attr_t": "{{ root }}/{{ domain }}/{{ entity }}/attributes",
"command_topic": "{{ root }}/{{ domain }}/{{ entity }}/set"
}'
retain: true
# Create Light discovery config
- conditions:
- condition: template
value_template: "{{
domain == 'light' and
element == 'state' }}"
sequence:
- service: mqtt.publish
data_template:
topic: "{{ root }}/{{ domain }}/{{ entity }}/attributes"
payload: '{
{% for att in states[domain + "."+ entity].attributes %}
"{{ att }}": "{{ state_attr(domain + "."+ entity, att) }}",
{% endfor %}
"mqttstream2discover": "true"
}'
retain: true
- service: mqtt.publish
data_template:
topic: "{{ root }}/{{ domain }}/{{ entity }}/config"
payload: '{
"uniq_id": "mqtt_{{domain }}_{{ entity }}",
"name": "{{ entity| replace(''_'', '' '') | title }}",
"pl_off":"off", "pl_on":"on",
"bri_stat_t": "{{ root }}/{{ domain }}/{{ entity }}/brightness",
"bri_cmd_t": "{{ root }}/{{ domain }}/{{ entity }}/set_bri",
"state_topic": "{{ root }}/{{ domain }}/{{ entity }}/state",
"json_attr_t": "{{ root }}/{{ domain }}/{{ entity }}/attributes",
"command_topic": "{{ root }}/{{ domain }}/{{ entity }}/set"
}'
retain: true
# Command for Switch & Light
- conditions:
- condition: template
value_template: "{{
(domain == 'light' or domain == 'switch') and
element == 'set' }}"
sequence:
- service: '{{ domain }}.turn_{{trigger.payload | lower }}'
data_template:
entity_id: '{{ domain }}.{{ entity }}'
# Command for Light brightness
- conditions:
- condition: template
value_template: "{{
domain == 'light' and
element == 'set_bri' }}"
sequence:
- service: 'light.turn_on'
data_template:
entity_id: '{{ domain }}.{{ entity }}'
brightness: '{{trigger.payload | int }}'
default:
- event: noop