Light template and modbus registers

Hi there fellow HA’ers

I’m trying to get a dimmable light controlled by modbus to work in HA.
I have created a sensor which reads the value of the dimmable light and it works great:

sensor:
  - platform: modbus
    registers:
      - name: stuespotssensor
        hub: SmartHouse
        slave: 1
        register: 1129

Then i created a light template like this:

  - platform: template
    lights:
      light_stue_spots_hkm:
        friendly_name: "hkm light"
        level_template: "{{ states.sensor.stuespotsensor.state|int }}"
        value_template: "{{ states.sensor.stuespotsensor.state|int > 0 }}"
        turn_on:
          - service: modbus.write_register
            data_template:
              hub: SmartHouse
              unit: 1
              address: 1129
              value: "{{ brightness }}"
          - service: homeassistant.update_entity
            data_template:
              entity_id: sensor.stuespotsensor
        turn_off:
          - service: modbus.write_register
            data_template:
              hub: SmartHouse
              unit: 1
              address: 1129
              value: 0
          - service: homeassistant.update_entity
            data_template:
              entity_id: sensor.stuespotssensor
        set_level:
          - service: modbus.write_register
            data_template:
              hub: SmartHouse
              unit: 1
              address: 1129
              value: "{{ brightness }}"
          - service: homeassistant.update_entity
            data_template:
              entity_id: sensor.stuespotssensor

The light is on when ever the sensor is above 0, and it’s turned off when it’s 0
If i use the developer tools i can use the service and write the modbus register which is working.

But i cant get the light card to function. It’s acting wierd… and as i see it it sets the brightness, but it sets it back to 0 right after.

Hope someone can assist me.

Modbus IoT class is Local Push (look at the top right part of the page).

Local Push Offers direct communication with device. Home Assistant will be notified as soon as a new state is available.

As such, those calls to homeassistant.update_entity are not needed. In fact, not sure what will happen when you use that on a push device.

Does the on/off switch work in the card? Is it just the brightness slider that doesn’t work? Keep in mind the slider is going to give you a value between 0-255. I don’t see any translation in your switch, so maybe that’s the correct input you’re expecting. If you need it to be between 0-100, you’ll have to modify the set_level function to convert the value.

Hi…

The on off is not working. If drag the dimmer slider above 39 it sets the modbus register to 0. If set to a number lower thatn 39 the REAL light turns on but the light in HA stays off

changed the level_template to this:

level_template: '{ "%2d" | format(states("sensor.stuespotsensor") | int*2.55 }'

Will it convert it from 0-255 to 0-100?

If your modbus is sending/expecting a value from 0-100, just do this.

  - platform: template
    lights:
      light_stue_spots_hkm:
        friendly_name: "hkm light"
        # Convert reported brightness (0-100) to (0-255)
        level_template: "{{ (states('sensor.stuespotsensor') | float * 2.55) | round(0) }}"
        # It's on if brightness > 0
        value_template: "{{ states('sensor.stuespotsensor') | int > 0 }}"
        turn_on:
          - service: modbus.write_register
            data_template:
              hub: SmartHouse
              unit: 1
              address: 1129
              # Need to convert brightness to a value between 1-100. 
              value: "{{ ((brightness | float / 255 ) * 100) }}"
        turn_off:
          - service: modbus.write_register
            data_template:
              hub: SmartHouse
              unit: 1
              address: 1129
              value: 0
        set_level:
          - service: modbus.write_register
            data_template:
              hub: SmartHouse
              unit: 1
              address: 1129
              value: "{{ ((brightness | float / 255 ) * 100) }}"

So, the things I changed here.

  1. Switched from states.sensor.stuespotsensor.state to states(‘sensor.stuespotsensor’). This will help in the event modbus template isn’t available before the template light is available. With the new change, if it’s not there, this wont throw an error.

  2. Convert 0-255 brightness values to/from modbus. Assuming it is 0-100, HA sends all brightness commands as the 8 bit value. So we need to translate the state correctly (level_template) as well as translate the requests to/from our template light.

  3. Removed the service calls to update the sensor. This is a push sensor, so they aren’t needed.

Wish I had a modbus thing to test with! Hope this helps.

