Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {# @Author: Jacob Lindvig Henriksen (J-Lindvig) #}
- {# VAR #}
- {% set on_sensors = {
- "cd71f96a0d0e412bba41e91e3866ee07": ["Havelåge", "3C003A", "door"]
- } %}
- {% set on_off_sensors = {
- "4b03ffb918314900b4b59eed108c1960": ["Dobbelt terrassedør", "45400", "A", "E", "door"]
- } %}
- {# CON #}
- {% set state_topics = [
- "tele/kontor/RESULT",
- "tele/indgang/RESULT",
- "tele/pigernes_stue/RESULT"
- ] %}
- {% set state_topic = "tele/tasmota/RESULT" %}
- {% set demux_topic = "home/" %}
- {% set payload = "{{ trigger.payload_json.RfReceived.Data }}" %}
- {% set python = true %}
- {% set python_script = "python_script.rfbridge_demux" %}
- {% set off_delay = 15 %}
- {# PRIVATE BEGIN #}
- {# BANNER IN YAML WITH SIGNATURE #}
- {% set signature = "by J-Lindvig" %}
- {% set stamp = "Created: " ~ now().day ~ '/' ~ now().month ~ '/' ~ now().year %}
- {% set banner = [
- " __ _______ ____________ _____ ",
- " / |/ / __ \/_ __/_ __/ / ___/___ ____ _________ __________",
- " / /|_/ / / / / / / / /_____\__ \/ _ \/ __ \/ ___/ __ \/ ___/ ___/",
- " / / / / /_/ / / / / /_____/__/ / __/ / / (__ ) /_/ / / (__ ) ",
- "/_/ /_/\___\_\/_/ /_/ /____/\___/_/ /_/____/\____/_/ /____/ ",
- " "
- ] %}
- {%- macro print_banner() %}
- {{ "#".ljust(80, "#") }}
- {%- for line in banner %}
- {%- if not loop.last %}
- # {{ line.center(76) }} #
- {%- else %}
- # {{ (line[:-(signature | length)] ~ signature).center(76) }} #
- {% endif -%}
- {% endfor -%}
- {{ "#".ljust(80, "#") }}
- # {{ stamp.ljust(76," ") }} #
- {{ "#".ljust(80, "#") }}
- {%- endmacro %}
- {# PRIVATE END #}
- {# MACRO #}
- {% macro python_code() %}
- sensors = {
- {%- for sensor in on_sensors %}
- "{{ on_sensors[sensor][1] }}": ["{{ on_sensors[sensor][0] }}", "ON", "false"],
- {%- endfor %}
- {%- for sensor in on_off_sensors %}
- "{{ on_off_sensors[sensor][1] }}{{ on_off_sensors[sensor][2] }}": ["{{ on_off_sensors[sensor][0] }}", "ON", "true"],
- "{{ on_off_sensors[sensor][1] }}{{ on_off_sensors[sensor][3] }}": ["{{ on_off_sensors[sensor][0] }}", "OFF", "true"]{{ ',' if not loop.last }}
- {%- endfor %}
- }
- payload = data.get("payload")
- if payload is not None:
- if payload in sensors.keys():
- service_data = {"topic": "home/{}".format(sensors[payload][0]), "payload":"{}".format(sensors[payload][1]), "qos":0, "retain":"{}".format(sensors[payload][2])}
- else:
- service_data = {"topic":"home/unknown", "payload":"{}".format(payload), "qos":0, "retain":"false"}
- logger.warning("<rfbridge_demux> Received unknown RF command: {}".format(payload))
- hass.services.call("mqtt", "publish", service_data, False)
- {% endmacro %}
- {% macro private_device(sensor) -%}
- {% set entity_id = on_off_sensors[sensor][0] %}
- {%- set entity_id = entity_id.replace("Æ", "AE") %}
- {%- set entity_id = entity_id.replace("æ", "ae") %}
- {%- set entity_id = entity_id.replace("ø", "oe") %}
- {%- set entity_id = entity_id.replace("Ø", "OE") %}
- {%- set entity_id = entity_id.replace("å", "aa") %}
- {%- set entity_id = entity_id.replace("Å", "AA") %}
- {%- set entity_id = entity_id.replace(" ", "_") -%}
- {{ "PRIVATE "~entity_id if on_off_sensors[sensor][4] == "window" else entity_id}}
- {%- endmacro %}
- {%- macro entity_name(name) -%}
- {% set name = name.lower() %}
- {%- set name = name.replace("æ", "ae") %}
- {%- set name = name.replace("ø", "oe") %}
- {%- set name = name.replace("å", "aa") %}
- {%- set name = name.replace(" ", "_") -%}
- {{ name }}
- {%- endmacro %}
- {% macro sensor_value(sensor_id) -%}
- {{ "{{ states('"~sensor_id~"') }}" }}
- {%- endmacro %}
- {% macro sensor_icon(sensor_id) -%}
- {{ "mdi:window-{{ 'open' if is_state('"~sensor_id~"', 'on') else 'closed' }}-variant" }}
- {%- endmacro %}
- {{ print_banner() }}
- windows_doors:
- group:
- open_sensors:
- name: "Vinduer og døre med åbn sensor"
- icon: mdi:window-open-variant
- all: true
- entities:
- {%- for sensor in on_sensors %}
- {%- set entity_id = entity_name (on_sensors[sensor][0]) %}
- {%- set sensor_id = "binary_sensor.private_"~entity_id %}
- - binary_sensor.{{ entity_id }}
- {%- endfor %}
- open_close_sensors:
- name: "Vinduer og døre med åbn/luk sensor"
- icon: mdi:window-closed-variant
- all: true
- entities:
- {%- for sensor in on_off_sensors %}
- {%- set entity_id = entity_name (on_off_sensors[sensor][0]) %}
- {%- set sensor_id = "binary_sensor.private_"~entity_id %}
- - binary_sensor.{{ entity_id }}
- {%- endfor %}
- automation:
- - alias: "RF Bridge Demux"
- mode: parallel
- trigger:
- {%- for state_topic in state_topics %}
- - platform: mqtt
- topic: "{{ state_topic }}"
- {%- endfor %}
- action:
- - service: "{{ python_script }}"
- data:
- payload: "{{ payload }}"
- binary_sensor:
- {%- for sensor in on_sensors %}
- - platform: mqtt
- name: "{{ on_sensors[sensor][0] }}"
- unique_id: {{ sensor }}
- device_class: {{ on_sensors[sensor][2] }}
- state_topic: "{{ demux_topic }}{{ on_sensors[sensor][0] }}"
- off_delay: {{ off_delay }}
- {% endfor %}
- {%- for sensor in on_off_sensors %}
- - platform: mqtt
- name: "{{ private_device(sensor) }}"
- unique_id: {{ 'private_' if on_off_sensors[sensor][4] == "window"}}{{ sensor }}
- device_class: {{ on_off_sensors[sensor][4] }}
- state_topic: "{{ demux_topic }}{{ on_off_sensors[sensor][0] }}"
- {% endfor %}
- {%- for sensor in on_off_sensors if on_off_sensors[sensor][4] == "window" %}
- {% if loop.first %}
- - platform: template
- sensors:
- {% endif %}
- {%- set entity_id = entity_name (on_off_sensors[sensor][0]) %}
- {%- set sensor_id = "binary_sensor.private_"~entity_id %}
- {{ entity_id }}:
- friendly_name: "{{ on_off_sensors[sensor][0] }}"
- unique_id: {{ sensor }}
- device_class: window
- value_template: "{{ sensor_value(sensor_id) }}"
- icon_template: "{{ sensor_icon(sensor_id) }}"
- {% endfor %}
- script:
- reload_mqtt_sensors:
- alias: "Genindlæs MQTT sensorer"
- icon: mdi:reload
- description: "Genindlæs MQTT, Grupper, Templates og Scripts"
- mode: single
- sequence:
- - service: automation.reload
- - service: mqtt.reload
- - service: template.reload
- - service: group.reload
- {{ "#".ljust(80, "#") }}
- #### {{ "End of file".center(70) }} ####
- {{ "#".ljust(80, "#") }}
- {{ python_code() if python }}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement