How do I correctly write a sensor value using the modbus.write_register() service?

I am trying to use the modbus.write_register() service to write the value of a sensor to a holding register. I am getting an error that the value I am seeking to pass is not an integer. I have looked at other posts that had the same problem and I have tried the solutions they proposed, but it hasn’t solved my problem.
I have tested writing a static integer value using the Developer Tools > Template tool and that works fine and I see the new value show up on my modbus device. When I have tried using the sensor value with the Developer Tools > Template tool, I get the same error about the value not being of type int.
I am running hass version 0.107.7 in a virtual environment on raspbian operating system.
This is my code:

script:
  sammy_cox_drive_setpoint_update:
    alias: Sammy Cox Drive Setpoint Update
    sequence:
      # Pipe fill frequency is stored in Pr. 1.22 - Preset Reference 2
      - service: modbus.write_register
        data:
          hub: hub2
          unit: 255
          address: 1810
          value: "{{ states('sensor.new_sammy_cox_setpoint') | int }}"

I am using modbus TCP, and hence this is why the unit has value 255.
The error message I get is:

expected int@data['value'][0]

Here is screenshot showing the template output:

Here is output from my log file:

2020-04-18 19:16:06 ERROR (MainThread) [homeassistant.components.script] Setpoint Update Test: Error executing script. Invalid data for call_service at pos 1: expected int @ data['value'][0]
2020-04-18 19:16:06 ERROR (MainThread) [homeassistant.components.websocket_api.http.connection.2556177968] expected int @ data['value'][0]
Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/components/websocket_api/commands.py", line 134, in handle_call_service
    connection.context(msg),
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/core.py", line 1234, in async_call
    await asyncio.shield(self._execute_service(handler, service_call))
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/core.py", line 1257, in _execute_service
    await handler.func(service_call)
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/components/script/__init__.py", line 207, in service_handler
    await script.async_turn_on(variables=service.data, context=service.context)
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/components/script/__init__.py", line 284, in async_turn_on
    await self.script.async_run(kwargs.get(ATTR_VARIABLES), context)
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/helpers/script.py", line 693, in async_run
    await run.async_run()
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/helpers/script.py", line 444, in async_run
    await self._async_run()
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/helpers/script.py", line 460, in _async_run
    await self._async_step(not propagate_exceptions)
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/helpers/script.py", line 138, in _async_step
    self, f"_async_{cv.determine_script_action(self._action)}_step"
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/helpers/script.py", line 233, in _async_call_service_step
    context=self._context,
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/helpers/service.py", line 101, in async_call_from_config
    domain, service_name, service_data, blocking=blocking, context=context
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/core.py", line 1212, in async_call
    processed_data = handler.schema(service_data)
  File "/srv/homeassistant/lib/python3.7/site-packages/voluptuous/schema_builder.py", line 272, in __call__
    return self._compiled([], data)
  File "/srv/homeassistant/lib/python3.7/site-packages/voluptuous/schema_builder.py", line 594, in validate_dict
    return base_validate(path, iteritems(data), out)
  File "/srv/homeassistant/lib/python3.7/site-packages/voluptuous/schema_builder.py", line 432, in validate_mapping
    raise er.MultipleInvalid(errors)
voluptuous.error.MultipleInvalid: expected int @ data['value'][0]
1 Like

I solved my problem. It (as to be expected) was a “dumb user” problem. I should have used data_template instead of data.

script:
  sammy_cox_drive_setpoint_update:
    alias: Sammy Cox Drive Setpoint Update
    sequence:
      # Pipe fill frequency is stored in Pr. 1.22 - Preset Reference 2
      - service: modbus.write_register
        data_template:
          hub: hub2
          unit: 255
          address: 1810
          value: "{{ states('sensor.new_sammy_cox_setpoint') | int }}"
1 Like