Use zwave trigger value in action for brightness

Recently I switched from a Fibaro HC2 to Home Assistant (the Fibaro broke down). I used to have some scripts on the HC2 to adjust the brightness of my Hue lights according to the value of the Fibaro Dimmer 2. However, I cannot seem to read the value of the dimmer in the Service call to set the brightness of the Hue lights.

Below the current version of the YAML:

alias: GameKamer_HueBrightnessChangeWithDimmer
description: ""
trigger:
  - platform: device
    device_id: be22d8ca284821c12248ff5afeea586f
    domain: zwave_js
    type: zwave_js.value_updated.value
    command_class: 32
    property: currentValue
    endpoint: "1"
condition: []
action:
  - target:
      entity_id: d5db0a5ec03108ba2114a73ac063c6ad
    data:
      brightness_pct: "{{ 'trigger.current_value' | float }}"
    action: light.turn_on
mode: single

This will give an error in the trace:
Error: Error rendering data template: ValueError: Template error: float got invalid input ‘trigger.current_value’ when rendering template ‘{{ ‘trigger.current_value’ | float }}’ but no default was specified

When I add a default for the float, it works, but that is not what I want.

In the ‘Changed variables’ tab of the trace I see the following values, where current_value is the one I need:

this:
  entity_id: automation.gamekamer_flashlightsonvaluechange
  state: 'on'
  attributes:
    id: '1723233019747'
    last_triggered: '2024-08-09T20:19:10.377505+00:00'
    mode: single
    current: 0
    friendly_name: GameKamer_HueBrightnessChangeWithDimmer
  last_changed: '2024-08-09T20:17:20.581666+00:00'
  last_reported: '2024-08-09T20:19:10.382063+00:00'
  last_updated: '2024-08-09T20:19:10.382063+00:00'
  context:
    id: 01J4WCQ3X8YV3MTNRGWNHKWE7N
    parent_id: null
    user_id: null
trigger:
  id: '0'
  idx: '0'
  alias: null
  platform: zwave_js.value_updated
  device_id: be22d8ca284821c12248ff5afeea586f
  node_id: 3
  command_class: 32
  command_class_name: Basic
  property: currentValue
  property_name: currentValue
  endpoint: 1
  property_key: null
  property_key_name: null
  previous_value: 80
  previous_value_raw: 80
  current_value: 80
  current_value_raw: 80
  description: >-
    Z-Wave value 3-32-1-currentValue updated on Fibaro Dimmer 2 Zolder Links
    Main

I have also tried to change the variable I try to read in the brightness_pct property to ‘trigger.to_state.state’ but that also gives the same error.

I have read a lot of post on this community and Automation YAML - Home Assistant but I think I still miss some of the logic.

Does anyone know what I do wrong here?

Don’t quote trigger.current_value. You need the variable, not a string with its name.

brightness_pct: "{{ trigger.current_value | float(0) }}"
1 Like

Hello VincentHomeAssistant,

Device triggers do not supply trigger variables. The only trigger variable it provides is trigger.platform.
Trigger.id and trigger.idx are there as well, but nothing else.
Automation trigger variables - Home Assistant.
If you want to make this work, use state triggers
Why and how to avoid device_ids in automations and scripts.

1 Like

Thanks to both @Sir_Goodenough and @Edwin_D I got this to work.

Ended up with the code below and it works like a charm:

alias: GameKamer_HueBrightnessResponseToDimmerChange
description: ""
trigger:
  - platform: state
    entity_id: light.dimmer_2_zolder_links
    attribute: brightness
    not_to:
      - unavailable
      - unknown
    not_from:
      - unavailable
      - unknown
condition: []
action:
  - target:
      entity_id: light.gamekamer
    data:
      brightness_pct: "{{ (100/255)*trigger.to_state.attributes.brightness | float }}"
    action: light.turn_on
mode: single

This is what the docs say, but the automation trace suggested the value was there. Nonetheless: the new solution is a much safer one, because each device brand/model can probably can decide to return different information.

What is now used is documented, so should work in any similar situation. It is part of why it is best to avoid device triggers if you can. So Sir_Goodenough’s advice is a good one.