Automation based on changing Attribute, how do I select one field in the attribute

Hi
Would appresiate some help, I am trying to create and automation based on an attribute of an entity. So I can create the automation, select the entity and the attribute. But the attribute contains several fields and option in each field.
I want to use the value of a single field. Don’t know how to do that ???
This is a four button remote, the state of each button is sent at the same time, with the value changing for the button pressed.

The complete set of attributes are as follows -

addr: 7c:c6:b6:74:cf:d5
rssi: -70
local_name:
service_data:
encryption: false
BTHome_version: 2
pid: 81
battery: 100
button:

  • 1
  • 0
  • 0
  • 0

I can select an attribute of ‘service_data’ form the drop down menu in the GUI.
But I need to isolate the value of the sub catagory button and the value of one of the 4 buttons.

Any help greatly appresiated, thank you

Which integration is used for this remote-control?

I hope this answers your question.
You want to use a template.

For example:
{{ states("sun.sun") }} becomes “above_horizon” in your automation.
{{ state_attr("sun.sun", "next_rising") }} becomes “2025-02-10T11:47:28.217968+00:00”

Hi
So this is a shelly Bluetooth RC Button 4, the bluetooth messages are converted to mqtt by local Shelly plug, and arrive as a topic in HA via mqtt.
strange way to go I know but it fits my purpose as all my devices are mqtt.

Hi Thank you for the reply all info is helpful, I just need more experiance at interpreting it !!

So from your reply and other diging arround I have got alot further, using ‘Template’ under the developer settings I have constructed an expresion to isolate the value I am after.

{{state_attr(‘sensor.shelly_brc4_bed_3_analog’,‘service_data’)[‘button’].0 }}

So using that I can see the individial button press. Next issue is how to include that in an automation so that it Triggers on a change of value from 254 to 1.

To better expalain when you press the button the value will change to ‘254’ when you release the button the value changes to ‘1’ for a short press ‘2’ for double press etc.

Any thoughts !
thank you.

And yes to any one reading this their are probable easier ways to make this work but if I can get it working this way it would be great.

Interesting.

Does the conversion process from Bluetooth to MQTT include the ability to support Home Assistant’s MQTT Discovery?

Because if it does then the remote-control may be present in Home Assistant as a device or, if not that, possibly as an event entity.

Go to Settings → Devices & Services and locate MQTT. Under its title you’ll see the number of MQTT-based devices. Click that number and then examine the resulting list for anything that resembles your remote-control.

Alternately, go to Developer Tools → States, type event. in the Entity field and see if it lists an event entity corresponding to your remote-control.

  • If there’s a device or event entity representing your remote-control then it becomes easier to create a trigger to detect the remote-control’s button events.

  • If neither exists then you’ll probably need to use a Template Trigger to detect button events.

Hi So no to most of that all i get is the states from the manuel configured mqtt entry.

So I think you correct a Template Trigger it is…
Thanks for the reply

You’re looking for way to detect the value’s change from 254 to 1. A Template Trigger can’t do that because it has no access to the previous value. At best, you can use a Template Trigger to detect when the value changes to 1 but not a specific change from 254 to 1.

Ok that’s a shame, because that would have been ideal. Never mind, do you know if it will work with other values so changing to 1 in first automation and a seperate automation when it changes to 2 ?
thanks for the reply

That’s why I had asked to check if there was a device or event entity because it simplifies the detection of button events. Anyways, you’ve confirmed they don’t exist so you’re obliged to use sensor.shelly_brc4_bed_3_analog.

You can use multiple triggers in the same automation, each trigger detecting a specific button event. Assign each trigger a unique trigger id and then use a choose in the actions to determine which trigger fired based on its trigger id.


EDIT

Go to Settings → Devices & Integrations and check the Shelly integration for anything related to your remote-control.

I was wondering where the “254” came from.
But as @tcruiser60 said, an automation can only detect a change to or a change from, but not both.

So, “254” means the button is down, so you can ignore that. When you release it you get a “1”, so trigger on the value change to 1.

Hi So first a thank you to all who contributed, I now have a working solution -

This is using a Shelly Pro Plug with Bluetooth Prox turned on and running the Shelly script, ‘universal-blu-to-mqtt.js’ from the Shellly libuary.

This takes any bluetooth message and relay it via mqtt to the broker configured.

Messages look like this for the Shelly Blu RC4-

7c:c6:b6:74:cf:d5 : msg : Object
object
topic: “7c:c6:b6:74:cf:d5”
payload: object
addr: “7c:c6:b6:74:cf:d5”
rssi: -53
local_name: “”
service_data: object
encryption: false
BTHome_version: 2
pid: 83
battery: 100
button: array[4]
0: 254
1: 0
2: 0
3: 0

qos: 0
retain: false
_topic: “7c:c6:b6:74:cf:d5”
_msgid: “06776fa45ab6feb6”

I then created a 2 sensors in configuration.yaml, one for the battery and the other for any message.

# --------------------------------------- Shelly Blue RC Button 4 ---------------------

      - name: "ShellyRC4_Bed3_Battery"
        unique_id: "shellyRC4-7c:c6:b6:74:cf:d5_BAT"
        state_topic: "7c:c6:b6:74:cf:d5"
        value_template: '{{ value_json.service_data.battery }}'
        json_attributes_topic: "7c:c6:b6:74:cf:d5"
        json_attributes_template: '{{ value_json | tojson }}'
        unit_of_measurement: "%"
        device_class: battery

      - name: "Shelly BRC4 Bed 3 analog"
        unique_id: "shellyRC4-7c:c6:b6:74:cf:d5-A"
        state_topic: "7c:c6:b6:74:cf:d5"
        json_attributes_topic: "7c:c6:b6:74:cf:d5"

Once this was done the hard part was isolating each button and press in an automation. I now have one automation containing 20 triggers with ID’s to cover each button, the 4 different press types ( single,double,tripple,long press and release).

I created the automation from from the GUI but have included the code below for refferance, essentially I created a trigger from each value for each button press, the Long press uses the ‘254’ value for more than one second , button held down, then last trigger looks for the release of the button which is a state value of 4, that way Ican use the long press for Dimming, or any thing which needs to increase/decrease.

Buttons are numbered 0,1,2,3 for buttons 1-4.
single press is 1
double press is 2
tripple press is 3
Long press is 4

In each case when the button is pressed down the value for that button is 254, hence why I look for the 254 value for more than 1 second to indicate a long press then end the action with the release value of 4.

  alias: automate rc4b1
  description: ''
  triggers:
  - triggers:
    - trigger: template
      value_template: '{{state_attr(''sensor.shelly_brc4_bed_3_analog'', ''service_data'')[''button''].0
        == 1}}'
      id: Single_Press
    - trigger: template
      value_template: '{{state_attr(''sensor.shelly_brc4_bed_3_analog'', ''service_data'')[''button''].0
        == 2}}'
      id: Double_Press
    - trigger: template
      value_template: '{{state_attr(''sensor.shelly_brc4_bed_3_analog'', ''service_data'')[''button''].0
        == 3}}'
      id: Tripple_Press
    - trigger: template
      value_template: '{{state_attr(''sensor.shelly_brc4_bed_3_analog'', ''service_data'')[''button''].0
        == 254}}   '
      id: Long_Press
      for:
        hours: 0
        minutes: 0
        seconds: 1
    - trigger: template
      value_template: '{{state_attr(''sensor.shelly_brc4_bed_3_analog'', ''service_data'')[''button''].0
        == 4}}   '
      id: Long_Press_Release
  conditions: []
  actions:
  - choose:
    - conditions:
      - condition: trigger
        id:
        - Single_Press
      sequence:
      - action: light.toggle
        target:
          entity_id: light.bed_4_beam_led
        data: {}
  - choose:
    - conditions:
      - condition: trigger
        id:
        - Long_Press
      sequence:
      - action: light.turn_on
        metadata: {}
        data:
          brightness_step_pct: 30
        target:
          entity_id: light.bed_4_beam_led
      - repeat:
          sequence: []
          until:
          - condition: trigger
            id:
            - Long_Press_Release

Any way case closed, hope this helps some one else, and yes their are easier ways to do this, but this fits my purpose maybe it will yours too, enjoy …