Thanks a lot for your help @jocnnor.
Ended up with this, with some minor adjustments from your example!
Great community!

  - platform: template
    lights:
      light_stue_spots_hkm:
        friendly_name: "hkm light"
        # Convert reported brightness (0-100) to (0-255)
        level_template: "{{ (states('sensor.stuespotsensor') | float * 2.55) | round(0) }}"
        # It's on if brightness > 0
        value_template: "{{ states('sensor.stuespotsensor') | int > 0 }}"
        turn_on:
          - service: modbus.write_register
            data_template:
              hub: SmartHouse
              unit: 1
              address: 1129
              # Need to convert brightness to a value between 1-100. 
              value: "{{ states('input_number.dimmerstue') | round(0) }}"
        turn_off:
          - service: input_number.set_value
            data_template:
              entity_id: input_number.dimmerstue
              value: "{{ ((brightness | float / 255 ) * 100) | round(0) }}"
          - service: modbus.write_register
            data_template:
              hub: SmartHouse
              unit: 1
              address: 1129
              value: 0
        set_level:
          - service: modbus.write_register
            data_template:
              hub: SmartHouse
              unit: 1
              address: 1129
              value: "{{ ((brightness | float / 255 ) * 100) | round(0) }}"
          - service: input_number.set_value
            data_template:
              entity_id: input_number.dimmerstue
              value: "{{ ((brightness | float / 255 ) * 100) | round(0) }}"
1 Like

Hi Heine_Madsen

thanks for you sharing your modbus dimmer configurations!

I followed your steps to config, but found there’s no INPUT_NUMBER section, so I add it as follows

modbus:
  name: SmartHouse
  type: serial
  method: rtu
  port: /dev/ttyUSB0
  baudrate: 9600
  stopbits: 1
  bytesize: 8
  parity: N
sensor:
  - platform: modbus
    registers:
      - name: stuespotssensor
        hub: SmartHouse
        slave: 1
        register: 0
input_number:
  dimmerstue:
    name: dimmerstue
    initial: 10
    min: 10
    max: 100
    step: 1
light:
  - platform: template
    lights:
      light_stue_spots_hkm:
        friendly_name: "hkm light11"
        # Convert reported brightness (0-100) to (0-255)
        level_template: "{{ (states('sensor.stuespotsensor') | float * 2.55) | round(0) }}"
        # It's on if brightness > 0
        value_template: "{{ states('sensor.stuespotsensor') | int > 0 }}"
        turn_on:
          - service: modbus.write_register
            data_template:
              hub: SmartHouse
              unit: 1
              address: 0
              # Need to convert brightness to a value between 1-100. 
              value: "{{ states('input_number.dimmerstue') | round(0) }}"
          
        turn_off:
          - service: input_number.set_value
            data_template:
              entity_id: input_number.dimmerstue
              value: "{{ ((brightness | float / 255 ) * 100) | round(0) }}"
          - service: modbus.write_register
            data_template:
              hub: SmartHouse
              unit: 1
              address: 0
              value: 0
        set_level:
          - service: modbus.write_register
            data_template:
              hub: SmartHouse
              unit: 1
              address: 0
              value: "{{ ((brightness | float / 255 ) * 100) | round(0) }}"
          - service: input_number.set_value
            data_template:
              entity_id: input_number.dimmerstue
              value: "{{ ((brightness | float / 255 ) * 100) | round(0) }}"

Then I got a light card in lovelace UI, but when I turn on the light, the UI button not toggle to the position “ON”. I tried several times, but I failed.

It is appreciates that you could sharing your fully configurations of MosBUS Ligfht, for example: include the input_number section and so on…

Thanks Again!

Here’s my current config

# Dimmer
  input_number:
    dimmerstue:
      name: Dimmer stue value
      min: 10
      max: 100
      step: 1

  sensor:
  - platform: modbus
    registers:
      - name: stuespotsensor
        hub: SmartHouse
        slave: 1
        register: 1129
