Set a sensor state based on an event property not working

I’m trying to set a sensor state based on an event triggered by a Fibaro “The Button”.

The button fires the following events (# with possible other values)

event_type: zwave_js_value_notification
data:
  domain: zwave_js
  node_id: 31
  home_id: 4255010352
  endpoint: 0
  device_id: 47bc48ea857706558f2ef1640bb61893
  command_class: 91
  command_class_name: Central Scene
  label: Scene 001
  property: scene
  property_name: scene
  property_key: "001"
  property_key_name: "001"
  value: KeyPressed      # Detected values KeyPressed, KeyPressed2x, KeyPressed3x, KeyPressed4x, KeyHeldDown, KeyReleased
  value_raw: 0           # Detected values 0,3,4,5,2,1
origin: LOCAL
time_fired: "2023-03-22T13:54:26.651783+00:00"
context:
  id: 01GW4SJZ6VAEQC25B6NAJJEXZF
  parent_id: null
  user_id: null

To achieve this, I created a dummy sensor in sensor.yaml

- platform: template
  sensors:
    btn_1_pressed:
      friendly_name: 'Button pressed'
      value_template: 0

The automation that should make this possible is

- id: 'capture_btn_1_events'
  alias: Button 1 Pressed
  description: ''
  trigger:
  - platform: event
    event_type: zwave_js_value_notification
    event_data:
      device_id: 47bc48ea857706558f2ef1640bb61893
      command_class: 91
      property: "value_raw"
  action:
    entity_id: btn_1_pressed
    state: "{{ trigger.event.data }}"

However, it’s not working. The button state is not updated. I’m a relatively newb in this, so your help is appreciated. My guess is that the action state does not have the right syntax.

You are not calling any service (action) here:

There is no service to do this. Sensors update through their integration, not manually.

You can however create a triggered template sensor that changes according to events. See:

https://www.home-assistant.io/integrations/template/#trigger-based-template-binary-sensors-buttons-numbers-selects-and-sensors

Yes! Got it working, thx for this. The final code is:

template:
  - trigger:
      - platform: event
        event_type: "zwave_js_value_notification"
        event_data:
          device_id: 47bc48ea857706558f2ef1640bb61893
          command_class: 91
    sensor:
      - name: "btn_1_pressed"
        state: "{{ trigger.event.data.value_raw }}"

Just out of curiosity, I like to keep my setup clean. Can I put this code in my sensor.yaml or keep it in configuration.yaml. If in sensor.yaml, I got an error, what syntax should I use?

Since the top level key is template it cannot go in the sensor.yaml file. If you want, you can add a template.yaml file, making sure to properly include it in your configuration.yaml.

Splitting the Config

You may also be interested in Packages

Thx for your help. For future reference I will include my last working yaml code here (templates.yaml). I needed to split the sensors, one to trigger the button pressed and one to read how many times it was pressed.

- trigger:
  - platform: event
    event_type: "zwave_js_value_notification"
    event_data:
      device_id: 47bc48ea857706558f2ef1640bb61893
      command_class: 91
  binary_sensor:
    - name: "btn_1_pressed"
      state: "true"
      auto_off: 5
  sensor:
    - name: "btn_1_value"
      state: "{{ trigger.event.data.value_raw }}"