Skip to content

Instantly share code, notes, and snippets.

@SteveDinn
Last active May 26, 2021 21:05
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save SteveDinn/3985f5a2b80afdcbabdcbb2acffcb94c to your computer and use it in GitHub Desktop.
Save SteveDinn/3985f5a2b80afdcbabdcbb2acffcb94c to your computer and use it in GitHub Desktop.
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 }}
@akasma74
Copy link

akasma74 commented Dec 3, 2019

Steve, wouldn't it be better to rewrite 2 template conditions (strings 8-13) as one?
Also, maybe instead of this

trigger.payload_json["RfReceived"] is not none

it's better to do this

trigger.payload_json.RfReceived is defined 

?

@SteveDinn
Copy link
Author

Funny you should say that, because I did exactly that last week. I don't like using the [" "] notation when I can directly reference something. I don't remember when I originally posted this...there's no date on it.

@SteveDinn
Copy link
Author

It was the same time that I changed my scripts to be event-triggered automations due to there being some problems if they ever had to run simultaneously.
https://community.home-assistant.io/t/simultaneously-running-multiple-instances-of-the-same-script/145169/17

@akasma74
Copy link

akasma74 commented Dec 4, 2019

Funny you should say that, because I did exactly that last week. I don't like using the [" "] notation when I can directly reference something. I don't remember when I originally posted this...there's no date on it.

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.

It was the same time that I changed my scripts to be event-triggered automations due to there being some problems if they ever had to run simultaneously.
https://community.home-assistant.io/t/simultaneously-running-multiple-instances-of-the-same-script/145169/17

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 ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment