Hi there! Finally “tomorrow” arrived so let me share with you how I control my BL-NR devices:
1. The shell script
I control them via shell script. Maybe this is a bit different depending on your installation, I’m using containers. The script itself is the following one:
#!/bin/bash
module=$1
minutes=$2
case $module in
module1)
module_mac="XX:XX:XX:XX:XX:XX"
;;
module2)
module_mac="XX:XX:XX:XX:XX:YY"
;;
*)
exit 1
;;
esac
if [ $minutes -gt 0 ]; then
payload=$(printf "0504000A00%02x00%02x00" "$minutes" $(($minutes + 19)))
else
payload="050400900000009900"
fi
while true; do
gatttool \
-b "$module_mac" \
-t random \
--char-write-req \
-a 0x0011 \
-n "$payload"
[ $? -eq 0 ] && break
sleep 2
done
The script receives 2 parameters, module name and minutes to irrigate (0 minutes stops irrigation). It converts module name to device MAC, generates the Bluetooth payload and send the command. Because sending BT commands was not working well for me, there is an endless loop sending the command every 2 seconds until it works.
The payload is sent to handle 0x0011
for BL-NR but maybe this is different on other devices
Obviously you need to register the shell command in HA, as follows:
shell_command:
irrigation: sh/irrigation.sh {{ module }} {{ minutes }}
2. HA script
Then a HA script to call the shell script:
alias: Irrigation
mode: single
icon: mdi:sprinkler
fields:
module:
name: Module
description: The irrigation module that will be invoked.
required: true
selector:
select:
options:
- label: Module 1
value: module1
- label: Module 2
value: module2
minutes:
name: Minutes
description: Amount of minutes to irrigate. 0 means stop irrigation.
required: true
default: 20
selector:
number:
min: 0
max: 60
unit_of_measurement: minutes
sequence:
- service: shell_command.irrigation
data_template:
module: "{{ module }}"
minutes: "{{ minutes }}"
3. On/Off sensor
I haven’t found a way to create the on/off sensor. Apparently, there is no handle to read the sensor. I think the way is sending a request to “receive” and update, and the update is in another handle you can listen, but can’t confirm. So what I did was to create one HA timer per module and, when timer starts, it calls the script with the minutes, so I can kind of track when it’s watering.
3.1. The timer
Create a timer. To help on the next automation, the timer entity ID must have a fixed prefix, in my case timer.irrigation_module1
, timer.irrigation_module2
and so on. Set the default amount of minutes.
3.2. The automation controller
An automation to listen timers changes and call the script:
alias: Irrigation controller
mode: queued
max: 10
trigger:
- platform: state
entity_id:
- timer.irrigation_module1
to: active
- platform: state
entity_id:
- timer.irrigation_module2
to: active
condition: []
action:
- service: script.irrigation
data_template:
module: |
{{ trigger.to_state.entity_id[17:] }}
minutes: >
{% set time = strptime(trigger.to_state.attributes.duration, "%H:%M:%S") %}
{{ time.minute }}
- service: script.notifier
data_template:
message: >
{% set time = strptime(trigger.to_state.attributes.duration, "%H:%M:%S") %}
Irrigation started on {{ trigger.to_state.entity_id[17:] }} for {{ time.minute }} minutes.
3.3. Timer starter
To start the timer, do it manually on entity details, via timer.start
service (so you can decide the duration) or using an automation. I’m automating it once a week on sunrise (best time according to Google), but feel free to use any kind of starter.
alias: Irrigation starter
mode: single
trigger:
- platform: sun
event: sunrise
offset: 0
condition:
- condition: time
weekday:
- mon
action:
- service: timer.start
data:
duration: "1200"
target:
entity_id:
- timer.irrigation_module1
- timer.irrigation_module2