How to write sensor data from HA to modbus?

Hi!

I’m an happy Home Assistant user for a while. A lot of issues has been resolved by searching topics in this community but it seems I need a bit help now with a - for me - more advanced topic.

I’ve been able to retrieve modbus data by adding the following lines to configuration.yml:

modbus:
  - name: hub1
    type: tcp
    host: 10.0.0.100
    port: 502
    sensors:

      - name: Temp_outside
        unit_of_measurement: °C
        state_class: measurement
        scan_interval: 15
        slave: 1
        address: 11
        input_type: input
        data_type: int16
        scale: 0.1
        offset: 0
        precision: 1

My goal is (maybe a bit unusual) to write sensordata from Home Assistant to my external modbus. I’ve looking into the documentation and some similar questions but I can’t find the right solution. I’m not looking for a static number to write to a modbus address, but a value from a Home Assistent sensor/entity (like a temperature). Is there anyone who did something similar and can help me with a direction?

There is a write register service in modbus.

Try calling this from developer tools / services. Once you have that working and writing to the correct location, the create an automation that uses the sensor value in a data_template to call the service.

Thanks for your reply! I’ve looked into that specific part of the documentation, but I wasn’t able to convert it into a practical working piece of configuration. If there’s anyone with an example who tried something familiar, that would be awesome. In the meanwhile I’ll proceed with the documentation and your suggestion about the dev tools / services.

Thanks again, it works now!

1 Like

Hey,
could you post here how you did it? I solved the same thing with a Jablotron Futura recuperation system. I need to write the CO2 value from an external sensor. Thank you

me too
want to know how he did

I want to connect mu solax inverter to home assistant using modbus. And i want to be able to send the digital meter data i have in home assistant to solax , is this possible?

I hate it that I can switch “coils” in modbus.yaml but need the service workaround for everything else. But I solved it in automations.yaml.

- id: '1722081581502'
  alias: R0-Solltemp
  description: ''
  trigger:
  - platform: time_pattern
    seconds: /25
  condition: []
  action:
  - service: modbus.write_register
    data:
      hub: modbus_hub
      slave: 180
      address: 22
      value: '{{int(1000 + 10 * float(states(''input_number.r0_solltemp'')))}}'
  - service: modbus.write_register
    data:
      hub: modbus_hub
      slave: 180
      address: 21
      value: '{{states(''input_number.r0_zustand'')}}'
- id: '1722081581503'
  alias: Relais-Setzen
  description: ''
  trigger:
  - platform: time_pattern
    minutes: /1
  condition: []
  action:
  - service: modbus.write_coil
    data:
      hub: modbus_hub
      address: 0
      slave: 1
      state: '{{states(''input_boolean.mbr1'')}}' 

This is still in the trial stage, so all values to be written are inputs.
By the way there is a principal weakness in HA, more so for modbus. If a relay board loses power for a short time, all relays are off but HA assumes them to be on and displays them as such. For a sensor outages don’t bother as much, when it comes back on values resume. But it fails with outputs that are only set once. So rather than switching something on once it’s better to set a variable “this should be on” and outputting that variable in a fixed time raster. This is what my last example does reliably.

1 Like

Hi. Have you found out how to do this?