Hi @lordzid ,
I’ll be happy to share and help you through your project as much as I can.
First, I’ll start by summarizing my case to align what background and intended outcome.
Usage:
I am using a Sonoff RF bridge with esphome and Portisch firmware.
I am using it to catch events from my open/Close, motion and smoke sensors and save the status in binary sensors. These sensors are available in HA and I use these to have an updated state of all my sensors and use it for automations etc. (I have more than 20 sensors)
I also use the RF bridge to send RF signals controlling my alarm system.
Problem
The logic was fine, but I guess due to the amount of code, the bridge lost often network connectivity and its binary sensors and switches got unavailable, events could then be missed while unavailable and the status of these in my HA was unreliable.
Solution
I wanted to move most of the logic out of the bridge and instead do the logic in HA which has mush more processing power.
These are the steps I did:
-
in HA create Input_boolean for each of my sensors to hold the state of the sensor. Example: input_boolean.alarm_sensor_window_1
-
In the bridge save the latest received rc RF code in at text sensor “Latest_Code” which is available from HA.
Here is the relevant ESPHome code for the rf bridge
remote_receiver:
pin:
number: 4
inverted: false
dump: rc_switch
tolerance: 50
filter: 4us
idle: 4ms
on_rc_switch:
then:
text_sensor.template.publish:
id: rf_code
state: !lambda |-
char bin_buffer[33];
itoa(x.code,bin_buffer,2);
return bin_buffer;
...
text_sensor:
- platform: template
name: "Latest Code"
id: rf_code
this is available in HA as sensor.latest_code
Note that this might change rapidly and for one senor can record various signals but no problem with that as it also catches the right code at least once, so we can do automations based on these codes we want to watch for.
- In HA define the RF codes representing your sensors and you are interested in doing automations when these are received by the bridge. I use home assistant variables ( HACS component) to store these constants. You could also use input_text, but the variables are more conveniant to maintain in one file.
sensor_openclose1_open: #Window1
initial_value: '100101011111001000001010'
sensor_openclose1_close:
initial_value: '100101011111001000001110'
- Make automations based on changes in sensor.latest_code
You need to create an automation for each code. If you have many of these it is recommended to create a blueprint with the common logic and this is what I did:
blueprint:
name: update sensor state when rf code received
domain: automation
input:
sensor_name:
name: sensor name
selector:
entity:
domain: input_boolean
on_code:
name: on code
selector:
entity:
domain: var
off_code:
name: off code
selector:
entity:
domain: var
variables:
sensor_on_code: !input on_code
sensor_off_code: !input off_code
trigger:
- platform: state
entity_id: sensor.latest_code
condition:
- condition: or
conditions:
- condition: template
value_template: '{{ is_state(''sensor.latest_code'', states(sensor_on_code)) }}'
- condition: template
value_template: '{{ is_state(''sensor.latest_code'', states(sensor_off_code)) }}'
action:
- choose:
- conditions:
- condition: template
value_template: '{{ is_state(''sensor.latest_code'', states(sensor_on_code)) }}'
sequence:
- service: input_boolean.turn_on
target:
entity_id: !input sensor_name
- conditions:
- condition: template
value_template: '{{ is_state(''sensor.latest_code'', states(sensor_off_code)) }}'
sequence:
- service: input_boolean.turn_off
target:
entity_id: !input sensor_name
default: []
mode: single
Based on this blueprint I create an automation for each my sensors by simply selecting which code (Saved as a variable) will set my senor (represented by input_boolean) to on and off, as follows:
alias: alarm sensor window1 open
description: ''
use_blueprint:
path: homeassistant/update_sensor_state_when_rf_code_received.yaml
input:
sensor_name: input_boolean.alarm_sensor_window_1
on_code: var.sensor_openclose1_open
off_code: var.sensor_openclose1_close
And that is it
It is very stable, the bridge don’t loose network connectivity and catches all codes and that’s all what I need.
I hope this gives you an Idea of what I did, and please don’t hesitate to ask if you have any question, I’d love to help.