Skip to content

Instantly share code, notes, and snippets.

@sjmf
Last active April 4, 2024 18:23
Show Gist options
  • Save sjmf/01ae431c654cb068ced6c9559cff9ebb to your computer and use it in GitHub Desktop.
Save sjmf/01ae431c654cb068ced6c9559cff9ebb to your computer and use it in GitHub Desktop.
HomeAssistant Configuration Files

Home Assistant Config for BuildAX

sensors.yaml

  • ⁠Contains a definition for a serial sensor called ‘bax’ -⁠ ⁠⁠Contains the eventual sensors for each data type, for each sensor
  • ⁠⁠HA understands a “sensor” to be of a single data type only. It can’t have multiple streams.

automations.yaml

  • ⁠Triggers the python script when the state of the sensor changes (i.e. a line is received over serial)

process_serial_data.py

  • ⁠Triggered with the latest line of input data from the serial connection
  • ⁠⁠Outputs values into input_numbers

input_number.yaml

  • ⁠Contains definitions for the input_number objects, which determine the state of the multivariate sensors
  • ⁠⁠Verbose.

configuration.yaml

  • ⁠Just includes those other files.
- 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
input_number: !include input_number.yaml
sensor: !include sensors.yaml
switch:
- platform: rest
name: pc_switch
unique_id: pc_switch_rest
resource: http://192.168.1.77/pulse
state_resource: http://192.168.1.77/read_json
is_on_template: "{{ value_json.on }}"
password: "<my password>"
username: "admin"
logger:
default: info
shell_command:
sw_io1: /bin/bash -c "echo 'rpc0' >> /dev/ttyACM0"
sw_io2: /bin/bash -c "echo 'rpc8' >> /dev/ttyACM0"
living_room_humidity:
name: Living Room Humidity
min: 0
max: 100
step: 1
living_room_temperature:
name: Living Room Temperature
min: -10
max: 50
step: 0.1
living_room_light:
name: Living Room Light
min: 0
max: 1000
step: 1
living_room_battery:
name: Living Room Battery
min: 0
max: 3500
step: 1
office_humidity:
name: Office Humidity
min: 0
max: 100
step: 1
office_temperature:
name: Office Temperature
min: -10
max: 50
step: 0.1
office_light:
name: Office Light
min: 0
max: 1000
step: 1
office_battery:
name: Office Battery
min: 0
max: 3500
step: 1
en_suite_humidity:
name: En Suite Humidity
min: 0
max: 100
step: 1
en_suite_temperature:
name: En Suite Temperature
min: -10
max: 50
step: 0.1
en_suite_light:
name: En Suite Light
min: 0
max: 1000
step: 1
en_suite_battery:
name: En Suite Battery
min: 0
max: 3500
bathroom_humidity:
name: Bathroom Humidity
min: 0
max: 100
step: 1
bathroom_temperature:
name: Bathroom Temperature
min: -10
max: 50
step: 0.1
bathroom_light:
name: Bathroom Light
min: 0
max: 1000
step: 1
bathroom_battery:
name: Bathroom Battery
min: 0
max: 3500
step: 1
bedroom_humidity:
name: Bedroom Humidity
min: 0
max: 100
step: 1
bedroom_temperature:
name: Bedroom Temperature
min: -10
max: 50
step: 0.1
bedroom_light:
name: Bedroom Light
min: 0
max: 1000
step: 1
bedroom_battery:
name: Bedroom Battery
min: 0
max: 3500
step: 1
kitchen_humidity:
name: Kitchen Humidity
min: 0
max: 100
step: 1
kitchen_temperature:
name: Kitchen Temperature
min: -10
max: 50
step: 0.1
kitchen_light:
name: Kitchen Light
min: 0
max: 1000
step: 1
kitchen_battery:
name: Kitchen Battery
min: 0
max: 3500
step: 1
loo_humidity:
name: Loo Humidity
min: 0
max: 100
step: 1
loo_temperature:
name: Loo Temperature
min: -10
max: 50
step: 0.1
loo_light:
name: Loo Light
min: 0
max: 1000
step: 1
loo_battery:
name: Loo Battery
min: 0
max: 3500
step: 1
# 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")
)
- platform: serial
serial_port: /dev/ttyACM0
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') }}"
living_room_battery_sensor:
friendly_name: "Living Room Battery"
unit_of_measurement: "V"
value_template: "{{ states('input_number.living_room_battery') }}"
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') }}"
office_battery_sensor:
friendly_name: "Office Battery"
unit_of_measurement: "V"
value_template: "{{ states('input_number.office_battery') }}"
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') }}"
en_suite_battery_sensor:
friendly_name: "En Suite Battery"
unit_of_measurement: "V"
value_template: "{{ states('input_number.en_suite_battery') }}"
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') }}"
bathroom_battery_sensor:
friendly_name: "Bathroom Battery"
unit_of_measurement: "V"
value_template: "{{ states('input_number.bathroom_battery') }}"
bedroom_humidity_sensor:
friendly_name: "Bedroom Humidity"
unit_of_measurement: "%"
value_template: "{{ states('input_number.bedroom_humidity') }}"
bedroom_temperature_sensor:
friendly_name: "Bedroom Temperature"
unit_of_measurement: "°C"
value_template: "{{ states('input_number.bedroom_temperature') }}"
bedroom_light_sensor:
friendly_name: "Bedroom Light"
unit_of_measurement: "lx"
value_template: "{{ states('input_number.bedroom_light') }}"
bedroom_battery_sensor:
friendly_name: "Bedroom Battery"
unit_of_measurement: "V"
value_template: "{{ states('input_number.bedroom_battery') }}"
kitchen_humidity_sensor:
friendly_name: "Kitchen Humidity"
unit_of_measurement: "%"
value_template: "{{ states('input_number.kitchen_humidity') }}"
kitchen_temperature_sensor:
friendly_name: "Kitchen Temperature"
unit_of_measurement: "°C"
value_template: "{{ states('input_number.kitchen_temperature') }}"
kitchen_light_sensor:
friendly_name: "Kitchen Light"
unit_of_measurement: "lx"
value_template: "{{ states('input_number.kitchen_light') }}"
kitchen_battery_sensor:
friendly_name: "Kitchen Battery"
unit_of_measurement: "V"
value_template: "{{ states('input_number.kitchen_battery') }}"
loo_humidity_sensor:
friendly_name: "Loo Humidity"
unit_of_measurement: "%"
value_template: "{{ states('input_number.loo_humidity') }}"
loo_temperature_sensor:
friendly_name: "Loo Temperature"
unit_of_measurement: "°C"
value_template: "{{ states('input_number.loo_temperature') }}"
loo_light_sensor:
friendly_name: "Loo Light"
unit_of_measurement: "lx"
value_template: "{{ states('input_number.loo_light') }}"
loo_battery_sensor:
friendly_name: "Loo Battery"
unit_of_measurement: "V"
value_template: "{{ states('input_number.loo_battery') }}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment