How to filter by device attribute in a template filter / query?

Hi,

I want to find all switches manufactured by Aeotec and a specific model number (I’m writing an automaton to set their configuration properties).

This works, but I want the two device attribute checks as part of the filter query if that makes any sense? A single query, ideally, that produces entity_id list (comma separated) that I’ll pass to the zwave_js_configure method. Thanks!!!

{{ states.switch
  | selectattr('entity_id', 'in', integration_entities('zwave_js'))
  | map(attribute='entity_id')
  | list
}}

{{ is_device_attr('switch.dishwasher', 'manufacturer', 'Aeotec Ltd.') }}
{{ is_device_attr('switch.dishwasher', 'model', 'ZWA023') }}

OK update… this correctly gets the models:

{{ states.switch
  | selectattr('entity_id', 'in', integration_entities('zwave_js'))
  | map(attribute='entity_id')
  | map('device_attr', 'model')
  | list
}}

So I somehow just need to be able to add those to the filter of the entity IDs.

The first one in the first post returns:

['switch.dishwasher', 'switch.water_heater', 'switch.holiday_lights_switch', 'switch.garage_dehumidifier', 'switch.christmas_tree', 'switch.espresso_machine', 'switch.christmas_train', 'switch.titan_water_valve_actuator']

The one in this post returns:

['ZWA023', 'ZWA023', 'ZWA023', 'ZWA023', 'ZWA023', 'ZEN15', 'ZWA023', 'ZAC36']
1 Like

Copy-paste the following template into the Template Editor and confirm it reports the information you want.

{% set ns = namespace(switches=[]) %}
{% for s in states.switch
    if is_device_attr(s.entity_id, 'manufacturer', 'Aeotec Ltd.')
    and is_device_attr(s.entity_id, 'model', 'ZWA023') %}
  {% set ns.switches = ns.switches + [s.entity_id] %}
{%- endfor %}
{{ ns.switches }}
1 Like

Thank you kindly @123 I appreciate you!

1 Like

FYI, is_device_attr is a test.

{{ states.switch 
         | map(attribute='entity_id') 
         | select('is_device_attr', 'manufacturer', 'Aeotec Ltd.')
         | select('is_device_attr', 'model', 'ZWA023') | list }}
3 Likes

Ah beautiful @petro thank you! That is clean!

I wasn’t sure if I can mark both of yours a solution. I think they are both solutions. I will probably do what @petro put because it’ll be very clean passing it to the service to set the settings.

Thank you both so much!!

Here is the script if anyone is interested (not yet using the new stuff from this thread):

script:
  configure_zwave_switches:
    variables:
      target_switch_entity_id: switch.dishwasher
    sequence:
      # https://aeotec.attachments7.freshdesk.com/data/helpdesk/attachments/production/6118457833/original/ES%20-%20Smart%20Switch%207%20US.pdf.pdf?response-content-type=application%2Fpdf&Expires=1687753514&Signature=gQwDCGUEIANMeZ3LQ8cy5zJ6Fv~sDymabo3yhKfFcV5LgGhnSOid~Gvl7-GZ71agny6gZGmO~0tMpp~xKe1Zo-hEXVT5msYHc5rD13rSFBwIr3Ck3U3dYZQ6NNnmPpJMi-EYO0EK1QbHhWR~hHOZD62TdLpfiEEz~FKYYj6cZL1qNcNffU5IX6tIUE13Q7cFMDx318iYMaGz6y3r33Zd1nIQQOh-CSZcxRlrz0sh2L~-8UY4abWWEhu~2JTel9YueitdyhbWpM~FIz--pegd3uPgy~3BZZG8V9-0WstuDTKC3RArURP25zvGNWT6KTMHhNsHlZBBYrNTa8cZSp094A__&Key-Pair-Id=APKAJ7JARUX3F6RQIXLA
      - service: zwave_js.set_config_parameter
        target:
          entity_id: "{{ target_switch_entity_id }}"
        data:
          parameter: 0x08 # Power Out Action
          value: 0 # Last Status
      - delay: "00:00:02"
      - service: zwave_js.set_config_parameter
        target:
          entity_id: "{{ target_switch_entity_id }}"
        data:
          parameter: 0x13 # Threshold Check Time
          value: 5 # Seconds
      - delay: "00:00:02"
      - service: zwave_js.set_config_parameter
        target:
          entity_id: "{{ target_switch_entity_id }}"
        data:
          parameter: 0x14 # kWh Report Threshold
          value: 500 # kWh
      - delay: "00:00:02"
      - service: zwave_js.set_config_parameter
        target:
          entity_id: "{{ target_switch_entity_id }}"
        data:
          parameter: 0x15 # Watt Report Threshold
          value: 25 # Watts
      - delay: "00:00:02"
      - service: zwave_js.set_config_parameter
        target:
          entity_id: "{{ target_switch_entity_id }}"
        data:
          parameter: 0x16 # Current Report Threshold
          value: 1 # Amps
      - delay: "00:00:02"
      - service: zwave_js.set_config_parameter
        target:
          entity_id: "{{ target_switch_entity_id }}"
        data:
          parameter: 0x17 # Watt Automatic Report Time
          value: 0 # Disabled
      - delay: "00:00:02"
      - service: zwave_js.set_config_parameter
        target:
          entity_id: "{{ target_switch_entity_id }}"
        data:
          parameter: 0x18 # kWh Automatic Report Time
          value: 0 # Disabled
      - delay: "00:00:02"
      - service: zwave_js.set_config_parameter
        target:
          entity_id: "{{ target_switch_entity_id }}"
        data:
          parameter: 0x19 # Voltage Automatic Report Time
          value: 0 # Disabled
      - delay: "00:00:02"
      - service: zwave_js.set_config_parameter
        target:
          entity_id: "{{ target_switch_entity_id }}"
        data:
          parameter: 0x1A # Current Automatic Report Time
          value: 0 # Disabled
      - delay: "00:00:02"
      - service: zwave_js.set_config_parameter
        target:
          entity_id: "{{ target_switch_entity_id }}"
        data:
          parameter: 0x28 # Automatic Off Timer
          value: 0 # Disabled
      - delay: "00:00:02"
      - service: zwave_js.set_config_parameter
        target:
          entity_id: "{{ target_switch_entity_id }}"
        data:
          parameter: 0x29 # Automatic On Timer
          value: 0 # Disabled

