Help creating automations that include API calls, a dynamic value(calculated) and some state refreshing

Hi everyone,

Trying hard to get automations working, where I will use my Z-Wave connected wall switches(1-button currently) to control my Hue bulbs that are connected through deCONZ. What I got till now works for clicking to toggle on/off, double-click to turn on at 100% and holding to dim either way on the same button.
So replacing the “???” for transitiontimevariable with, for example, 50, makes it work - although with inconsistent dimming speed.

So the problem is the inconsistent speed of the transition, and actually also sometimes what direction it goes in. It seems to me that the problem relates(at least partly) to the brightness value in Home Assistant not being updated from deCONZ fast enough(polling?). So using states.light.light1.attributes.brightness does not seem viable.

I experience that it does not always figure out that brightness is above or below 157, and then goes the wrong direction. Also trying to calculate a transitiontime from states.light.light1.attributes.brightness does not seem to be working for me.

I also see some inconsistency in missed presses and the likes of that, but that might also be down to Z-Wave. I unfortunately have no clue how to optimize that though, but the device might be set up to send way more messages than the events needed by Home Assistant.

Any ideas about how I should go from here? I would be happy to receive suggestions for:
1.) How to calculate transitiontime within the automations, and ensure a live value for brightness is used. A transitiontime for starters could be 51 brightness per second(to replicate Fibaro Dimmer 2 that dims 100% / 255 in 5 seconds). Changing the exact value should be easy afterwards though I assume.
Maybe the value can be found in the API call?
Maybe another API needs to be made before initiating the automation to get the value?
2.) How to optimize a Z-Wave node to be faster in this type of application.
3.) Generally if there are changes that could be made to make the automations better/faster/simpler.

This is what I got so far:

 rest_command:
  light1stop:
    url: 'http://myip:port/api/XXXXXXXXXX/lights/1/state'
    method: PUT
    headers:
      content-type: application/json
    payload: '{"bri_inc":0}'
  light1startup:
    url: 'http://myip:port/api/XXXXXXXXXX/lights/1/state'
    method: PUT
    headers:
      content-type: application/json
    payload: '{"on": true,
              "bri": 255,
              "transitiontime": "{{ transitiontimevariable }}"}'
  light1startdown:
    url: 'http://myip:port/api/XXXXXXXXXX/lights/1/state'
    method: PUT
    headers:
      content-type: application/json
    payload: '{"on": true,
              "bri": 1,
              "transitiontime": "{{ transitiontimevariable }}"}'
 
 automation:
    - alias: 'light1 - brighten'
    trigger:
    - event_data:
        entity_id: zwave.fibaro_system_fgs223_double_relay
        node_id: 24
        scene_id: 2
        scene_data: 2
        event_type: zwave.scene_activated
        platform: event
    condition:
        condition: or
        conditions:
        - condition: template
            value_template: '{{ states.light.light1.attributes.brightness < 157 }}'
    action:
      - service: rest_command.light1startup
        data:
          transitiontimevariable: ????????

    - alias: 'light1 - dim'
    trigger:
    - event_data:
        entity_id: zwave.fibaro_system_fgs223_double_relay
        node_id: 24
        scene_id: 2
        scene_data: 2
        event_type: zwave.scene_activated
        platform: event
    condition:
        condition: or
        conditions:
        - condition: template
            value_template: '{{ states.light.light1.attributes.brightness > 157 }}'
    action:
      - service: rest_command.light1startdown
        data:
          transitiontimevariable: ????????

    - alias: 'light1 - brighten'
    trigger:
    - event_data:
        entity_id: zwave.fibaro_system_fgs223_double_relay
        node_id: 24
        scene_id: 2
        scene_data: 1
        event_type: zwave.scene_activated
        platform: event
    condition: []
    action:
      - service: rest_command.light1stop

    - alias: 'light1 - toggle'
    trigger:
    - event_data:
        entity_id: zwave.fibaro_system_fgs223_double_relay
        node_id: 24
        scene_id: 2
        scene_data: 0
        event_type: zwave.scene_activated
        platform: event
    condition: []
    action:
        service: light.toggle
        entity_id: light.light1

    - alias: 'light1 - on full'
    trigger:
    - event_data:
        entity_id: zwave.fibaro_system_fgs223_double_relay
        node_id: 24
        scene_id: 2
        scene_data: 3
        event_type: zwave.scene_activated
        platform: event
    condition: []
    action:
        service: light.turn_on
        entity_id: light.light1
        data_template:
        brightness: 255

Just updated the entire thread, as I made a lot of progress on this. However, I am now stuck trying to finish it. Hope someone can chip in.

How about using an input number to track your target brightness, use the script/automations to change that, and set the brightness of the light to that?

It should be simple enough to tweak this cookbook entry to do that.

Hmm, I do not think I quite understand how that would work.
So I would have an input number that changes between 0 and 255, to switch direction of dimming?