Skip to content

Instantly share code, notes, and snippets.

@dshanske
Forked from koying/mqttstream2discover.yaml
Last active June 20, 2021 22:20
Show Gist options
  • Save dshanske/82c09aa76f1e13f79e7193979220b55b to your computer and use it in GitHub Desktop.
Save dshanske/82c09aa76f1e13f79e7193979220b55b to your computer and use it in GitHub Desktop.
Home assistant MQTT Statestream to MQTT Discovery blueprint - Modified to use 2 different topics
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/+/device_class' }}"
- platform: mqtt
topic: "{{ base_topic ~ '/binary_sensor/+/device_class' }}"
- 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:
- choose:
# Create Sensor discovery config
- conditions:
- condition: template
value_template: "{{ trigger.topic.split('/')[1] == 'sensor' and trigger.topic.split('/')[3] == 'device_class' }}"
sequence:
- service: mqtt.publish
data_template:
topic: "homeassistant/{{ trigger.topic.split('/')[1] }}/{{ trigger.topic.split('/')[2] }}/config"
payload: '{
"uniq_id": "mqtt_{{trigger.topic.split(''/'')[1] }}_{{ trigger.topic.split(''/'')[2] }}",
"name": "{{ trigger.topic.split(''/'')[2]| replace(''_'', '' '') | title }}",
"dev_cla": {{ trigger.payload }},
"unit_of_meas": "{{ state_attr(''sensor.''+ trigger.topic.split(''/'')[2], "unit_of_measurement") }}",
"state_topic": "{{trigger.topic.split(''/'')[0] }}/{{trigger.topic.split(''/'')[1] }}/{{ trigger.topic.split(''/'')[2] }}/state"
}'
retain: true
# Create Binary Sensor discovery config
- conditions:
- condition: template
value_template: "{{ trigger.topic.split('/')[1] == 'binary_sensor' and trigger.topic.split('/')[3] == 'device_class' }}"
sequence:
- service: mqtt.publish
data_template:
topic: "homeassistant/{{ trigger.topic.split('/')[1] }}/{{ trigger.topic.split('/')[2] }}/config"
payload: '{
"uniq_id": "mqtt_{{trigger.topic.split(''/'')[1] }}_{{ trigger.topic.split(''/'')[2] }}",
"name": "{{ trigger.topic.split(''/'')[2]| replace(''_'', '' '') | title }}",
"dev_cla": {{ trigger.payload }},
"pl_off":"off", "pl_on":"on",
"state_topic": "{{trigger.topic.split(''/'')[0] }}/{{trigger.topic.split(''/'')[1] }}/{{ trigger.topic.split(''/'')[2] }}/state"
}'
retain: true
# Create Switch discovery config
- conditions:
- condition: template
value_template: "{{
trigger.topic.split('/')[1] == 'switch' and
trigger.topic.split('/')[3] == 'state' }}"
sequence:
- service: mqtt.publish
data_template:
topic: "homeassistant/{{ trigger.topic.split('/')[1] }}/{{ trigger.topic.split('/')[2] }}/config"
payload: '{
"uniq_id": "mqtt_{{trigger.topic.split(''/'')[1] }}_{{ trigger.topic.split(''/'')[2] }}",
"name": "{{ trigger.topic.split(''/'')[2]| replace(''_'', '' '') | title }}",
"pl_off":"off", "pl_on":"on",
"state_topic": "{{trigger.topic.split(''/'')[0] }}/{{trigger.topic.split(''/'')[1] }}/{{ trigger.topic.split(''/'')[2] }}/state",
"command_topic": "{{trigger.topic.split(''/'')[0] }}/{{trigger.topic.split(''/'')[1] }}/{{ trigger.topic.split(''/'')[2] }}/set"
}'
retain: true
# Create Light discovery config
- conditions:
- condition: template
value_template: "{{
trigger.topic.split('/')[1] == 'light' and
trigger.topic.split('/')[3] == 'state' }}"
sequence:
- service: mqtt.publish
data_template:
topic: "homeassistant/{{ trigger.topic.split('/')[1] }}/{{ trigger.topic.split('/')[2] }}/config"
payload: '{
"uniq_id": "mqtt_{{trigger.topic.split(''/'')[1] }}_{{ trigger.topic.split(''/'')[2] }}",
"name": "{{ trigger.topic.split(''/'')[2]| replace(''_'', '' '') | title }}",
"pl_off":"off", "pl_on":"on",
"bri_stat_t": "{{trigger.topic.split(''/'')[0] }}/{{trigger.topic.split(''/'')[1] }}/{{ trigger.topic.split(''/'')[2] }}/brightness",
"bri_cmd_t": "{{trigger.topic.split(''/'')[0] }}/{{trigger.topic.split(''/'')[1] }}/{{ trigger.topic.split(''/'')[2] }}/set_bri",
"state_topic": "{{trigger.topic.split(''/'')[0] }}/{{trigger.topic.split(''/'')[1] }}/{{ trigger.topic.split(''/'')[2] }}/state",
"command_topic": "{{trigger.topic.split(''/'')[0] }}/{{trigger.topic.split(''/'')[1] }}/{{ trigger.topic.split(''/'')[2] }}/set"
}'
retain: true
# Command for Switch & Light
- conditions:
- condition: template
value_template: "{{
(trigger.topic.split('/')[1] == 'light' or trigger.topic.split('/')[1] == 'switch') and
trigger.topic.split('/')[3] == 'set' }}"
sequence:
- service: '{{ trigger.topic.split(''/'')[1] }}.turn_{{trigger.payload | lower }}'
data_template:
entity_id: '{{ trigger.topic.split(''/'')[1] }}.{{ trigger.topic.split(''/'')[2] }}'
# Command for Light brightness
- conditions:
- condition: template
value_template: "{{
trigger.topic.split('/')[1] == 'light' and
trigger.topic.split('/')[3] == 'set_bri' }}"
sequence:
- service: 'light.turn_on'
data_template:
entity_id: '{{ trigger.topic.split(''/'')[1] }}.{{ trigger.topic.split(''/'')[2] }}'
brightness: '{{trigger.payload | int }}'
default:
- event: noop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment