I am a newbie to HA and I am looking for a bit of guidance on how best to approach gathering several HA bit entities and packing them into a single modbus register write.
A little background, I have a MODBUS I/O device which has 50 or so registers but the main 2 registers are the Status register which has the switch states packed into 1 word. And then we have the output states packed into another word.
I have worked out how to unpack the switch states using a template, see code below for SysStatus
I want to pack output LED data into the GreenLEDS register, there are 6 LEDS.
LED values are 0=off, 1=on, 2=slow flash, 3=fast flash
B0, B1 = LED1
B2, B3 = LED2
…
Any pointers would be appreciated
Thanks in advance, Richard
# Loads default set of integrations. Do not remove.
default_config:
# Load frontend themes from the themes folder
frontend:
themes: !include_dir_merge_named themes
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml
modbus:
#Configure MODBUS connections
- name: modbus_hub
type: serial
port: /dev/ttySC0
baudrate: 9600
bytesize: 8
method: rtu
parity: N
stopbits: 1
delay: 0
message_wait_milliseconds: 5
retries: 0
timeout: 1
#Configure MODBUS entities
sensors:
- name: SysStatus
slave: 99
address: 21
input_type: holding
data_type: uint16
scan_interval: 1
- name: UpTime
slave: 99
address: 5
input_type: holding
data_type: uint64
scan_interval: 1
- name: RxCount
slave: 99
address: 9
input_type: holding
data_type: uint64
scan_interval: 1
- name: RedLEDs
slave: 99
address: 47
input_type: holding
data_type: uint16
- name: GreenLEDs
slave: 99
address: 48
input_type: holding
data_type: uint16
# The following looks at the SysStatus register, and then creates several new entities with their own name
# so you can use them in your automation
sensor:
- platform: template
sensors:
sw1_pressed:
friendly_name: "SW1 Pressed"
value_template: '{% if states("sensor.sysstatus") | int | bitwise_and(1) %} on {% else %} off {% endif %}'
sw2_pressed:
friendly_name: "SW2 Pressed"
value_template: '{% if states("sensor.sysstatus") | int | bitwise_and(2) %} on {% else %} off {% endif %}'
sw3_pressed:
friendly_name: "SW3 Pressed"
value_template: '{% if states("sensor.sysstatus") | int | bitwise_and(4) %} on {% else %} off {% endif %}'
sw4_pressed:
friendly_name: "SW4 Pressed"
value_template: '{% if states("sensor.sysstatus") | int | bitwise_and(8) %} on {% else %} off {% endif %}'
sw5_pressed:
friendly_name: "SW5 Pressed"
value_template: '{% if states("sensor.sysstatus") | int | bitwise_and(16) %} on {% else %} off {% endif %}'
sw6_pressed:
friendly_name: "SW6 Pressed"
value_template: '{% if states("sensor.sysstatus") | int | bitwise_and(32) %} on {% else %} off {% endif %}'