Hi, can you please explain to me the syntax here?
What is ‘not state_attr’?
How do I know when to use it?
For example, if I want to toggle power, is it with the same string?
state_attr gets an attribute from an entity. The attribute mentioned in the other posts is_volume_muted is a boolean. This means the attribute is either true or false. When using the method state_attr to get is_volume_muted, it will return the value of the attribute; i.e. true or false. When adding the not in front of it, it inverts the true/false value. So true becomes false and false becomes true.
This depends on a lot. What are entity are you working with and what are you trying to create?
Regarding the power.
I have a denon receiver.
I want to create a script that toggle the power the same it toggles mute function.
Any idea what should the template compare?
But the error i get is the following when i push the button:
“Failed to call service media_player/volume_mute. invalid boolean value {{ not state_attr(‘media_player.living_room_speaker’, ‘is_volume_muted’) }} for dictionary value @ data[‘is_volume_muted’]”
So this to me suggests lovelace is accepting templates but the output of the template is not in the right format. Now when i go to the developer tools → templates and put this in, it is giving me the proper ‘True’ and ‘False’ but it also says that the result is a string as opposed to boolean. It almost suggests that the function “state_attr” is pumping out a string result and the button card is not accepting that string result. I also cannot see any way to convert it to a true boolean. I may try to convert it to 1 and 0 and see what happens there. Anyone run into this?
Hello @petro i have read some of your other posts in similar topics… Super helpful.
Ya i had may have tried what you provided there because i think i saw something you posted in another thread. I will try it again tho.
Its very strange. I have this working now but im using a script to do it (again this may have come from you):
mute_toggle:
fields:
media_player:
description: Media Player that will be mute or unmuted
sequence:
- service: media_player.volume_mute
target:
entity_id: "{{ media_player }}"
data:
is_volume_muted: "{{ not state_attr(media_player , 'is_volume_muted') }}"
So then i tried to do the same thing with a media_player toggle which should turn the media player (this case a google home) on and off. So i used a modification of the script like this (almost identical):
power_toggle:
fields:
media_player:
description: Media Player that will be toggled
sequence:
- service: media_player.toggle
target:
entity_id: "{{ media_player }}"
The weird thing is that it will only turn on the media_player but not turn it off. However, if i use the services developer tools and call the same media_player.toggle call with this same entity, it too will only turn it on but not off… HOWEVER if i create a basic entities card with teh media player (google home) and hit the power button on that (default media player i suppose), it will turn on and off all day long. There is something weird going on. I was thinking maybe it was a button card issue now but with the developer tools call not working now im not sure.
By they way, the code you provided does work! So it saves me from having to use a script… very nice! I even was able to template it in side a declutter card like this:
Just to add to the story. I was trying to figure out why the button will only turn on the media player but not turn it off. I did use your suggested ‘toggle’ service and that works, but again, still only turns the media player on but not off. I ended up taking out the button-card template called ‘icon_only_tom’ and now the button does both. But the weird thing is that this is that button-card template: