Extracting Binary state of bits from Modbus uint16 holding register

I am reading out a uint16 value in Modbus from a Status Word for a wind turbine setup.
This value records the alarm on.off state of various parameters (overspeed/wind excessive/overcurrent etc)
I need to read the first (lsb) 8 bits of this uint16 holding register and depending on their individual state (1 or 0 obviously) use this to present the alarm state.

So - for instance:
0000000000000001 would equate to Brake_Status = ON
0000000000000010 would equate to Run_Status = ON
etc for bits 0 - 7

It is possible to have more than one active at a time. So I cant just read out an integer value (2,4,8,16,etc) for the state. I need to extract the binary bit value for each of these first 8 bits and name each bit, give it a unique ID and then read it’s 1 or 0 state as a coil type status. These will then be used on the UI probably as a red/green LED type output.

Can anyone give me some hints as the best way to structure this please?

I can pull out the basic integer value using this:

- name: RUN_STATUS
        address: 15
        slave: 1
        data_type: uint16
        input_type: holding

3 Likes

Resloved this - for anyone interested here is the yaml:

      - name: RUN_STATUS
        unique_id: CB_15
        address: 15
        scan_interval: 5
        data_type: custom
        input_type: holding
        count: 1
        structure: ">H"
        slave: 1
        state_class: measurement
template:
 - sensor:    
    - name: "Run_Status_Map"
      state: "{{ states('sensor.run_status') }}"
      attributes:
        brake_status: "{% if states('sensor.run_status') | int | bitwise_and(1) %}on{% else %}off{% endif %}"
        run_status_state: "{% if states('sensor.run_status') | int | bitwise_and(2) %}on{% else %}off{% endif %}"
        over_rpm_status: "{% if states('sensor.run_status') | int | bitwise_and(4) %}on{% else %}off{% endif %}"
        over_voltage_status: "{% if states('sensor.run_status') | int | bitwise_and(8) %}on{% else %}off{% endif %}"
        over_current_status: "{% if states('sensor.run_status') | int | bitwise_and(16) %}on{% else %}off{% endif %}"
        high_wind_status: "{% if states('sensor.run_status') | int | bitwise_and(32) %}on{% else %}off{% endif %}"
        low_wind_status: "{% if states('sensor.run_status') | int | bitwise_and(64) %}on{% else %}off{% endif %}"
        remote_stop_status: "{% if states('sensor.run_status') | int | bitwise_and(128) %}on{% else %}off{% endif %}"
        
        
 - binary_sensor:
    - name: Brake_Status
      state: "{% if states('sensor.run_status') | int | bitwise_and(1) %}on{% else %}off{% endif %}"
      unique_id: CB8151F76E0_15_1
 - binary_sensor:
    - name: Run_Status
      state: "{% if states('sensor.run_status') | int | bitwise_and(2) %}on{% else %}off{% endif %}"
      unique_id: CB8151F76E0_15_2
 - binary_sensor:
    - name: Over_RPM_Stop
      state: "{% if states('sensor.run_status') | int | bitwise_and(4) %}on{% else %}off{% endif %}"
      unique_id: CB8151F76E0_15_3
 - binary_sensor:
    - name: Over_Voltage_Stop
      state: "{% if states('sensor.run_status') | int | bitwise_and(8) %}on{% else %}off{% endif %}"
      unique_id: CB8151F76E0_15_4
 - binary_sensor:
    - name: Over_Current_Stop
      state: "{% if states('sensor.run_status') | int | bitwise_and(16) %}on{% else %}off{% endif %}"
      unique_id: CB8151F76E0_15_5
 - binary_sensor:
    - name: High_Wind_Stop
      state: "{% if states('sensor.run_status') | int | bitwise_and(32) %}on{% else %}off{% endif %}"
      unique_id: CB8151F76E0_15_6
 - binary_sensor:
    - name: Low_Wind_Stop
      state: "{% if states('sensor.run_status') | int | bitwise_and(64) %}on{% else %}off{% endif %}"
      unique_id: CB8151F76E0_15_7
 - binary_sensor:
    - name: Remote_Stop
      state: "{% if states('sensor.run_status') | int | bitwise_and(128) %}on{% else %}off{% endif %}"
      unique_id: CB8151F76E0_15_8
6 Likes