Get nested attributes from Switch attributes

I’ve got a switch that reports its power usage etc via mqtt. The switch is setup and working in HA, along with its data. Now, I need to grab some of that data and have it visible in HA for use with automations.

The data (watts, volts, amps) is available in the following format:

How can I grab the values presented as dps 18,19 and 20?

I’ve tried the following:

- platform: template
  sensors:
    test_tuya_dryer_volts:    
      friendly_name: Test Tuya Dryer Volts
      unit_of_measurement: 'v'
      entity_id:
        - switch.tuya_dryer
      value_template: "{{ state_attr('switch.tuya_dryer', 'dps')[4] }}"
      icon_template: 'mdi:flash'

as well as variations such as:

value_template: "{{ state_attr('switch.tuya_dryer', 'dps').20 }}"
value_template: "{{ state_attr('switch.tuya_dryer', 'dps')[0].20 }}"

All these efforts just return a sensor with an unavailable status and none of the desired data.

Any suggestions as to what I need to do?

Thanks.

It would help if you entered the following in the template editor and posted the results so we could see the actual structure of the data:

{{ state_attr('switch.tuya_dryer', 'dps') }}

But I’ll take a guess that this might work:

- platform: template
  sensors:
    test_tuya_dryer_volts:    
      friendly_name: Test Tuya Dryer Volts
      unit_of_measurement: 'v'
      value_template: "{{ state_attr('switch.tuya_dryer', 'dps')['18'] }}"
      icon_template: 'mdi:flash'
1 Like

Thanks!

That appears to have done the job.

1 Like