|
| 1 | +# LoRa Door/Window Sensor State Communication |
| 2 | + |
| 3 | +## Short description |
| 4 | + |
| 5 | +Scripts for sending door/window sensor state over LoRa between Shelly devices. The sender monitors an input connected to a DW sensor and transmits state changes (via status handler) and/or periodically (via timer) to a receiver, which displays the result in a virtual boolean component. |
| 6 | + |
| 7 | +## Requirements |
| 8 | + |
| 9 | +- Two Shelly devices with LoRa addons (e.g., Shelly i4DC Gen4, Shelly 1PM Gen4) |
| 10 | +- Door/Window sensor connected to an input on the sender device |
| 11 | +- Virtual boolean component configured on the receiver device (ID: 200) |
| 12 | +- AES 128-bit base64 encryption key for secure communication |
| 13 | + |
| 14 | +### Generate AES Key |
| 15 | + |
| 16 | +```bash |
| 17 | +openssl rand -base64 16 |
| 18 | +``` |
| 19 | + |
| 20 | +## Configuration |
| 21 | + |
| 22 | +### Sender Configuration (lora-wired-dw-sender.js) |
| 23 | + |
| 24 | +| Parameter | Description | Default | |
| 25 | +|-----------|-------------|---------| |
| 26 | +| `loraComponentKey` | ID of the LoRa component instance | `"lora:100"` | |
| 27 | +| `tx_key_id` | Encryption key index [1,2,3] | `1` | |
| 28 | +| `lr_addr` | Recipient LoRa address (hex string) | `"000000BB"` | |
| 29 | +| `doorWindowComponent` | Input where DW sensor is connected | `"input:1"` | |
| 30 | +| `useStatusHandler` | Enable sending on state change | `true` | |
| 31 | +| `useTimer` | Enable periodic sending via timer | `true` | |
| 32 | +| `interval` | Timer interval in milliseconds | `3000` | |
| 33 | + |
| 34 | +### Receiver Configuration (lora-wired-dw-reciever.js) |
| 35 | + |
| 36 | +| Parameter | Description | Default | |
| 37 | +|-----------|-------------|---------| |
| 38 | +| `doorWindowComponent` | Input key from sender to match | `"input:1"` | |
| 39 | +| `doorWindowVirtualComponent` | Virtual component type | `"boolean"` | |
| 40 | +| `doorWindowVirtualComponentId` | Virtual component ID | `200` | |
| 41 | +| `lr_addr` | LoRa address | `"000000BB"` | |
| 42 | +| `key1` | Encryption key (same as sender's key1) | - | |
| 43 | + |
| 44 | +**Note:** User LoRa calls must be set to `true` on both devices in LoRa transport layer config settings. |
| 45 | + |
| 46 | +## Installation |
| 47 | + |
| 48 | +1. Wire up your Shelly devices |
| 49 | +2. Attach LoRa addons to both devices |
| 50 | +3. Power up the devices |
| 51 | +4. In the web interface, go to Add-on submenu and enable LoRa add-on |
| 52 | +5. Enable "User LoRa calls" in LoRa transport layer config on both devices |
| 53 | +6. Configure matching encryption key (key1) on both sender and receiver |
| 54 | + |
| 55 | +### Sender Setup |
| 56 | + |
| 57 | +1. Create a script from `lora-wired-dw-sender.js` on the sender device |
| 58 | +2. Configure the `doorWindowComponent` to match your DW sensor input |
| 59 | +3. Set `lr_addr` to the receiver's LoRa address |
| 60 | +4. Save and run the script |
| 61 | + |
| 62 | +### Receiver Setup |
| 63 | + |
| 64 | +1. Create a virtual boolean component (ID: 200) on the receiver device |
| 65 | +2. Create a script from `lora-wired-dw-reciever.js` on the receiver device |
| 66 | +3. Configure `doorWindowComponent` to match the sender's configuration |
| 67 | +4. Save and run the script |
| 68 | + |
| 69 | +## How It Works |
| 70 | + |
| 71 | +### Sender |
| 72 | + |
| 73 | +The sender supports two modes (can be enabled independently via CONFIG): |
| 74 | + |
| 75 | +**Status Handler Mode** (`useStatusHandler: true`): |
| 76 | +- Listens for status changes on the configured input |
| 77 | +- When the DW sensor state changes (open/close), immediately sends a JSON message over LoRa |
| 78 | + |
| 79 | +**Timer Mode** (`useTimer: true`): |
| 80 | +- Periodically sends the current state every `interval` milliseconds (default: 3000ms) |
| 81 | +- Useful as a heartbeat or to ensure receiver stays in sync |
| 82 | + |
| 83 | +Message format: |
| 84 | +```json |
| 85 | +{"component": "input:1", "value": true} |
| 86 | +``` |
| 87 | + |
| 88 | +### Receiver |
| 89 | + |
| 90 | +- Listens for LoRa events with `event.info.event === "user_rx"` |
| 91 | +- Decodes and parses the received message |
| 92 | +- Compares received value with current virtual component value |
| 93 | +- Only updates the virtual boolean component if the value has changed (reduces unnecessary RPC calls) |
0 commit comments