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