Modbus binary_sensor - Option register_count doesn't work

Hello,

In order to get a binary_sensor from my ventilation system, I need to ask for 2 register, for example with the command 02.04.13.AC.00.02.
It works with :

  - platform: modbus_controller
    modbus_controller_id: wp
    id: bad_plus
    name: "Fonction Bad+"
    icon: "mdi:heat-wave"
    device_class: "running"
    custom_command: [0x02,0x04,0x13,0xAC,0x00,0x02]

As this command works, I “translate” it in yaml as :

  - platform: modbus_controller
    modbus_controller_id: wp
    id: bad_plus
    name: "Fonction Bad+"
    icon: "mdi:heat-wave"
    device_class: "running"
    address: 5036
    register_type: read
    register_count: 2
    force_new_range: true

And then, I get the error whern validating the code, that register_count option doesn’t exist for binary_sensor, but I found it in the documentation here : https://esphome.io/components/binary_sensor/modbus_controller

[register_count] is an invalid option for [binary_sensor.modbus_controller]

What could I do ?
Is there a bug somewhere?

Thanks for your help.

PS : I get the same issue with number:

How could binary response occupy more than one register???
What are you getting as response (on log) with that custom command?

I think that booleans are treated as integers.

This is the doc:

In the documentation, it’s written to always ask for 2 registers.

I’ll show you a log as soon as I can.

But if response is 0 or 1, one of the registers has to be 0 and the other one can be 0 or 1.
Let’s see when you have log.

Hello,

With the custom_command, here is the log I get :


[12:34:07][V][modbus_controller:232]: Updating modbus component
[12:34:45][V][modbus_controller:232]: Updating modbus component
[12:34:45][V][modbus_controller:199]: Range : 4284 Size: 1 (0) skip: 0
[12:34:45][V][modbus_controller:044]: Sending next modbus command to device 2 register 0x4284 count 1
[12:34:45][V][modbus:244]: Modbus write raw: 02.04.13.AC.00.02 (6)
[12:34:45][V][modbus_controller:569]: Command sent 0 0x4284 1 send_count: 1
[12:34:45][V][modbus_controller:081]: Modbus response queued
[12:34:45][V][modbus:159]: Clearing buffer of 8 bytes - parse succeeded
[12:34:45][V][modbus_controller:089]: Process modbus response for address 0x4284 size: 4
[12:34:45][V][modbus_controller:170]: data for register address : 0x4284 : 
[12:35:07][V][modbus_controller:232]: Updating modbus component
[12:35:45][V][modbus_controller:232]: Updating modbus component
[12:35:45][V][modbus_controller:199]: Range : 4284 Size: 1 (0) skip: 0
[12:35:45][V][modbus_controller:044]: Sending next modbus command to device 2 register 0x4284 count 1
[12:35:45][V][modbus:244]: Modbus write raw: 02.04.13.AC.00.02 (6)
[12:35:45][V][modbus_controller:569]: Command sent 0 0x4284 1 send_count: 1
[12:35:45][V][modbus_controller:081]: Modbus response queued
[12:35:45][V][modbus:159]: Clearing buffer of 8 bytes - parse succeeded
[12:35:45][V][modbus_controller:089]: Process modbus response for address 0x4284 size: 4
[12:35:46][V][modbus_controller:170]: data for register address : 0x4284 : 

And in HA, it seems that I have a value :


(“A l’arrêt” means “Stopped”)

If I do the same without the custom_command :

  - platform: modbus_controller
    modbus_controller_id: wp
    id: bad_plus
    name: "Fonction Bad+"
    icon: "mdi:heat-wave"
    device_class: "running"
    address: 5036
    register_type: read
    #register_count: 2
    force_new_range: true
    #custom_command: [0x02,0x04,0x13,0xAC,0x00,0x02]

And here are the logs :

[12:39:24][V][modbus_controller:232]: Updating modbus component
[12:39:24][V][modbus_controller:199]: Range : 13AC Size: 1 (4) skip: 0
[12:39:24][V][modbus_controller:044]: Sending next modbus command to device 2 register 0x13AC count 1
[12:39:24][V][modbus:223]: Modbus write: 02.04.13.AC.00.01.F5.5C (8)
[12:39:24][V][modbus_controller:569]: Command sent 4 0x13AC 1 send_count: 1
[12:39:24][V][modbus_controller:232]: Updating modbus component
[12:39:24][D][modbus:136]: Modbus error function code: 0x84 exception: 2
[12:39:24][E][modbus_controller:094]: Modbus error function code: 0x4 exception: 2 
[12:39:24][E][modbus_controller:103]: Modbus error - last command: function code=0x4  register address = 0x13AC  registers count=1 payload size=0
[12:39:24][V][modbus:159]: Clearing buffer of 4 bytes - parse succeeded

And the feedback in HA :
image
(“Inconnu” means “unknown”)

And putting 2 sensors, gives me :

[12:56:17][V][modbus_controller:044]: Sending next modbus command to device 2 register 0xBC1A count 1
[12:56:17][V][modbus:244]: Modbus write raw: 02.04.15.74.00.02 (6)
[12:56:17][V][modbus_controller:569]: Command sent 0 0xBC1A 1 send_count: 1
[12:56:17][V][modbus_controller:081]: Modbus response queued
[12:56:17][V][modbus:159]: Clearing buffer of 8 bytes - parse succeeded
[12:56:17][V][modbus_controller:089]: Process modbus response for address 0xBC1A size: 4
[12:56:17][V][modbus_controller:170]: data for register address : 0xBC1A : 
[12:56:17][D][binary_sensor:036]: 'Fonction Heizung+': Sending state OFF

Code :

  - platform: modbus_controller
    modbus_controller_id: wp
    id: bad_plus
    name: "Fonction Bad+"
    icon: "mdi:heat-wave"
    device_class: "running"
    #address: 5036
    #register_type: read
    #register_count: 2
    #force_new_range: true
    custom_command: [0x02,0x04,0x13,0xAC,0x00,0x02]

  - platform: modbus_controller
    modbus_controller_id: wp
    id: heizung_plus
    name: "Fonction Heizung+"
    icon: "mdi:heat-wave"
    device_class: "running"
    #address: 5492
    #register_type: read
    #register_count: 2
    #force_new_range: true
    custom_command: [0x02,0x04,0x15,0x74,0x00,0x02]

Ok, your device doesn’t accept request for only one address.

All the logs for custom_command is super confusing:

What the heck is 0xBC1A ??

Anyway, back to the point, I guess your options are custom_command or using sensor instead of binary sensor (and passing the value to template binary sensor if needed).

Hi,

I found a workaround, using lambda and putting the sensor internal :


sensor:
  - platform: modbus_controller
    modbus_controller_id: wp
    id: bad_plus_sensor
    name: "Fonction Bad+"
    icon: "mdi:heat-wave"
    skip_updates: 60
    address: 5036
    register_type: read
    register_count: 2
    force_new_range: true
    value_type: S_DWORD
    internal: true

binary_sensor:
  - platform: template
    id: bad_plus
    name: "Fonction Bad+"
    icon: "mdi:heat-wave"
    device_class: "running"
    lambda: |-
      if (id(bad_plus_sensor).state == 0) {
        // Function Bad+ is Off
        return false;
      } else {
        // Function Bad+ is On
        return true;
      }