Just keep it as what 123 had. I was just merely pointing out to him that it can be used as a test.

1 Like

As a sidebar, you can make your script a bit easier to manage.

script:
  configure_zwave_switches:
    variables:
      target_switch_entity_id: switch.dishwasher
      items:
      - parameter: 0x08 # Power Out Action
        value: 0 # Last Status
      - parameter: 0x13 # Threshold Check Time
        value: 5 # Seconds
      - parameter: 0x14 # kWh Report Threshold
        value: 500 # kWh
      - parameter: 0x15 # Watt Report Threshold
        value: 25 # Watts
      - parameter: 0x16 # Current Report Threshold
        value: 1 # Amps
      - parameter: 0x17 # Watt Automatic Report Time
        value: 0 # Disabled
      - parameter: 0x18 # kWh Automatic Report Time
        value: 0 # Disabled
      - parameter: 0x19 # Voltage Automatic Report Time
        value: 0 # Disabled
      - parameter: 0x1A # Current Automatic Report Time
        value: 0 # Disabled
      - parameter: 0x28 # Automatic Off Timer
        value: 0 # Disabled
      - parameter: 0x29 # Automatic On Timer
        value: 0 # Disabled
  sequence:
    - repeat:
        for_each: "{{ items }}"
        sequence:
        - service: zwave_js.set_config_parameter
          target:
            entity_id: "{{ target_switch_entity_id }}"
          data: "{{ repeat.item }}"
        - delay: "00:00:02"

EDIT: Spacing was off, fixed.

1 Like

Wow this is awesome Petro! Thanks so much I didn’t know you could do this!

I’ll add in the multiple entity IDs as well from our previous conversation in this thread. I’m hoping it doesn’t generate too much traffic. I have the 2 second delay since I’ve noticed sending lots of Z-Wave commands tends to slow things down. Under the hood, does the set_config_parameter script have a “for” loop over all the entity IDs that you pass it, if you pass multiple? Hopefully that still works OK. I tested the script last night on the single entity ID and it worked well.

No unfortunately it doesn’t.

Why are you setting the parameters so often?

Adding multiple entities may cause a problem, I’d have to test and see what it does.

Thanks @petro . From the documentation, it does say that you can pass multiple entities. Can you not pass multiple entities? I’ll try it out, but if it only works with 1, maybe we need to update the docs?

Basically what I am doing, I am going to have this script run about once a week or so. I’ve found that I get new plugs every so often, and the default on these is actually a 0 watt and 0 amp change to send a Z-wave signal, so I noticed that VERY often (every few seconds) the plugs would be updating the Z Wave hub even if there was no change. This clogs the Z-wave network.

So I wanted a script that would replace my manual config in the Z-wave settings page (you can do what I’m doing from the UI).

The other benefit is that if I have to factory reset a device, I can restore my preferred settings repeatedly and without error.

I plan to make one of these scripts for each type of Z-wave plug I have that measures current.

I hope that makes sense. LMK if you think it’s a bad idea. I wouldn’t call it often, maybe not even automate it, but can do it if I change my setup and then it sets across the board.

You can, but it might just flood your network.

I’d just make this a script that you run once. Add a device, run the script. But personally, I’d make it only update what needs updating.

1 Like

Ah got it so you’re saying it doesn’t do it sequentially / wait for each to finish before moving on, makes sense! Thanks so much Petro!