Skip to content

Instantly share code, notes, and snippets.

@sjmf
Last active February 24, 2024 23:09
Show Gist options
  • Save sjmf/c46e72ff37ea5da8702aeeb8cacf47db to your computer and use it in GitHub Desktop.
Save sjmf/c46e72ff37ea5da8702aeeb8cacf47db to your computer and use it in GitHub Desktop.
Home Assistant Configuration for BuildAX Sensors
- id: process_serial_data
alias: Process Serial Data
description: Process Data from the BAX sensors
trigger:
- platform: state
entity_id: sensor.bax
action:
- service: python_script.process_serial_data
data:
data: '{{ trigger.to_state.state }}'
# Loads default set of integrations. Do not remove.
default_config:
# Load frontend themes from the themes folder
frontend:
themes: !include_dir_merge_named themes
script: !include scripts.yaml
python_script:
scene: !include scenes.yaml
automation: !include automations.yaml
logger:
default: info
input_number:
living_room_humidity:
name: Living Room Humidity
min: 0
max: 100
step: 1
living_room_temperature:
name: Living Room Temperature
min: -100
max: 100
step: 0.1
living_room_light:
name: Living Room Light
min: 0
max: 1000
step: 1
office_humidity:
name: Office Humidity
min: 0
max: 100
step: 1
office_temperature:
name: Office Temperature
min: -100
max: 100
step: 0.1
office_light:
name: Office Light
min: 0
max: 1000
step: 1
en_suite_humidity:
name: En Suite Humidity
min: 0
max: 100
step: 1
en_suite_temperature:
name: En Suite Temperature
min: -100
max: 100
step: 0.1
en_suite_light:
name: En Suite Light
min: 0
max: 1000
step: 1
bathroom_humidity:
name: Bathroom Humidity
min: 0
max: 100
step: 1
bathroom_temperature:
name: Bathroom Temperature
min: -100
max: 100
step: 0.1
bathroom_light:
name: Bathroom Light
min: 0
max: 1000
step: 1
sensor:
- platform: serial
serial_port: /dev/ttyACM1
baudrate: 9600
name: 'bax'
- platform: template
sensors:
living_room_humidity_sensor:
friendly_name: "Living Room Humidity"
unit_of_measurement: "%"
value_template: "{{ states('input_number.living_room_humidity') }}"
living_room_temperature_sensor:
friendly_name: "Living Room Temperature"
unit_of_measurement: "°C"
value_template: "{{ states('input_number.living_room_temperature') }}"
living_room_light_sensor:
friendly_name: "Living Room Light"
unit_of_measurement: "lx"
value_template: "{{ states('input_number.living_room_light') }}"
office_humidity_sensor:
friendly_name: "Office Humidity"
unit_of_measurement: "%"
value_template: "{{ states('input_number.office_humidity') }}"
office_temperature_sensor:
friendly_name: "Office Temperature"
unit_of_measurement: "°C"
value_template: "{{ states('input_number.office_temperature') }}"
office_light_sensor:
friendly_name: "Office Light"
unit_of_measurement: "lx"
value_template: "{{ states('input_number.office_light') }}"
en_suite_humidity_sensor:
friendly_name: "En Suite Humidity"
unit_of_measurement: "%"
value_template: "{{ states('input_number.en_suite_humidity') }}"
en_suite_temperature_sensor:
friendly_name: "En Suite Temperature"
unit_of_measurement: "°C"
value_template: "{{ states('input_number.en_suite_temperature') }}"
en_suite_light_sensor:
friendly_name: "En Suite Light"
unit_of_measurement: "lx"
value_template: "{{ states('input_number.en_suite_light') }}"
bathroom_humidity_sensor:
friendly_name: "Bathroom Humidity"
unit_of_measurement: "%"
value_template: "{{ states('input_number.bathroom_humidity') }}"
bathroom_temperature_sensor:
friendly_name: "Bathroom Temperature"
unit_of_measurement: "°C"
value_template: "{{ states('input_number.bathroom_temperature') }}"
bathroom_light_sensor:
friendly_name: "Bathroom Light"
unit_of_measurement: "lx"
value_template: "{{ states('input_number.bathroom_light') }}"
# This file lives in:
# python_scripts/process_serial_data.py
#
# 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',
}
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 = {
'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)
28,0-1 Top
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment