|
# Place this file in a directory `python_scripts/` under the HA config directory. |
|
# |
|
# BAX Field structure: |
|
# |
|
# 1. Date YYYY/MM/DD formatted ISO 8601 date of the packet |
|
# 2. Time HH:MM:SS formatted time the packet was received at |
|
# 3. Name The sensor's name, or its address if none has been assigned |
|
# 4. RSSI Received Signal Strength Indication, in dBm |
|
# 5. Type Received packet type. See Packet Types for full definition |
|
# 6. Seq No. Packet identifier (sensors send packets incrementally) |
|
# 7. TX Power Sensor-configured transmission power in dBm |
|
# 8. Battery Battery level, in millivolts, of the BuildAX sensor |
|
# 9. Humidity Relative Humidity (percentage of the saturation vapour pressure) |
|
# 10. Temp Temperature in degrees Celsius (°C), multiplied by 10. |
|
# 11. Light Luminous flux measurement in Lux |
|
# 12. PIR Count Activation counts of the Passive Infra-red sensor |
|
# 13. PIR Energy PIR energy last captured |
|
# 14. Switch Magnetic switch triggers on the device |
|
# |
|
# Example data: |
|
# 2024/02/24,19:04:26,Office,-55,1,45,20,2722,39.90,185,0,0,19398,1 |
|
# 2024/02/24,19:04:36,Living Room,-50,1,240,20,2642,40.10,187,7,0,62181,1 |
|
# 2024/02/24,19:04:45,En Suite,-74,1,51,20,2631,55.51,151,0,0,8341,1 |
|
# 2024/02/24,19:04:53,Upst. Bathrm,-51,1,237,20,2688,51.02,143,0,0,16589,1 |
|
# 2024/02/24,19:04:57,Office,-55,1,46,20,2722,39.90,185,0,0,19764,1 |
|
# |
|
#logger.info("input is {}".format(data)) |
|
#hass.bus.fire("script_run", {"process_serial_data": data}) |
|
|
|
names_entities = { |
|
'Office': 'office', |
|
'Living Room': 'living_room', |
|
'Upst. Bathrm': 'bathroom', |
|
'En Suite': 'en_suite', |
|
'Bedroom': 'bedroom', |
|
'Kitchen': 'kitchen', |
|
'Downst. Loo': 'loo' |
|
} |
|
|
|
def process_serial_data(data): |
|
|
|
sensor_data = data.strip().split(',') |
|
|
|
if len(sensor_data) < 14: |
|
logger.info('Non-data packet received') |
|
return |
|
|
|
try: |
|
name = sensor_data[2] |
|
#fields = ['date', 'time', 'name', 'rssi', 'type', 'seq', 'tx', 'battery', 'humidity', 'temperature', 'light', 'pir_count', 'pir_energy', 'switch_count' ] |
|
#values = {x: y for x,y in zip(fields, sensor_data)} |
|
# NB: Home Assistant's Python environment does not have dict(). WTAF. |
|
|
|
values = { |
|
'battery': float(sensor_data[7]) / 1000, |
|
'humidity': float(sensor_data[8]), |
|
'temperature': float(sensor_data[9]) / 10, |
|
'light': int(sensor_data[10]), |
|
} |
|
|
|
# Update entity based on sensor name |
|
entity = names_entities[name] |
|
for v in values: |
|
update_entity( |
|
'input_number.{}_{}'.format(entity, v), values[v] |
|
) |
|
|
|
except KeyError as e: |
|
logger.info(e) |
|
|
|
|
|
def update_entity(entity_id, state): |
|
# Update entity state |
|
logger.info(f"Updating entity {entity_id} with state {state}") |
|
|
|
# Update input_numbers |
|
hass.services.call( |
|
"input_number", |
|
"set_value", |
|
{ |
|
"value": state, |
|
"entity_id": entity_id |
|
} |
|
) |
|
|
|
|
|
# `data` is available as builtin and is a dictionary with the input data. |
|
process_serial_data( |
|
data.get("data") |
|
) |