JLindvig

MQTT generator

Oct 20th, 2020
715
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. {# VAR #}
  2. {% set on_sensors = {
  3.   "cd71f96a0d0e412bba41e91e3866ee07": ["Havelåge", "3C003A", "door"],
  4.   "f1fe358686d14a9f916ffaf77d7d2fde": ["Hoveddør", "C0E3DA", "door"]
  5. } %}
  6.  
  7. {% set on_off_sensors = {
  8.   "4b03ffb918314900b4b59eed108c1960": ["Dobbelt terrassesdør", "45400", "A", "E", "door"],
  9.   "48d246efd72c4485afca819baa59051b": ["Soveværelse vindue", "B1C40", "A", "E", "window"],
  10.   "7e3185c4aef2445a981583b736273364": ["Pigernes stue vindue", "AFF70", "A", "E", "window"],
  11.   "3ff715ed48204fce967b67fdc70fa692": ["Nathalies vindue", "B2B10", "A", "E", "window"],
  12.   "2a3dec1038084981b6f786b54a28c17b": ["Cornelies vindue", "181F0", "A", "E", "window"]
  13. } %}
  14.  
  15. {# CON #}
  16. {% set state_topic = "tele/tasmota/RESULT" %}
  17. {% set value_template = "{{ value_json.RfReceived.Data }}" %}
  18. {% set off_delay = 15 %}
  19.  
  20. {# MACRO #}
  21. {% macro private_device(sensor) -%}
  22.   {{ "PRIVATE "~on_off_sensors[sensor][0] if on_off_sensors[sensor][4] == "window" else on_off_sensors[sensor][0]}}
  23. {%- endmacro %}
  24.  
  25. {%- macro entity_name(name) -%}
  26.   {% set name = name.lower() %}
  27.   {%- set name = name.replace("æ", "a") %}
  28.   {%- set name = name.replace("ø", "o") %}
  29.   {%- set name = name.replace("å", "a") %}
  30.   {%- set name = name.replace(" ", "_") -%}
  31.         {{ name }}
  32. {%- endmacro %}
  33.  
  34. {% macro sensor_value(sensor_id) -%}
  35.   {{ "{{ states('"~sensor_id~"') }}" }}
  36. {%- endmacro %}
  37.  
  38. {% macro sensor_icon(sensor_id) -%}
  39.   {{ "mdi:window-{{ 'open' if is_state('"~sensor_id~"', 'on') else 'closed' }}-variant" }}
  40. {%- endmacro %}
  41.  
  42. windows_doors:
  43.   group:
  44.     open_close_sensors:
  45.       name: "Vinduer og døre med åbn/luk sensor"
  46.       icon: mdi:window-closed-variant
  47.       all: true
  48.       entities:
  49. {%- for sensor in on_off_sensors %}
  50.   {%- set entity_id = entity_name (on_off_sensors[sensor][0]) %}
  51.   {%- set sensor_id = "binary_sensor.private_"~entity_id %}
  52.         - binary_sensor.{{ entity_id }}
  53. {%- endfor %}
  54.  
  55.   binary_sensor:
  56. {%- for sensor in on_sensors %}
  57.     - platform: mqtt
  58.       name: "{{ on_sensors[sensor][0] }}"
  59.       unique_id: {{ sensor }}
  60.       device_class: {{ on_sensors[sensor][2] }}
  61.       state_topic: "{{ state_topic }}"
  62.       value_template: "{{ value_template }}"
  63.       payload_on: "{{ on_sensors[sensor][1] }}"
  64.       off_delay: {{ off_delay }}
  65. {% endfor %}
  66. {%- for sensor in on_off_sensors %}
  67.     - platform: mqtt
  68.       name: "{{ private_device(sensor) }}"
  69.       unique_id: {{ 'private_' if on_off_sensors[sensor][4] == "window"}}{{ sensor }}
  70.       device_class: {{ on_off_sensors[sensor][4] }}
  71.       state_topic: "{{ state_topic }}"
  72.       value_template: "{{ value_template }}"
  73.       payload_on: "{{ on_off_sensors[sensor][1] }}{{ on_off_sensors[sensor][2] }}"
  74.       payload_off: "{{ on_off_sensors[sensor][1] }}{{ on_off_sensors[sensor][3] }}"
  75. {% endfor %}
  76.     - platform: template
  77.       sensors:
  78. {%- for sensor in on_off_sensors if on_off_sensors[sensor][4] == "window" %}
  79.   {%- set entity_id = entity_name (on_off_sensors[sensor][0]) %}
  80.   {%- set sensor_id = "binary_sensor.private_"~entity_id %}
  81.         {{ entity_id }}:
  82.           friendly_name: "{{ on_off_sensors[sensor][0] }}"
  83.           unique_id: {{ sensor }}
  84.           device_class: window
  85.           value_template: "{{ sensor_value(sensor_id) }}"
  86.           icon_template: "{{ sensor_icon(sensor_id) }}"
  87. {% endfor %}
RAW Paste Data