Advertisement
JLindvig

MQTT generator

Oct 25th, 2020 (edited)
2,707
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
YAML 6.47 KB | None | 0 0
  1. {# @Author: Jacob Lindvig Henriksen (J-Lindvig) #}
  2.  
  3. {# VAR #}
  4. {% set on_sensors = {
  5.   "cd71f96a0d0e412bba41e91e3866ee07": ["Havelåge", "3C003A", "door"]
  6. } %}
  7.  
  8. {% set on_off_sensors = {
  9.   "4b03ffb918314900b4b59eed108c1960": ["Dobbelt terrassedør", "45400", "A", "E", "door"]
  10. } %}
  11.  
  12. {# CON #}
  13. {% set state_topics = [
  14.   "tele/kontor/RESULT",
  15.   "tele/indgang/RESULT",
  16.   "tele/pigernes_stue/RESULT"
  17. ] %}
  18. {% set state_topic = "tele/tasmota/RESULT" %}
  19. {% set demux_topic = "home/" %}
  20. {% set payload = "{{ trigger.payload_json.RfReceived.Data }}" %}
  21. {% set python = true %}
  22. {% set python_script = "python_script.rfbridge_demux" %}
  23. {% set off_delay = 15 %}
  24.  
  25. {# PRIVATE BEGIN #}
  26. {# BANNER IN YAML WITH SIGNATURE #}
  27. {% set signature = "by J-Lindvig" %}
  28. {% set stamp = "Created: " ~ now().day  ~ '/' ~ now().month ~ '/' ~ now().year %}
  29. {% set banner = [
  30.   "    __  _______  ____________   _____                                ",
  31.   "   /  |/  / __ \/_  __/_  __/  / ___/___  ____  _________  __________",
  32.   "  / /|_/ / / / / / /   / /_____\__ \/ _ \/ __ \/ ___/ __ \/ ___/ ___/",
  33.   " / /  / / /_/ / / /   / /_____/__/ /  __/ / / (__  ) /_/ / /  (__  ) ",
  34.   "/_/  /_/\___\_\/_/   /_/     /____/\___/_/ /_/____/\____/_/  /____/  ",
  35.   "                                                                     "
  36.   ] %}
  37.  
  38. {%- macro print_banner() %}
  39. {{ "#".ljust(80, "#") }}
  40. {%- for line in banner %}
  41. {%- if not loop.last %}
  42. # {{ line.center(76) }} #
  43. {%- else %}
  44. # {{ (line[:-(signature | length)] ~ signature).center(76) }} #
  45. {% endif -%}
  46. {% endfor -%}
  47. {{ "#".ljust(80, "#") }}
  48. # {{ stamp.ljust(76," ") }} #
  49. {{ "#".ljust(80, "#") }}
  50. {%- endmacro %}
  51. {# PRIVATE END #}
  52.  
  53. {# MACRO #}
  54. {% macro python_code() %}
  55. sensors = {
  56. {%- for sensor in on_sensors %}
  57.   "{{ on_sensors[sensor][1] }}": ["{{ on_sensors[sensor][0] }}", "ON", "false"],
  58. {%- endfor %}
  59. {%- for sensor in on_off_sensors %}
  60.   "{{ on_off_sensors[sensor][1] }}{{ on_off_sensors[sensor][2] }}": ["{{ on_off_sensors[sensor][0] }}", "ON", "true"],
  61.   "{{ on_off_sensors[sensor][1] }}{{ on_off_sensors[sensor][3] }}": ["{{ on_off_sensors[sensor][0] }}", "OFF", "true"]{{ ',' if not loop.last }}
  62. {%- endfor %}
  63. }
  64. payload = data.get("payload")
  65.  
  66. if payload is not None:
  67.   if payload in sensors.keys():
  68.     service_data = {"topic": "home/{}".format(sensors[payload][0]), "payload":"{}".format(sensors[payload][1]), "qos":0, "retain":"{}".format(sensors[payload][2])}
  69.   else:
  70.    service_data = {"topic":"home/unknown", "payload":"{}".format(payload), "qos":0, "retain":"false"}
  71.     logger.warning("<rfbridge_demux> Received unknown RF command: {}".format(payload))
  72.  hass.services.call("mqtt", "publish", service_data, False)
  73. {% endmacro %}
  74.  
  75. {% macro private_device(sensor) -%}
  76.  {% set entity_id = on_off_sensors[sensor][0] %}
  77.  {%- set entity_id = entity_id.replace("Æ", "AE") %}
  78.  {%- set entity_id = entity_id.replace("æ", "ae") %}
  79.  {%- set entity_id = entity_id.replace("ø", "oe") %}
  80.  {%- set entity_id = entity_id.replace("Ø", "OE") %}
  81.  {%- set entity_id = entity_id.replace("å", "aa") %}
  82.  {%- set entity_id = entity_id.replace("Å", "AA") %}
  83.  {%- set entity_id = entity_id.replace(" ", "_") -%}
  84.  {{ "PRIVATE "~entity_id if on_off_sensors[sensor][4] == "window" else entity_id}}
  85. {%- endmacro %}
  86.  
  87. {%- macro entity_name(name) -%}
  88.  {% set name = name.lower() %}
  89.  {%- set name = name.replace("æ", "ae") %}
  90.  {%- set name = name.replace("ø", "oe") %}
  91.  {%- set name = name.replace("å", "aa") %}
  92.  {%- set name = name.replace(" ", "_") -%}
  93.        {{ name }}
  94. {%- endmacro %}
  95.  
  96. {% macro sensor_value(sensor_id) -%}
  97.  {{ "{{ states('"~sensor_id~"') }}" }}
  98. {%- endmacro %}
  99.  
  100. {% macro sensor_icon(sensor_id) -%}
  101.  {{ "mdi:window-{{ 'open' if is_state('"~sensor_id~"', 'on') else 'closed' }}-variant" }}
  102. {%- endmacro %}
  103. {{ print_banner() }}
  104. windows_doors:
  105.  group:
  106.    open_sensors:
  107.      name: "Vinduer og døre med åbn sensor"
  108.      icon: mdi:window-open-variant
  109.      all: true
  110.      entities:
  111. {%- for sensor in on_sensors %}
  112.  {%- set entity_id = entity_name (on_sensors[sensor][0]) %}
  113.  {%- set sensor_id = "binary_sensor.private_"~entity_id %}
  114.        - binary_sensor.{{ entity_id }}
  115. {%- endfor %}
  116.  
  117.    open_close_sensors:
  118.      name: "Vinduer og døre med åbn/luk sensor"
  119.      icon: mdi:window-closed-variant
  120.      all: true
  121.      entities:
  122. {%- for sensor in on_off_sensors %}
  123.  {%- set entity_id = entity_name (on_off_sensors[sensor][0]) %}
  124.  {%- set sensor_id = "binary_sensor.private_"~entity_id %}
  125.        - binary_sensor.{{ entity_id }}
  126. {%- endfor %}
  127.  
  128.  automation:
  129.    - alias: "RF Bridge Demux"
  130.      mode: parallel
  131.      trigger:
  132. {%- for state_topic in state_topics %}
  133.        - platform: mqtt
  134.          topic: "{{ state_topic }}"
  135. {%- endfor %}
  136.      action:
  137.        - service: "{{ python_script }}"
  138.          data:
  139.            payload: "{{ payload }}"
  140.  
  141.  binary_sensor:
  142. {%- for sensor in on_sensors %}
  143.    - platform: mqtt
  144.      name: "{{ on_sensors[sensor][0] }}"
  145.      unique_id: {{ sensor }}
  146.      device_class: {{ on_sensors[sensor][2] }}
  147.      state_topic: "{{ demux_topic }}{{ on_sensors[sensor][0] }}"
  148.      off_delay: {{ off_delay }}
  149. {% endfor %}
  150. {%- for sensor in on_off_sensors %}
  151.    - platform: mqtt
  152.      name: "{{ private_device(sensor) }}"
  153.      unique_id: {{ 'private_' if on_off_sensors[sensor][4] == "window"}}{{ sensor }}
  154.      device_class: {{ on_off_sensors[sensor][4] }}
  155.      state_topic: "{{ demux_topic }}{{ on_off_sensors[sensor][0] }}"
  156. {% endfor %}
  157.  
  158. {%- for sensor in on_off_sensors if on_off_sensors[sensor][4] == "window" %}
  159.  {% if loop.first %}
  160.    - platform: template
  161.      sensors:
  162.  {% endif %}
  163.  {%- set entity_id = entity_name (on_off_sensors[sensor][0]) %}
  164.  {%- set sensor_id = "binary_sensor.private_"~entity_id %}
  165.        {{ entity_id }}:
  166.          friendly_name: "{{ on_off_sensors[sensor][0] }}"
  167.          unique_id: {{ sensor }}
  168.          device_class: window
  169.          value_template: "{{ sensor_value(sensor_id) }}"
  170.          icon_template: "{{ sensor_icon(sensor_id) }}"
  171. {% endfor %}
  172.  script:
  173.    reload_mqtt_sensors:
  174.      alias: "Genindlæs MQTT sensorer"
  175.      icon: mdi:reload
  176.      description: "Genindlæs MQTT, Grupper, Templates og Scripts"
  177.      mode: single
  178.      sequence:
  179.        - service: automation.reload
  180.        - service: mqtt.reload
  181.        - service: template.reload
  182.        - service: group.reload
  183.  
  184. {{ "#".ljust(80, "#") }}
  185. #### {{ "End of file".center(70) }} ####
  186. {{ "#".ljust(80, "#") }}
  187.  
  188. {{ python_code() if python }}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement