Invalid boolean prevents action

Its frustrating, and Google (AI) gives conflicting solutions.

I want to add a button on a dashboard, that toggels the oscillation function of a fan.

- type: custom:button-card
    template: icon_button
    icon: mdi:closed-caption-outline
    color: red
    tap_action:
      action: call-service
      service: fan.oscillate
      target:
        entity_id: fan.xiaomi_p85_f71e_fan
      data:
        oscillating: true

This button effectively swithes the oscillation ON. (and if i change ‘true’ to ‘false’ .. the button swithches the oscillation OFF

So now I want to replace the hard coded true or false with a template that returns true if the oscillation is OFF , so that it will switch it on.

tap_action:
      action: call-service
      service: fan.oscillate
      target:
        entity_id: fan.xiaomi_p85_f71e_fan
      data:
        oscillating: '{{ not state_attr("fan.xiaomi_p85_f71e_fan", "oscillating")}}'

Failed to perform the action fan/oscillate. invalid boolean value {{ not state_attr(“fan.xiaomi_p85_f71e_fan”, “oscillating”)}} at ‘oscillating’

Google suggests:

- type: custom:button-card
    template: icon_button
    icon: mdi:closed-caption-outline
    color: red
    tap_action:
      action: call-service
      service: fan.oscillate
      target:
        entity_id: fan.xiaomi_p85_f71e_fan
      data:
        oscillating: >-
          {{ not state_attr('fan.xiaomi_p85_f71e_fan', 'oscillating') |
          bool(false) }}

Failed to perform the action fan/oscillate. invalid boolean value {{ not state_attr(‘fan.xiaomi_p85_f71e_fan’, ‘oscillating’) | bool(false) }} at ‘oscillating’
Google says action expects a raw boolean, and the template returns text.

After many itterations (single quotes, double quotes…)
I created a binary sensor template sensor (a helper)
binary_sensor.xiaomi_2_swing

{{state_attr("fan.xiaomi_p85_f71e_fan", "oscillating") |bool}}

This returns True, but with a cappital T, which is an invalid boolean.

- type: custom:button-card
    template: icon_button
    icon: mdi:closed-caption-outline
    color: red
    tap_action:
      action: call-service
      service: fan.oscillate
      target:
        entity_id: fan.xiaomi_p85_f71e_fan
      data:
        oscillating: >-
          {{ not is_state('sensor.xiaomi_2_swing') }}

Gives the error :
Failed to perform the action fan/oscillate. invalid boolean value {{ not is_state(‘sensor.xiaomi_2_swing’) }} at ‘oscillating’
Probably because True is spelled with a capital T (or that it is text and not a bool, even though i have supposedly cast the template sensor to a bool)

Does anyone know how to create a proper bool so that my button can toggel the fan oscillation ?

Edit:
My binary sensor :

{{ state_attr('fan.xiaomi_p85_f71e_fan', 'oscillating') == 'off' |bool(false) }}

STILL reports back as on or off , not as true or false

custom:button-card template language is javascript enclosed in 3 brackets: [[[ code ]]]. Templates have access to the states variable. So it should look something like this:

:grinning_face:

That works. Thank you so very much.
You are more cleverer than artificial intelligence.
At some point I found another topic with a similar question, but every option it and google came up with was the bool( ) function.
There are several answers that assume “oscillating” is a state, rather than an attribute. (Thats why I tried to create a template sensor and a template binary_sensor.)

Again, thank you very much.

So, are you saying there is a difference because I use a custom:button-card, rather than a simple button?

Yes, but a standard button probably doesn’t support templates at all when defining actions. Standard cards typically require the use of a script. custom:button-card is quite feature-rich, you may want to see its documentation page.

“You may want to see its documentation page”
Fair point. :slight_smile:
(RTFM)
I have a difficult time with the syntax of yaml, the concept of templates , and the documentation seems (to me) written for people who already understand it.

Have a good weekend.

I wasn’t snide. I completely understand you. I remember my utter bewilderment: “why does a completely different syntax suddenly apply here?”. ESPhome is C/C++, integrations are Python :sweat_smile: It’s just hard until you gain some experience.

That’s ok.. I was not taking it as a snide. :wink:

Absolutely. The mixing of systems and syntaxes is confusing.
A lot of tutorials give you a step by step without telling you were it gets the data / variable from and why it needs to be accessed in that specific way in that specific situation.
So, the only way to learn IS reading the documentation, fully.
But at the same time, Home Assistant is claiming to make the platform accessible for people like me. (I am not an idiot, I am also not a software engineer or developer. I have a fair bit of experience in C++, none in Java, none in Yaml.)
I have tried to get into yaml by creating / editing some ESPHome devices.
But I am far more comfortable creating devices 100% in C++ , or sometimes throuhg MQTT auto discovery.
ESPHome, to me, is just an additional layer on top of C++ . And every time the ESPHome platform updates, my devices are out of date.

I really enjoy HA when it works. But I don’t do enought to keep up with all the updates.
If I don’t touch the code for a month, I basically have to start reading the documents again,

That’s why it’s called ‘Artificial …’ and therefor the remark ‘AI can mistakes’.
The use of it for HA (or anything else) often turns out to be wrong and asking for help here might provide solutions for others.

Anyways, good that it got solved!

Please take the time to mark the post that holds the solution.
You do that by selecting the three dots under the post:

image

Then select the check box:

image
By doing so:

  • this thread can be useful to other users as well (this thread comes up as solution when starting a thread which is similar to yours).
  • this prevents that someone else steps in trying to help

Thanks for giving back to the community! :+1:

Uhhmm.. Thats why it should remain to be called “artificial intelligence” and not “super intelligence”
:rofl:

You are right. I acknowledged the solution. Should have done that already.
For the benefit people who have been given a Xiaomi fan, and are looking to control it through Home Assistant.
(I have it running through the integration: Xiaomi Miot , which i believe is adding the necessary entities)

I have since the oscillation function works, also added a button to switch on/off the “natural wind” mode. (something that does not work for my fan through the custom: fan-xiaomi card)

The code I added to a custom:button-card

- type: custom:button-card
    template: icon_button
    entity: fan.xiaomi_p85_f71e_fan
    icon: mdi:leaf
    color: green
    name: natural
    tap_action:
      action: perform-action
      perform_action: button.press
      target:
        entity_id: button.xiaomi_p85_f71e_loop_mode

How completely counter intuitive it is that to press a button entity, you need to
tap_action:
action: perform-action
perform_action: button.press

seems a few abstractions too many for me. But at least and at last, it works

:slight_smile:

Hi, thanks for the solution tag!
It’s not possible to do this if you don’t attribute a category to a thread, that’s why I changed it.