Last active
May 26, 2021 21:05
-
-
Save SteveDinn/3985f5a2b80afdcbabdcbb2acffcb94c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
automation: | |
- id: rf_signal_receiver | |
alias: rf_signal_receiver | |
trigger: | |
- platform: mqtt | |
topic: tele/RFBridge/RESULT | |
condition: | |
- condition: template | |
value_template: > | |
{{ trigger.payload_json["RfReceived"] is not none }} | |
- condition: template | |
value_template: > | |
{{ trigger.payload_json["RfReceived"]["Data"] is not none }} | |
action: | |
- event: rf_signal_received | |
event_data_template: | |
code: > | |
{{ trigger.payload_json.RfReceived.Data }} | |
- alias: rf_door_state | |
trigger: | |
- platform: event | |
event_type: rf_signal_received | |
condition: | |
- condition: template | |
value_template: > | |
{%- set operationCodes = ['A', 'E', '6', '7'] -%} | |
{%- set operation = trigger.event.data.code[-1:] -%} | |
{{ operation in operationCodes }} | |
action: | |
- service_template: > | |
{%- set operations = [ 'A', 'E' ] -%} | |
{%- set operation = trigger.event.data.code[-1:] -%} | |
script.{{ 'rf_door_openclose' if (operation in operations) else 'rf_door_problem' }} | |
data_template: | |
state: > | |
{%- set door_code = trigger.event.data.code[:-1] -%} | |
{%- set operation = trigger.event.data.code[-1:] -%} | |
{%- set operations = { | |
'A': 'open', | |
'E': 'close' | |
} -%} | |
{%- set problems = { | |
'6': 'low battery', | |
'7': 'intrusion' | |
} -%} | |
{%- set doors = { | |
"AAAAA": { "code": "AAAAA", "name": "Back door", "topic": "BackDoor" }, | |
"BBBBB": { "code": "BBBBB", "name": "Ensuite door", "topic": "EnsuiteDoor" }, | |
"CCCCC": { "code": "CCCCC", "name": "Office door", "topic": "OfficeDoor" }, | |
"DDDDD": { "code": "DDDDD", "name": "Bedroom 1 door", "topic": "Bedroom1Door" }, | |
"EEEEE": { "code": "EEEEE", "name": "Hallway door", "topic": "HallwayDoor" }, | |
"FFFFF": { "code": "FFFFF", "name": "Bedroom 2 door", "topic": "Bedroom2Door" }, | |
"11111": { "code": "11111", "name": "Garage freezer", "topic": "GarageFreezer" } | |
} -%} | |
{{ | |
{ | |
'door': (doors[door_code] if (door_code in doors) else ''), | |
'operation': (operations[operation] if (operation in operations) else ''), | |
'problem': (problems[operation] if (operation in problems) else '') | |
} | to_json | |
}} | |
script: | |
rf_door_openclose: | |
alias: rf_door_openclose | |
sequence: | |
- condition: template | |
value_template: > | |
{%- set data = state | from_json -%} | |
{{ (data.door != '') and (data.operation != '') }} | |
- service: mqtt.publish | |
data_template: | |
topic: > | |
{%- set data = state | from_json -%} | |
{{ data.door.topic }}/STATE | |
payload: > | |
{%- set data = state | from_json -%} | |
{{ 'ON' if (data.operation | lower == 'open') else 'OFF' }} | |
retain: true | |
rf_door_problem: | |
sequence: | |
- condition: template | |
value_template: > | |
{%- set data = state | from_json -%} | |
{{ (data.door != '') and (data.problem != '') }} | |
- service: rest_command.post_slack_steve | |
data_template: | |
message: > | |
{%- set data = state | from_json -%} | |
{{ data.door.name }} is experiencing {{ data.problem }} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
some time ago.. ;)
I saw it at the time of posting but came back to read carefully just this week when decided to rewrite my code handling PIRs/smoke detectors - they were based on input_boolean and very seldom remained on, which is pretty annoying if it happens when you're about to set the alarm before leaving the house.
yes, scripts are special here - you can easily have multiple instances of python_script or the same automation can run in parallel.
I rarely use scripts, think the only one runs at startup and is just a sequence of commands, just to make the start code more clear.
I use events if there is a common piece of code that should be accessible from various automations - that way you can pass parameters, which is not possible with automations.
I've just finished rewriting my code and hope to publish it soon ;)