Skip to content

Instantly share code, notes, and snippets.

@koying
Last active September 14, 2021 06:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save koying/d622a0d7e055a8d383b420b4e22f501b to your computer and use it in GitHub Desktop.
Save koying/d622a0d7e055a8d383b420b4e22f501b to your computer and use it in GitHub Desktop.
Home assistant MQTT Statestream to MQTT Discovery blueprint (as of 2021.6)
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
@koying
Copy link
Author

koying commented Sep 14, 2021

Update for non-existing state_class

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment