#Triggering on a rotary knob that sends level in the payload

1 messages · Page 1 of 1 (latest)

open lagoon
#

Trying to figure out how to make my lights increase \ decrease based on the payload in zigbee2mqtt

Every time I turn the know I get an updated level in the payload, so I guess I need to have this a conditional somehow where "if number greater than last, then increase light" and "if number smaller than last then decrease light"
Anyone done an automation like that before ? Couldn't find any good hints in the forums

[2025-03-14 11:15:43] info: z2m:mqtt: MQTT publish: topic 'zigbee2mqtt/Stue Rotor', payload '{"action":"brightness_move_to_level","action_group":64257,"action_level":25,"action_transition_time":0,"battery":200,"last_seen":"2025-03-14T10:15:43.126Z","linkquality":61,"update":{"installed_version":22,"latest_version":22,"state":"idle"},"voltage":3300}'

final igloo
#

Is action_level the parameter you are wanting to look at? And you’re saying you don’t care about the absolute value, instead you only care whether it is larger or smaller than the precious value?

#

If you need to compare to the previous, then you have to store the previous, and so you need a trigger-based template sensor or else you need an input helper and an automation

#

So I’d recommend a trigger-based template sensor using:

  • trigger = mqtt trigger
  • state = {{ now() }}
  • attributes:
    • action_level = {{ trigger.payload_json.action_level }}
    • change = {{ trigger.payload_json.action_level | float - this.attributes.action_level | float(0) }}
#

Then create your light automation and trigger with a state trigger referencing this new sensor. Leave the options blank so it triggers on any state or attribute change. Then you can do a choose in the actions, and use a numeric state condition for each option. The first would be if the change attribute is greater than 0, and the second option of the choose would be if change is less than zero

open lagoon
#

Hmmm interesting, don't think I've done anything like that before, thanks for the pointers and I'll just have to try

Yes action level is the Rotary knob position

final igloo
#

ok, this is tested and will work with the payload you posted. However, this assumes the payload to the topic will always be json that contains action_level. If that isn't always true, then this would need to be updated to handle that scenario

template:
  - trigger:
      - platform: mqtt
        topic: "zigbee2mqtt/Stue Rotor"
    sensor:
      - name: "Rotor Change"
        unique_id: 01959cc4-f30d-747b-b66c-5b40048dc89d
        device_class: timestamp
        state: >
          {{ now() if trigger.payload_json.action_level | int(0) != this.attributes.get('action_level', none) else this.state }}
        attributes:
          action_level: >
            {{ trigger.payload_json.action_level | int }}
          change: >
            {{ trigger.payload_json.action_level | int - this.attributes.get('action_level', 0) | int(0) }}
final igloo
#

Just for kicks, here's the alternative which is an automation, and you'll also need to create an input_number helper (in this example I called it input_number.zigbee_knob_action_level )

alias: Brightness Change Based on Rotary Knob
description: ""
mode: queued
max: 10
triggers:
  - trigger: mqtt
    topic: zigbee2mqtt/Stue Rotor
conditions: []
actions:
  - variables:
      change: >
        {{ trigger.payload_json.action_level | int(0) - states('input_number.zigbee_knob_action_level') | int }}
  - choose:
      - conditions:
          - "{{ change > 0 }}"
        sequence:
          - action: light.turn_on
            data:
              brightness_step_pct: 10
            target:
              entity_id: light.my_light
      - conditions:
          - "{{ change < 0 }}"
        sequence:
          - action: light.turn_on
            data:
              brightness_step_pct: -10
            target:
              entity_id: light.my_light
  - action: input_number.set_value
    data:
      value: "{{ trigger.payload_json.action_level | int(0) }}"
    target:
      entity_id: input_number.zigbee_knob_action_level