"zha-toolkit" - a big set of Zigbee commands on top of ZHA/zigpy

Thanks for the feedback,

correct me if I’m wrong but the second option would require to manually create additional sensors, which I’m trying to avoid.

I’ve tried the first option, but the attribute value won’t accept a template.
My syntax could definitely be wrong so feel free to comment; here’s the full automation:

alias: THERMOSTATS sensors read floor temp zzz2
description: ""
trigger:
  - platform: time_pattern
    minutes: /10
condition: []
action:
  - repeat:
      for_each:
        - climate.thermostat1_thermostat
        - climate.thermostat2_thermostat
        - climate.thermostat3_thermostat
        - climate.thermostat4_thermostat
        - climate.thermostat5_thermostat
        - climate.thermostat6_thermostat
        - climate.thermostat7_thermostat
        - climate.thermostat8_thermostat
        - climate.thermostat9_thermostat
        - climate.thermostat10_thermostat
        - climate.thermostat11_thermostat
      sequence:
        - variables:
            ieee: |
              {{ device_attr(repeat.item, 'identifiers') 
              | selectattr(0, 'eq', 'zha') | map(attribute=1) | first }}
            device: |
              {{ repeat.item | replace('climate.', '', 1) |
              replace('_thermostat', '', 1) }}
            friendlyname: >
              {{ state_attr(repeat.item, 'friendly_name') | replace('
              Thermostat', '', 1) | replace(' ', '_') }}
        - service: zha_toolkit.execute
          data:
            command: attr_read
            ieee: "{{ ieee }}"
            cluster: 65281
            attribute: 263
            state_id: sensor.{{ friendlyname }}_floor_temp
            state_value_template: (value/100) | float(0) | round(1)
            allow_create: true
            tries: 10
        - service: zha_toolkit.ha_set_state
          data:
            fail_exception: true
            state_id: sensor.{{ friendlyname }}_floor_temp
            state_attr: floortemp
            attr_val: (value/100) | float(0) | round(1)
            allow_create: true
mode: single

Also, I’ve tried replacing “zha_toolkit.execute” with “zha_toolkit.attr_read”, but it doesn’t seem to have the state_value_template option?

ha_set_state is only for setting states - it’s not related to zigbee as such. I added this to zha-toolkit as I wanted such a method for some scripts/automations with zha-toolkit.

Given this valid Zigbee sensor:

we need unit_of_measurement, and possibly also the state_class and device_class attributes - with friendly_name being optional.

        - service: zha_toolkit.ha_set_state
          data:
            state_id: sensor.{{ friendlyname }}_floor_temp
            state_attr: unit_of_measurement
            attr_val: °C
            allow_create: true
        - service: zha_toolkit.ha_set_state
          data:
            state_id: sensor.{{ friendlyname }}_floor_temp
            state_attr: state_class
            attr_val: measurement
            allow_create: true
        - service: zha_toolkit.ha_set_state
          data:
            state_id: sensor.{{ friendlyname }}_floor_temp
            state_attr: device_class
            attr_val: temperature
            allow_create: true

It’s available in GUI mode:

and it’s used in a blueprint:

Do note the slight difference in the service call. Here are two partial service call configurations for attr_read:

# Using execute to call attr_read
service: zha_toolkit.execute
data:
  command: attr_read
  ieee: button.tz3000_qd7hej8u_ts0505b_identify
  cluster: 5465

# Using attr_read
service: zha_toolkit.attr_read
data:
  ieee: button.tz3000_qd7hej8u_ts0505b_identify
  cluster: 545

So where would I apply the value change? IE: (value/100)

Or is this simply an additional command to add “unit of measurement” after my initial command:

- service: zha_toolkit.execute
        data:
          command: attr_read
          ieee: '{{ ieee }}'
          cluster: 65281
          attribute: 263
          state_id: sensor.{{ friendlyname }}_floor_temp
          state_value_template: (value/100) | float(0) | round(1)    
          allow_create: true
          tries: 10

Thank you

That is something else that zha-toolkit enables: you can use a template - it’s slightly different from the usual templates: you should not add the curly braces ( “{{ }}” ) as the template needs to be evaluated after fetching the value.
There is an example of “state_value_template” in the readme and also in this blueprint.

But you found that because of the 2nd part of your message.

Yes, setting the unit of measurement is an additionnal command. I do not think that the order is important.
In principle you just need to write this after a ha restart, but you can write it anytime.

The code that sets the state does not require that the “entity” exists already (if allow_create is “thruthy” (true/1)).

1 Like