Problem with setting up modbus with a light template

Hello,

I am struggling to get my dimmer lights to work with my Siemens Logo. I was able to make it work with my non dimmable lights but here…

  - platform: template
    lights:
      living_room:
        friendly_name: "Living Room"
        level_template: "{{ states('sensor.sensorlivingroom') / 10 }}"
        value_template: "{{ states('sensor.sensorlivingroom') | int > 0 }}"
        turn_on:
          - service: modbus.write_register
            data:
              hub: hub1
              unit: 1
              address: 50
              value: 50
        turn_off:
          - service: modbus.write_register
            data:
              hub: hub1
              unit: 1
              address: 50
              value: 0
        set_level:
          - service: modbus.write_register
            data:
              hub: hub1
              unit: 1
              address: 50
              value: "{{ brigthness | int }}"

What are my issues:

  • I can’t see the state of my lights level and value

  • Turning off and turning on doesn’t work

If in run the service in the developer tools it works fine:

value_template works fine in developer tools events.

I’ve been at it for hours so am grateful for any piece of advice.
thanks,

Your level template needs to convert to an int/float before doing math. You’ll also need to convert it to a value between 0-255. If your light reports state from 0-100, do some math to make it that range. I assume that’s what the /10 is supposed to be doing though…

level_template: "{{ states('sensor.sensorlivingroom') | int / 10 }}"

You also misspelled brightness here.

        set_level:
          - service: modbus.write_register
            data:
              hub: hub1
              unit: 1
              address: 50
              value: "{{ brigthness | int }}"

Thanks I didn’t know about the 0 - 255.

level_template: "{{ states('sensor.sensordiningroomlight') | int * 255 / 1000 }}"

I changed it to reflect the full range 0 - 255 and also corrected my english but this doesn’t solve the problem,

Is the only difference between your dimmable and non dimmable the level_template and set_level?

Your set_level will also have to convert the value back from 0-255 to whatever scale your things needs.

I’m not sure what your things need, in your example, you sent a value of 500 and said that worked, but in your yaml, you’re sending a value of 50.

Either way, set_level will have to do the conversion. And you have to use a data_template because you’re using a template value.

    set_level:
      - service: modbus.write_register
        data_template:
          hub: hub1
          unit: 1
          address: 50
          # Converts 0-255 to 0-100. 
          value: "{{ (brightness | float / 255.0) * 100 | round(0) }}"

yes those are the only difference and the fact that I don’t have to do any math from the state as it’s a binary sensor :

value_template: "{{ states('binary_sensor.BinarySensorAttic') }}"

My PLC can receive any value and anything above 100 it will round it down. But the range yes is from to 100 and i receive a value from 0 - 1000 back

What troubles me most is that the value_template doesn’t work as it seems straightforward in my code.

Now i do see that I get the following error:

2020-03-21 22:53:46 ERROR (MainThread) [homeassistant.core] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/helpers/entity.py", line 576, in async_request_call
    await coro
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/components/template/light.py", line 254, in async_turn_on
    {"brightness": kwargs[ATTR_BRIGHTNESS]}, context=self._context
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/helpers/script.py", line 189, in async_run
    await self._handle_action(action, variables, context)
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/helpers/script.py", line 272, in _handle_action
    await self._actions[_determine_action(action)](action, variables, context)
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/helpers/script.py", line 354, in _async_call_service
    context=context,
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/helpers/service.py", line 96, 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 1204, 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]

Whoops, need parenthesis. In my version, I’m rounding 100.0 to 100…then the entire thing is a giant float.

value: "{{ ((brightness | float / 255.0) * 100) | int }}"

I am still getting the same message. This only happens when i change the value of course.

In the meanwhile been testing a whole lot of different things but nothing seem to change the outcome :frowning:

Just checking, you made it a data_template vs data?

Thanks Jim this is probably the best question! I was able to make it work by adding data_template for the brightness. With my switches I don’t need data_templates.

Here is my working code!

  - platform: template
    lights:
      living_room:
        friendly_name: "Living Room"
        level_template: "{{ (states('sensor.sensorlivingroom') | int * 255 / 1000) | round(0) }}"
        value_template: "{{ states('sensor.sensorlivingroom') | int > 0 }}"
        turn_on:
          - service: modbus.write_register
            data:
              hub: hub1
              unit: 1
              address: 50
              value: 50
        turn_off:
          - service: modbus.write_register
            data:
              hub: hub1
              unit: 1
              address: 50
              value: 0
        set_level:
          - service: modbus.write_register
            data:
              hub: hub1
              unit: 1
              address: 50
            data_template:
              value: "{{ ((brightness | float / 255) * 100)  | int }}"