light:
- platform: template
  lights:
    stue_spots:
      friendly_name: "Spots"
      # Convert reported brightness (0-100) to (0-255)
      level_template: "{{ (states('sensor.stuespotsensor') | float * 2.55) | round(0) }}"
      # It's on if brightness > 0
      value_template: "{{ states('sensor.stuespotsensor') | int > 0 }}"
      turn_on:
        - service: modbus.write_register
          data_template:
            hub: SmartHouse
            unit: 1
            address: 1129
            # Need to convert brightness to a value between 1-100. 
            value: "{{ states('input_number.dimmerstue') | round(0) }}"
      turn_off:
        #- service: input_number.set_value
        #  data_template:
        #    entity_id: input_number.dimmerstue
        #    value: "{{ ((brightness | float / 255 ) * 100) | round(0) }}"
        - service: modbus.write_register
          data_template:
            hub: SmartHouse
            unit: 1
            address: 1129
            value: 0
      set_level:
        - service: modbus.write_register
          data_template:
            hub: SmartHouse
            unit: 1
            address: 1129
            value: "{{ ((brightness | float / 255 ) * 100) | round(0) }}"
        - service: input_number.set_value
          data_template:
            entity_id: input_number.dimmerstue
            value: "{{ ((brightness | float / 255 ) * 100) | round(0) }}"

Hi, I’m trying to implement this myself but I cannot get it to work. The register value updates only when the on/off switch is toggled. When the dimmer state is on, and I use the slider, the register does not update continuously. Is it still working smoothly for you? Any idea what I could be doing wrong? My dimmer config in configuration.yaml is the following:

#--------------------------------------------
#Dimmer
#--------------------------------------------
input_number:
  dimmer_number:
    name: Dimmer test range
    min: 10
    max: 4000
    step: 10

sensor:
  - platform: modbus
    registers:
      - name: DimmerRegister
        hub: froso_berge
        slave: 1
        register: 39858
    scan_interval:
      seconds: 0.5

light:
  - platform: template
    lights:
      dimmertest:
        friendly_name: "DimmerTest"
        # Convert reported brightness (0-4000) to (0-255)
        level_template: "{{ ((states('sensor.DimmerRegister') | float / 4000) * 255) | round(0) }}"
        # It's on if brightness > 0
        value_template: "{{ states('sensor.DimmerRegister') | int > 0 }}"
        turn_on:
          - service: modbus.write_register
            data:
              hub: froso_berge
              unit: 1
              address: 39858
              # Need to convert brightness to a value between 10-4000.
              value: "{{ states('input_number.dimmer_number') | round(0) }}"
        turn_off:
          - service: modbus.write_register
            data:
              hub: froso_berge
              unit: 1
              address: 39858
              value: 0
        set_level:
          - service: modbus.write_register
            data:
              hub: froso_berge
              unit: 1
              address: 39858
              value: "{{ ((brightness | float / 255 ) * 4000) | round(0) }}"
          - service: input_number.set_value
            data:
              entity_id: input_number.dimmer_number
              value: "{{ ((brightness | float / 255 ) * 4000) | round(0) }}"

Hi @Heine_Madsen

I currently have a Enocean-Dali-Gateway setup to control my Dali lights with HA. However, due to some additional Dali devices, I need to switch to a different gateway, one being able to handle a multi master setting.

I’m looking at either Modbus TCP-Dali or MQTT-Dali, with the following products

https://adfweb.com/Home/products/DALI_MQTT.asp?language=ENG

The MQTT light integration in HA seems to be more mature than Modbus TCP lights, especially when it comes to dimming. And therefore easier to implement. However, from the actual products above, I’d prefer the Lunatone device, which would mean the Modbus setup.

What is your experience with your dimmable Modbus lights in HA? Does your config work as you want it to? How is the response time? And how reliable is the setting?

I have the same problem.
At section
set_level:
I could write actual brightness to input_number, but couldn’t to modbus register

Hi there,

Is there made any progress on this topic?
I’ve been planning my smart home layout and for me the dali4net would be a perfect fit in combination with a lot of dali drivers.

I would like to know if this would work through the modbus interface to properly control dali lights through the Dali 4Net Gateway.

Thanks a lot!
Pieter

Disclaimer: I am working for Atios in Switzerland.

Our Atios SmartCore supports control of DALI Lights (DT6, DT7, DT8, Adress and Group control) and DALI-2 Keypads. Pairing to Home Assistant can be done via HomeKit or Matter, certified since October 2023.

Future firmware updates will add support of further DALI-2 Sensors such as Motion, Presence, Temperature, AirQuality and so on. We are also integrating Blinds / Curtain controls via existing DALI-modules on the market. Directly assigning addresses to new DALI loads on the bus via our webinterface will be the icing on the cake.