Change state of sensor with event?

I flashed an FR Bridge 2 with ESPHome and the data is being received and displayed using the event “esphome.rf_code_received”. I have a Govee 433 MHz water sensor that sends code 006ba32 when moisture is detected and 006ba30 when the button is pushed. I’d like to have a template binary sensor that sets the state wet when the first code is received and dry when the second code is received. Like this in pseudo code:

  - name: "Laundry Moisture"
    unique_id: laundry_moisture
    device_class: moisture
    state: wet with the first code, dry with the second

Is it possible to set the state of a sensor directly from an event being fired, or do I have to have a publish event for each code in the ESPHome yaml in the RF Bridge? Thanks for any help.

So the data is in esphome.
Post your esphome yaml.

This is the RF Bridge yaml:

#Sonoff RF BridgeR2 https://www.irrgang.dev/how-to-flash-the-sonoff-rf-bridger2-with-esphome/
substitutions:
  name: rf-bridge
  friendly_name: RF Bridge

esphome:
  name: ${name}
  friendly_name: ${friendly_name}

esp8266:
  board: esp01_1m

# Enable logging
logger:
  baud_rate: 0

# Enable Home Assistant API
api:
  services:
    - service: send_rf_code
      variables:
        sync: int
        low: int
        high: int
        code: int
      then:
        - rf_bridge.send_code:
            sync: !lambda 'return sync;'
            low: !lambda 'return low;'
            high: !lambda 'return high;'
            code: !lambda 'return code;'
    - service: learn
      then:
        - rf_bridge.learn
rf_bridge:
  on_code_received:
    then:
      - homeassistant.event:
          event: esphome.rf_code_received
          data:
            sync: !lambda 'return format_hex(data.sync);'
            low: !lambda 'return format_hex(data.low);'
            high: !lambda 'return format_hex(data.high);'
            code: !lambda 'return format_hex(data.code);'

ota:
  - platform: esphome

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "${friendly_name} ESP"
    password: !secret wifi_password_2

button:
  # Restart the ESP
  - platform: restart
    name: "Restart"

uart:
  tx_pin: 1
  rx_pin: 3
  baud_rate: 19200

captive_portal:

# Sensors for ESP version and WIFI information
text_sensor:
  # ESPHome version
  - platform: version
    hide_timestamp: true
    name: "ESPHome Version"
  # IP address and connected SSID
  - platform: wifi_info
    ip_address:
      name: "IP Address"
      icon: mdi:wifi
    ssid:
      name: "Connected SSID"
      icon: mdi:wifi-strength-2

sensor:
  # WiFi signal
  - platform: wifi_signal
    name: "WiFi Signal"
    update_interval: 120s
  - platform: uptime
    name: "${friendly_name} Uptime"

binary_sensor:
  - platform: status
    name: "${friendly_name} Status"

light:
  - platform: status_led
    name: "Switch state"
    pin: GPIO13

I have never used RF bridge and the documentation is quite shy.
You could try something like this:

binary_sensor:
  - platform: template
    id: moisture
    name: "Moisture detected"
    

rf_bridge:
  on_code_received:
    then:
      - lambda: |-
          if (data.code == 0x006ba32) {
            id(moisture).publish_state(true);
          }
          if (data.code == 0x006ba30) {
            id(moisture).publish_state(false);
          }
1 Like

I added device class as moisture and this works just as I wanted. Thanks! I have five of these sensors so a lot of copy and paste. I previously had these sensors read by a rtl_433 device to MQTT, but that wasn’t very reliable. This should work better.

You are welcome.

For curiosity, which device you used?