Reading and writing modbus register - SOLVED

Hi,

I have made some progress with my Modbus controller. I can now read temperature value from it using Modbus TCP. Next step is to create a switch for turning on/off the heating. In my configuration.yaml I have:

modbus:
  - name: "ouman_garage"
    type: tcp
    host: 192.168.1.60
    port: 502
    delay: 0
    timeout: 5

    sensors:
      - name: "Garage heating ON/OFF"
        address: 132
        count: 1
        input_type: holding
        scan_interval: 5
        slave: 1

    switches:
      - name: "Garage heating ON/OFF"
        slave: 1
        address: 132

Basically I have created a sensor and switch. Modbus register is 132. In the scripts.yaml file I have:

script:
  description: "Garage heating"
  sequence:
    - service: modbus.write_register
      data_template:
        hub: ouman_garage
        unit: 1
        address: 130
        value: 1
    - delay:
        milliseconds: 100
    - service: modbus.write_register
      data_template:
        hub: ouman_garage
        unit: 1
        address: 132
        command_on: 100
        command_off: 0

Idea is to write first “1” to register 130 and after a short delay to write to register 132 either 0 (=off) or 100 (=on). My problem is that I donät understand how to pass the on/off information from the switch in the configuration.yaml to script.yaml. Any help would be appreciated. Thanks.

1 Like

Managed to solve this problem. Don’t remember exactly what was causing problems but at least I changed in command_on: 100 to “100”.

3 Likes

Hi I see that you resolved the problem.

I’m playing with modbus.
So far I’m able to read all sensors that I need but no sure what to do to write.
My goal is to set temp to the heatpump controller.

To read I’m using

      - name: "DHW temperature setpoint - comfort"
        unique_id: "DHW temperature setpoint - comfort"
        device_class: temperature
        data_type: int16
        address: 703
        scale: 0.1
        precision: 1
        input_type: holding
        unit_of_measurement: °C
        state_class: measurement
        scan_interval: 20

I’m able write to his register a value with node-red but how to do that in HA?
In node-read I’m using function code 06 (single)

I used standard

modbus.write_register

That’s it. It works with automation.

Can you share all the code that you needed to be able to write a holding register? I’m struggeling to find how to do it

To write a value to a sensor (holding) you need to do this via automation.
Here an example:

- id: '1761718868273'
  alias: Nibe Verdichter Blockieren
  description: ''
  triggers:
  - trigger: state
    entity_id:
    - input_boolean.nibe_blockieren
    to: 'on'
  conditions: []
  actions:
  - data:
      hub: nibe_smo_s40_f2120
      address: 2740
      slave: 1
      value: 4
    action: modbus.write_register
  - delay:
      milliseconds: 100
  - data:
      hub: nibe_smo_s40_f2120
      address: 2741
      slave: 1
      value: 1
    action: modbus.write_register
  mode: single

This is an example how to trigger with a helper-switch an action to write in two subsequent registers in modbus