Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HomeAssistant template warning and error for Hampton Bay/Wink/King of Fans 99432 #8617

Closed
mwav3 opened this issue Sep 2, 2021 · 13 comments
Closed
Labels
problem Something isn't working

Comments

@mwav3
Copy link

mwav3 commented Sep 2, 2021

What happened

The MQTT template for the Hampton Bay/Wink/King of Fans 99432 was recently updated. Thanks to the great work of @cmccambridge with this PR - #7359 , all fan controls and speeds are now able to be controlled in the Home Assistant template. Home Assistant has also recently updated their templating requirements to be more rigid, and now expects a default and defined value for all template items.

However, two errors still persist in the logs as a result of the template loaded for this fan.

The light template creates the following error:

Exception in state_received when handling msg on 'zigbee2mqtt/Bedroom Fan': '{"fan_mode":"low"}' Traceback (most recent call last): File "/usr/src/homeassistant/homeassistant/components/mqtt/debug_info.py", line 34, in wrapper msg_callback(msg) File "/usr/src/homeassistant/homeassistant/components/mqtt/light/schema_json.py", line 299, in state_received if values["state"] == "ON": KeyError: 'state'

The fan template creates the following warning:

2021-09-01 21:44:02 WARNING (MainThread) [homeassistant.helpers.template] Template variable warning: 'dict object' has no attribute 'fan_state' when rendering '{{ value_json.fan_state }}'

What did you expect to happen

No errors in the logs and the template follows Home Assistant's requirements. I was able to modify templates to control the fan and light and when I set them in my config file for Home Assistant the error and warning are cleared. I would submit a PR to fix this but unfortunately I do not understand the zigbee2mqtt program enough to find the code and propose changes. Hopefully someone can take these templates and put them in the files so they can be auto-discovered for other users.

Right now, everything works fine and these warnings do not impact performance, but the concern is that starting with the next release for 10/2021 for Home Assistant, any template warning or error will cause the template to fail, and the device will not work and render the device uncontrollable on the next update.

The light template that fixes the error is here:

light:
  - platform: mqtt 
    name: "Bedroom Fan"
    unique_id: "bedroom_fan"
    state_topic: "zigbee2mqtt/Bedroom Fan"
    command_topic: "zigbee2mqtt/Bedroom Fan/set"
    json_attributes_topic: zigbee2mqtt/Bedroom Fan
    brightness_state_topic: "zigbee2mqtt/Bedroom Fan"
    brightness_value_template: "{{ value_json.brightness |float | default('')}}"
    brightness_command_topic: "zigbee2mqtt/Bedroom Fan/set/brightness"
    brightness_scale: 254
    state_value_template: "{{ value_json.state | default('')}}"

The fan template that fixes the error is here:

fan:
  - platform: mqtt
    name: "Bedroom Fan"
    command_topic: "zigbee2mqtt/Bedroom Fan/set/fan_state"
    availability:
      - topic: "zigbee2mqtt/bridge/state"
    json_attributes_topic: "zigbee2mqtt/Bedroom Fan"
    state_topic: "zigbee2mqtt/Bedroom Fan"
    state_value_template: "{{ value_json.fan_state | default('')}}"
    unique_id: "bedroom_fan"
    speed_range_min: 1
    speed_range_max: 4
    percentage_state_topic: "zigbee2mqtt/Bedroom Fan"
    percentage_value_template: "{{ {'off': 0, 'low': 1, 'medium': 2, 'high': 3, 'on': 4, 'smart': 2}[value_json.fan_mode] | default('') }}"
    percentage_command_topic: "zigbee2mqtt/Bedroom Fan/set/fan_mode"
    percentage_command_template: "{{ {0: 'off', 1: 'low', 2: 'medium', 3: 'high', 4: 'on'}[value] | default('')}}"
    preset_mode_state_topic: "zigbee2mqtt/Bedroom Fan"
    preset_mode_value_template: "{{ 'smart' if value_json.fan_mode == 'smart' else 'normal' }}"
    preset_mode_command_template: "{{ value if value == 'smart' else 'medium' }}"
    preset_mode_command_topic: "zigbee2mqtt/Bedroom Fan/set/fan_mode"
    preset_modes:
      - "normal"
      - "smart"

How to reproduce it (minimal and precise)

Error is displayed as soon as the fan is discovered in Home Assistant

Please let me know if you need any other info.

Debug info

Zigbee2MQTT version: 1.21.1
Adapter hardware: CC2652R1, - zzh stick
Adapter firmware version: CC2652R_router_20210128

@mwav3 mwav3 added the problem Something isn't working label Sep 2, 2021
@github-actions
Copy link
Contributor

github-actions bot commented Oct 3, 2021

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 7 days

@github-actions github-actions bot added the stale Stale issues label Oct 3, 2021
@mwav3
Copy link
Author

mwav3 commented Oct 3, 2021

Not stale, still an issue

@OzGav
Copy link
Contributor

OzGav commented Oct 3, 2021

So of all that is in your templates is it only the | default('') that is the change from what zigbee2mqtt creates normally? If so I believe I have found the lines in the code that would need this added...

This is the file: https://github.com/Koenkk/zigbee2mqtt/blob/07a8a78a551f95ef345d97b4f5db79cb85318189/lib/extension/homeassistant.ts

@Koenkk
Copy link
Owner

Koenkk commented Oct 9, 2021

Can you check if this has been fixed in the latest-dev?

Changes will be available in the dev branch in a few hours from now. (https://www.zigbee2mqtt.io/how_tos/how-to-switch-to-dev-branch.html)

@mwav3
Copy link
Author

mwav3 commented Oct 20, 2021

Sorry for the delay. I switched to the dev branch and still get the log errors for some reason. I'm not sure why the code didn't fix it - tried restarting home assistant and zigbee2mqtt several times, and reinstalled the mqtt integration.

This is the discovery payload I get:

availability:
  - topic: zigbee2mqtt/bridge/state
command_topic: zigbee2mqtt/Bedroom Fan/set/fan_state
device:
  identifiers:
    - zigbee2mqtt_0x0022a3000029410e
  manufacturer: Hampton Bay
  model: Universal wink enabled white ceiling fan premier remote control (99432)
  name: Bedroom Fan
  sw_version: Zigbee2MQTT 1.21.2-dev
json_attributes_topic: zigbee2mqtt/Bedroom Fan
name: Bedroom Fan
percentage_command_template: '{{ {0:''off'', 1:''low'', 2:''medium'', 3:''high'', 4:''on''}[value] | default('''') }}'
percentage_command_topic: zigbee2mqtt/Bedroom Fan/set/fan_mode
percentage_state_topic: zigbee2mqtt/Bedroom Fan
percentage_value_template: >-
  {{ {'off':0, 'low':1, 'medium':2, 'high':3, 'on':4}[value_json.fan_mode] |
  default('None') }}
preset_mode_command_topic: zigbee2mqtt/Bedroom Fan/set/fan_mode
preset_mode_state_topic: zigbee2mqtt/Bedroom Fan
preset_mode_value_template: >-
  {{ value_json.fan_mode if value_json.fan_mode in ['smart'] else 'None' |
  default('None') }}
preset_modes:
  - smart
speed_range_max: 4
speed_range_min: 1
state_topic: zigbee2mqtt/Bedroom Fan
state_value_template: '{{ value_json.fan_state }}'
unique_id: 0x0022a3000029410e_fan_zigbee2mqtt
platform: mqtt

The new default option in the code change doesn't seem to feed into the state_value_template for some reason.

@Koenkk
Copy link
Owner

Koenkk commented Oct 20, 2021

@mwav3 could you post the Home Assistant warning? I expect that fan_state should be set to null now

@mwav3
Copy link
Author

mwav3 commented Oct 22, 2021

I'm still getting the same ones initially, its like the code change to add the default isn't having any impact on the template for some reason

Logger: homeassistant.helpers.template
Source: helpers/template.py:1612
First occurred: 10:17:06 AM (4 occurrences)
Last logged: 10:17:06 AM

Template variable warning: 'dict object' has no attribute 'fan_state' when rendering '{{ value_json.fan_state }}'
Logger: homeassistant.util.logging
Source: util/logging.py:105
First occurred: 10:17:06 AM (5 occurrences)
Last logged: 10:17:06 AM

Exception in state_received when handling msg on 'zigbee2mqtt/Bedroom Fan': '{"fan_mode":"low"}' Traceback (most recent call last): File "/usr/src/homeassistant/homeassistant/components/mqtt/debug_info.py", line 37, in wrapper msg_callback(msg) File "/usr/src/homeassistant/homeassistant/components/mqtt/light/schema_json.py", line 299, in state_received if values["state"] == "ON": KeyError: 'state'

I'm thinking the wrong file may have been updated? I see updates to lib/extension/homeassistant.ts in this PR, but the old PR that made changes to this template by @cmccambridge #7359 updated two other files called lib/extension/homeassistant.js and test/homeassistant.test.js

Update:
It looks like the previous homeassistant.js file was refactored to typescript, so I'm not sure what's going wrong. Is updating the file in the "test" directory necessary here as well?

@Koenkk
Copy link
Owner

Koenkk commented Oct 22, 2021

It looks like the previous homeassistant.js file was refactored to typescript, so I'm not sure what's going wrong. Is updating the file in the "test" directory necessary here as well?

After checking out the dev branch make sure you do a npm ci && npm run build, this will compile the typescript files to javascript files in the dist directory.

@mwav3
Copy link
Author

mwav3 commented Oct 22, 2021

When I run npm ci && npm run build I'm getting

cipm can only install packages with an existing package-lock.json or npm-shrinkwrap.json with lockfileVersion >= 1. Run an install with npm@5 or later to generate it, then try again.

I tried npm install but then get errors about python not being installed

This was on docker with latest-dev tag

@Koenkk
Copy link
Owner

Koenkk commented Oct 23, 2021

with docker you don't have to worry about building/installing dependencies, everything is already in the image.

Could you provide the debug log when z2m posts something to zigbee2mqtt/Bedroom Fan? (which causes these HA errors)

@mwav3
Copy link
Author

mwav3 commented Oct 25, 2021

Debug logs show this for light config

info  2021-10-24 21:08:30: MQTT publish: topic 'homeassistant/light/0x0022a3000029410e/light/config', payload '{"availability":[{"topic":"zigbee2mqtt/bridge/state"}],"brightness":true,"brightness_scale":254,"command_topic":"zigbee2mqtt/Bedroom Fan/set","device":{"identifiers":["zigbee2mqtt_0x0022a3000029410e"],"manufacturer":"Hampton Bay","model":"Universal wink enabled white ceiling fan premier remote control (99432)","name":"Bedroom Fan","sw_version":"Zigbee2MQTT 1.21.2-dev"},"json_attributes_topic":"zigbee2mqtt/Bedroom Fan","name":"Bedroom Fan","schema":"json","state_topic":"zigbee2mqtt/Bedroom Fan","unique_id":"0x0022a3000029410e_light_zigbee2mqtt"}'

And for fan config

info  2021-10-24 21:08:30: MQTT publish: topic 'homeassistant/fan/0x0022a3000029410e/fan/config', payload '{"availability":[{"topic":"zigbee2mqtt/bridge/state"}],"command_topic":"zigbee2mqtt/Bedroom Fan/set/fan_state","device":{"identifiers":["zigbee2mqtt_0x0022a3000029410e"],"manufacturer":"Hampton Bay","model":"Universal wink enabled white ceiling fan premier remote control (99432)","name":"Bedroom Fan","sw_version":"Zigbee2MQTT 1.21.2-dev"},"json_attributes_topic":"zigbee2mqtt/Bedroom Fan","name":"Bedroom Fan","percentage_command_template":"{{ {0:'off', 1:'low', 2:'medium', 3:'high', 4:'on'}[value] | default('') }}","percentage_command_topic":"zigbee2mqtt/Bedroom Fan/set/fan_mode","percentage_state_topic":"zigbee2mqtt/Bedroom Fan","percentage_value_template":"{{ {'off':0, 'low':1, 'medium':2, 'high':3, 'on':4}[value_json.fan_mode] | default('None') }}","preset_mode_command_topic":"zigbee2mqtt/Bedroom Fan/set/fan_mode","preset_mode_state_topic":"zigbee2mqtt/Bedroom Fan","preset_mode_value_template":"{{ value_json.fan_mode if value_json.fan_mode in ['smart'] else 'None' | default('None') }}","preset_modes":["smart"],"speed_range_max":4,"speed_range_min":1,"state_topic":"zigbee2mqtt/Bedroom Fan","state_value_template":"{{ value_json.fan_state }}","unique_id":"0x0022a3000029410e_fan_zigbee2mqtt"}'

Here's one showing current state of the device

info  2021-10-24 21:08:30: MQTT publish: topic 'zigbee2mqtt/Bedroom Fan', payload '{"brightness":254,"fan_mode":"off","fan_state":"OFF","linkquality":null,"state":"ON"}

Update:
Could the state of the fan and light being all uppercase cause an issue with the template? Is it case sensitive?

@Koenkk
Copy link
Owner

Koenkk commented Oct 25, 2021

Do you still get the: Template variable warning: 'dict object' has no attribute 'fan_state' when rendering '{{ value_json.fan_state }}' error in HA now? I can see that fan_state has a value in the published state from z2m.

@mwav3
Copy link
Author

mwav3 commented Oct 26, 2021

It didn't make sense I was getting the error because the entire state of the device was being published, including the fan_state. After further testing it only happened when Home Assistant restarted. When it restarted, there was a "retained" message in the MQTT broker on zigbee2mqtt/Bedroom Fan that was only reporting the fan speed an no other values, which created the template error. Shortly after Home Assistant restarted, the MQTT message was updated with all fresh values and the error didn't happen anymore.

I deleted the retained message (which I can only guess must have been stuck there from an older version of zigbee2mqtt), and the template error cleared and has not returned.

At some point, the device was retaining messages, and then the setting changed and it no longer was. I'm not sure if I did that or it changed when between versions, but I'm closing as this issue was on my end. Thanks for your time and patience.

@mwav3 mwav3 closed this as completed Oct 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
problem Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants