Pass variable to modbus in automation

I hope someone here can help me:

I am controlling my fireplace over modbus and all is working but setting the intensity. I have a slider (slider1) for this together with an automation. But I can’t get the slider value to be sent. If I set the automation with a fixed value it works:

- id: '210'
  alias: Haard intensiteit
  trigger:
  - entity_id:  input_number.slider1
    platform: state
  action:
  - service: modbus.write_register
    data:
      unit: 2
      address: 40201
      value: 10

But as soon as I change the value into something dynamic I get an error:

value: '{{states.input_number.slider1.state}}'
or
value: '{{states(input_number.slider1)}}'

the error is

Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/automation/__init__.py", line 294, in async_trigger
    await self._async_action(self.entity_id, variables, context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/automation/__init__.py", line 378, in action
    await script_obj.async_run(variables, context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/script.py", line 130, in async_run
    await self._handle_action(action, variables, context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/script.py", line 172, in _handle_action
    action, variables, context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/script.py", line 261, in _async_call_service
    context=context
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/service.py", line 81, in async_call_from_config
    domain, service_name, service_data, blocking=blocking, context=context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/core.py", line 1101, in async_call
    processed_data = handler.schema(service_data)
  File "/usr/local/lib/python3.6/site-packages/voluptuous/schema_builder.py", line 267, in __call__
    return self._compiled([], data)
  File "/usr/local/lib/python3.6/site-packages/voluptuous/schema_builder.py", line 589, in validate_dict
    return base_validate(path, iteritems(data), out)
  File "/usr/local/lib/python3.6/site-packages/voluptuous/schema_builder.py", line 427, in validate_mapping
    raise er.MultipleInvalid(errors)
voluptuous.error.MultipleInvalid: expected int @ data['value'][0]

I have tried many versions but somehow I can’t seem to pass a variable to modbus…

Have you tried it with data_template ?

Thanks for the suggestion! Unfortunately I still get the error. I’ll give a more detailed report as soon as I can find the time to deep-dive into it.

Thomas

From your error

The value you are sending is not an integer (int)
So change your code to send an integer.

After some further exploring I found the solution what your suggestion and something else:

value: '{{trigger.to_state.state | int}}'

For some reason the slider value was not getting through, but I managed to make it work with the trigger value in this way.

Thank you for that hint / solution.

Have spent hours on this but didn’t figure it out. I was also puzzled on that.

Strange that it is not working “the normal way”.

Working now flawlessly!

fregatte