Toggleable card with helper input_boolean

Hey!

I’ve spent the last 24h getting acquainted with HASS and it’s amazing. I’m still doing my baby steps but I love it already - just wanted to start off with a huge thank you :slight_smile:

I have a Xiaomi Standing Fan 3, and I use HACS with GitHub - syssi/xiaomi_fan: Xiaomi Mi Smart Fan integration for Home Assistant to control it. Unfortunately, it does not seem to have any toggleable presets, so I have to create these myself. Luckily it does poll the various attributes so I don’t have to resort to input booleans at this stage.

In the process of automating this as a card, I’ve run into several issues:

  1. The WebUI complains about a plethora of missing keys in the YAML configuration:
Configuration errors detected:
Required key "entities.0.tap_action.url_path" is missing.
Required key "entities.0.tap_action.navigation_path" is missing.
Required key "entities.0.type" is missing.
Required key "entities.0.name" is missing.
Required key "entities.0.tap_action.url_path" is missing.
Required key "entities.0.tap_action.navigation_path" is missing.
Required key "entities.0.type" is missing.
Required key "entities.0.view" is missing.
Required key "entities.0.view" is missing.
Required key "entities.0.view" is missing.
Required key "entities.0.type" is missing.
Required key "entities.0.conditions" is missing.
Required key "entities.0.type" is missing.
Required key "entities.0.type" is missing.
Required key "entities.0.type" is missing.
Required key "entities.0.url" is missing.
Required key "entities.0.type" is missing.
Required key "entities.0.entities" is missing.
Required key "entities.0.type" is missing.
Required key "entities.0.attribute" is missing.
Required key "entities.0.type" is missing.
Required key "entities.0.name" is missing.
Required key "entities.0.service" is missing.
Required key "entities.0.type" is missing.
Required key "entities.0.name" is missing.
Required key "entities.0.text" is missing.
Required key "entities.0.type" is missing.

which means I cannot save the card unless I input e.g. type: null in these.

  1. With the following configuration, I get an error about required key not provided:
type: entities
entities:
  - entity: fan.xiaomi_standing_fan_3
    name: Oscillation
    view: null
    type: null
    url: null
    conditions: null
    entities: null
    attribute: null
    service: null
    text: null
    tap_action:
      url_path: null
      navigation_path: null
      action: call-service
      service: fan.oscillate
      service_template:
        entity_id: fan.xiaomi_standing_fan_3
        oscillating: "{{ not state_attr('fan.xiaomi_standing_fan_3', 'oscillating') }}"

I’ve found this thread, suggesting one cannot use template in Lovelace. I’ve then managed to create a script to do this toggling, but that leads me to the next point -

  1. With the script in place, I can create a button card that executes the script. I’d like for it to be a toggleable card (like the default entity card), so that I can see the state + execute the script as needed. I’m stuck here :confused:

EDIT: I’ve also tried defining a template switch such as this, and I get an unknown error whenever I try to toggle it. Looking at the log file, I see e.g. the following:

  File "/usr/src/homeassistant/homeassistant/components/fan/__init__.py", line 607, in state_attributes
    data[ATTR_SPEED] = self.speed
  File "/usr/src/homeassistant/homeassistant/components/fan/__init__.py", line 457, in speed
    percentage = self.percentage
  File "/config/custom_components/xiaomi_miio_fan/fan.py", line 1157, in percentage
    return ordered_list_item_to_percentage(FAN_SPEEDS_1C, self._preset_mode)
  File "/usr/src/homeassistant/homeassistant/util/percentage.py", line 21, in ordered_list_item_to_percentage
    raise ValueError(f'The item "{item}"" is not in "{ordered_list}"')
ValueError: The item "None"" is not in "['Level 1', 'Level 2', 'Level 3']"

Switch template:

switch:
  - platform: template
    switches:
      fan_osc:
        value_template: "{{ not state_attr('fan.xiaomi_standing_fan_3', 'oscillating') }}"
        turn_on:
          service: fan.oscillate
          target:
            entity_id: fan.xiaomi_standing_fan_3
          data:
            entity_id: fan.xiaomi_standing_fan_3
            oscillating: true
        turn_off:
          service: fan.oscillate
          target:
            entity_id: fan.xiaomi_standing_fan_3
          data:
            entity_id: fan.xiaomi_standing_fan_3
            oscillating: false

Any help will be appreciated!

your switch template should be:

switch:
  - platform: template
    switches:
      fan_osc:
        value_template: "{{ not state_attr('fan.xiaomi_standing_fan_3', 'oscillating') }}"
        turn_on:
          service: fan.oscillate
          target:
            entity_id: fan.xiaomi_standing_fan_3
          data:
            oscillating: true
        turn_off:
          service: fan.oscillate
          target:
            entity_id: fan.xiaomi_standing_fan_3
          data:
            oscillating: false

however, the custom integration that you’re using

Has a bug in it. This service should not error out when calling the service. Otherwise this would work.

EDIT: My guess is that this would work if the fan is already on. If your fan is on, the creator never even tested this functionality. Either way, you need to notify the creator of the custom integration.

1 Like

Thanks petro! I was fearing that might be the case. I’m fine also with tweaking the Python code myself to adjust for these errors. It seems to me that the error runs a bit deeper though, stemming from homeassistant.util.percentage's method ordered_list_item_to_percentage. That is, None is a bit of an edge case when comparing to list of items, perhaps it should be treated as such?

Any thoughts about the UI issues? Seems a bit silly having to add all the optional keys with null values just to get past the schema validation.

The normal GUI does not complain about that. Are you using something custom?

Nope, a normal docker-based setup. I enter Dashboard edit mode, add an Entities card, click on “Show code editor”, and add e.g. a type: button to any entity:

EDIT: Additional info:

System Health

version core-2021.7.4
installation_type Home Assistant Container
dev false
hassio false
docker true
virtualenv false
python_version 3.9.5
os_name Linux
os_version 5.13.4-arch2-1
arch x86_64
timezone Europe/Helsinki
Home Assistant Community Store
GitHub API ok
Github API Calls Remaining 4993
Installed Version 1.13.2
Stage running
Available Repositories 839
Installed Repositories 14
Home Assistant Cloud
logged_in false
can_reach_cert_server ok
can_reach_cloud_auth ok
can_reach_cloud ok
Lovelace
dashboards 1
resources 8
views 2
mode storage

well your configuration is wrong. type: button is not valid.

Oh? I thought a button type is provided by default – Entities Card - Home Assistant? (Goes without saying that adding the mandatory tap_action does not resolve the rest of the errors)

type only accepts custom cards for entity types:

I’m confused; The example at Entities Card - Home Assistant suggests otherwise. Is the documentation simply misleading then…? I did manage to use custom cards without a problem, but I figured that’s due to the custom cards internal schema verification.

image

Then it’s a bug in the UI editor, but the docs say otherwise in the configuration section. So either the example is wrong or the docs are wrong and there’s a bug.

1 Like