Tuya smart knob improved blueprint for Zigbee2Mqtt

Hi!

I took my time and figured out how to make the tuya smart knob work smoothly with dimming lights and happy to annouce it’s possible!

Here’s my manual on how to get it working: TheUnlimited64/tuya_smart_knob_blueprint

It requires a little change with zigbee2mqtt due to not having the property exposed into home assistant.

But with that change we’re getting a positive and negative value when dimming. The event mode is very slow to respond, command mode is way better.

Maybe someone can create an even more advanced blueprint than mine (was a pain to get it working lol).

@rdeangel @pbergman maybe check it out :slight_smile:

3 Likes

I’ve added a new feature to the blueprint and zigbee2mqtt. It now allows for color_temperature control action, that enbles color temperature control of your lamp, or control other devices brightness, volume etc.

In my example I’ve used the action to control the brightness of my led strip, but you’re free to use it for volume control, color temperature control and anything else.

I’ve also included an example for color temperature control, because it wasn’t straight forward (atleast for me).

Check it out: TheUnlimited64 tuya smart knob temperature control

Hi, sorry if this is high jacking your post but you seem to be the only source of useful information for this device.

I’m using this to control a input number helper 0-5 which correlates to 6 individual RF commands through RM pro to my fan.
I’d like it to register the commands faster so if i rotate right quickly it will jump to higher fan speeds without having to go through each step. Can this method be used to achieve this result?

Also, I’ve been trying to work out the difference between command and event modes, can you explain? I tried switching to command mode but everything stopped working, so guess I would have to recreate all the automations to find out and its taken me months just to get it working so I’m not in a hurry to do that yet. I do have a second unit so maybe ill try it over Christmas if needed.

Hi,

Yes I can give a bit more info. The event mode is very slow from tuya side. It will send rotate left only after an delay.

If you switch to command mode, it publishes how much you’ve turned in a specific time. So turning fast means it registers more ticks.

It’s nearly instant and you can control speed very nicely. I for example control my brightness with this and it’s allows for quick and precise control

In my blueprint I expose the variable delta_value which you can use to calculate the amount you want to change. 1 tick is 1 delta_value

Feel free to ask more questions :slight_smile:

It’s currently done with the custom converter, but I’m in the progress to release it to the official converters

Hi! Do I still need to create the converter? Or it has been fixed on z2m?

Thanks :slight_smile:

Hi,
You still need the converter, but it should be ready shortly. Still have to test my PR (seems a bit tricky) and then it has to be accepted.

Ok thanks :smiley:

Any updates on this?

Thanks a lot for the blueprint. I managed to get it working. Just a question though, is there a way to limit brightness so it doesn’t turn off the light when the knob is rotated all the way?

I want the button to toggle the lights and the rotation to determine brightness. Rotating too far shouldn’t turn the lights off.

I did propose mychange, but there’s a different way of implementing it. Due to exams I currently don’t have enough time to look at it yet. Sorry!

You most likely can fix this by doing some templating magic. Like an if statement, that checks if the brightness percent goes negative.

Means that you need to fetch the brightness from your lamp, calculate the brightness step as before and then add the step to your brightness and check if it’s equal or greater than 1, if it’s 0, home assistant will turn it off

No worries mate. Focus on the important things. I’ll try out the template sensor and share if it works

1 Like

Thanks for this!

I just started with Zigbee2MQTT and prefer the event entities, so I adjusted your blueprint somewhat to work with that. I also added a few features, such as that you can assign something to the rotation while the button is pressed (the color_temp mode) and you can adjust the sensitivity.

blueprint:
  name: Universal Smart Knob Control
  description: Control any entity with a Smart Knob using customizable rotation and click actions
  domain: automation
  input:
    brightness_delta_sensor:
      name: Brightness Delta Sensor
      description: Sensor providing brightness delta values from Smart Knob rotation
      selector:
        entity:
          filter:
            domain: sensor
            integration: mqtt        
    step_size_sensor:
      name: Action Step Size Sensor
      description: Sensor providing the step size unsigned, can be used with the pressed rotation
      selector:
        entity:
          filter:
            domain: sensor
            integration: mqtt
    event_entity:
      name: Smart Knob Event Sensor
      description: Sensor providing all events from the knob (toggle and rotations)
      selector:
        entity:
          filter:
            domain: event
            integration: mqtt
    sensitivity_brightness:
      name: Sensitivity Brightness
      description: factor the delta_value for the brightness action is multiplied with to adjust the strength of the control
      selector:
        number:
          min: 0.01
          max: 10
          step: 0.01
          mode: box
      default: 0.5
    sensitivity_color:
      name: Sensitivity Color
      description: factor the delta_value for the color action is multiplied with to adjust the strength of the control
      selector:
        number:
          min: 0.01
          max: 10
          step: 0.01
          mode: box
      default: 0.5
    rotation_action:
      name: Rotation Action
      description: Action to execute when rotating the knob. Uses 'delta_value' variable for the rotation value.
      default: []
      selector:
        action: {}
    color_rotation_action:
      name: Color Rotation Action
      description: Action to execute when rotating the knob while pressed. Uses 'delta_value' variable for the rotation value.
      default: []
      selector:
        action: {}
    click_action:
      name: Click Action
      description: Action to execute when clicking the knob
      default: []
      selector:
        action: {}

mode: parallel
max_exceeded: silent

variables:
  event_entity: !input event_entity
  brightness_delta_sensor: !input brightness_delta_sensor
  step_size_sensor: !input step_size_sensor

trigger:
  - trigger: state
    entity_id: !input event_entity

condition:
  - condition: template
    value_template: >
      {{ trigger.to_state.state not in ['unavailable', 'unknown'] }}
  - condition: template
    value_template: >
      {{ trigger.from_state.state not in ['unavailable', 'unknown'] }}


action:
  - variables:
      sensitivity_brightness: !input sensitivity_brightness
      sensitivity_color: !input sensitivity_color
      command: "{{ state_attr(event_entity, 'event_type') }}"
      brightness_delta: "{{ states(brightness_delta_sensor) | float(0) * sensitivity_brightness }}"
      step_size: "{{ states(step_size_sensor) | float(0) * sensitivity_color }}"
  - choose:
      # Handle rotation action
      - conditions:
          - condition: template
            value_template: >
              {{ command == 'brightness_step' }}
        sequence:
          - variables:
              delta_value: "{{ brightness_delta }}"
          - alias: "Execute rotation action"
            sequence: !input rotation_action

      # Handle color rotation action up
      - conditions:
          - condition: template
            value_template: >
              {{ command == 'color_temperature_step_up' }}
        sequence:
          - variables:
              delta_value: "{{ step_size }}"
          - alias: "Execute color rotation action"
            sequence: !input color_rotation_action

      # Handle color rotation action down
      - conditions:
          - condition: template
            value_template: >
              {{ command == 'color_temperature_step_down' }}
        sequence:
          - variables:
              delta_value: "{{ - step_size }}"
          - alias: "Execute color rotation action"
            sequence: !input color_rotation_action

      # Handle click action
      - conditions:
          - condition: template
            value_template: >
              {{ command == 'toggle' }}
        sequence:
          - alias: "Execute click action"
            sequence: !input click_action

    default:
      - action: logbook.log
        data:
          name: "Smart Knob Debug"
          message: "Unhandled command: {{ command }}"

[edit] I have learned you also need to protect against a from_state that is unavaible, this sometimes happens pontaneously (I don’t know why) and on restarts of z2mqtt.

1 Like

Hello.
I’m trying out this blueprint but I cannot get the rotation to work. The click to turn the lights on and off does work however.
I think I’ve followed the necessary steps but I guess I’m doing something wrong.
I can see “Smart knob custom” in Z2M for the knob’s description.
I’m attempting to control a Z2M group of lights called “bedroom_lights”

This is my yaml for the automation. Any help is appreciated!

alias: Bedroom Knob Test
description: ""
use_blueprint:
  path: TheUnlimited64/blueprint.yaml
  input:
    brightness_sensor: sensor.bedroom_knob_action_brightness_delta
    click_sensor: sensor.bedroom_knob_action
    rotation_action:
      - target:
          entity_id: light.bedroom_lights
        data:
          brightness_step_pct: "{{ delta_value * 10 }}"
          transition: 0.5
        action: light.turn_on
    click_action:
      - target:
          entity_id: light.bedroom_lights
        action: light.toggle
        data:
          transition: 0.2

Hello. I also have problems with setting my knob. Perhaps someone can help me.
I have installed the external converter successfully. The description and the picture of the knob in the MQTT UI changed to “Smart knob custom”

The exact knob model I am having is reported in MQTT as: _TZ3000_402vrq2i

the exposed entities in MQTT are:


The blueprint is also installed in the folder and responding when I create an automation.
The problem is I don’t see the click action (sensor.knob_action) in the suggested entities:

Then I tried also the updated blueprint with color control and also no color entity is visible:


And so with this automation - the knob is not responding neither to click nor to rotate.
What am I doing wrong ?

You’re not doing anything wrong. Since the end of last year, Z2M was updated to v2.0.0 and the action sensor was disabled by default as one of the breaking changes.

You have 2 options:

  • Set legacy_action_sensor: true in the Z2M config (it’s also available as an option in the Z2M Settings page)
  • Use the new Z2M events for your automation as shown in this post instead.

I just wen through the story of setting up this knob - finally got there - thank you all!

For anyone looking for help - once you got the knob configured as in the docs (still need the converter), use template from sophof post from above (Tuya smart knob improved blueprint for Zigbee2Mqtt - #13 by sophof), add it as a new template (Creating an automation blueprint - Home Assistant), reload your automations and create an automation with the blueprint via GUI - fill in all actions related to the knob itself and then just the click action and save. Then edit it with YAML to fill in the “custom” stuff and the rotations. Here’s my YAML of the automation:

alias: Garderoba - Universal Smart Knob Control - v2
description: ""
use_blueprint:
  path: MyOwn/Smart Knob.yaml
  input:
    brightness_delta_sensor: sensor.garderoba_wylacznik_action_brightness_delta
    step_size_sensor: sensor.garderoba_wylacznik_action_step_size
    event_entity: event.garderoba_wylacznik_action
    click_action:
      - action: light.toggle
        target:
          entity_id:
            - light.garderoba_swiatla_grupa
        data:
          transition: 1
    rotation_action:
      - action: light.turn_on
        target:
          entity_id:
            - light.garderoba_swiatla_grupa
        data:
          brightness: >
            {% set current = state_attr('light.garderoba_swiatla_grupa',
            'brightness') | int %} {% set new = current + delta_value | int %}
            {% set min_brightness = 5 %} {% set max_brightness = 255 %} {{
            [min_brightness, [new, max_brightness]|min]|max }}
          transition: 1
    color_rotation_action:
      - action: light.turn_on
        target:
          entity_id:
            - light.garderoba_swiatla_grupa
        data:
          color_temp: >
            {% set current = state_attr('light.garderoba_swiatla_grupa',
            'color_temp') | int %} {% set new = current + delta_value | int %}
            {% set min_ct = 250 %} {% set max_ct = 454 %} {{ [min_ct, [new,
            max_ct]|min]|max }}
          transition: 2
    sensitivity_brightness: 2
    sensitivity_color: 2