Struggling to get state represented correctly with a template switch combining two switches

Sonos has “Subwoofer Enabled” and “Night Sound” as separate options. I basically want to combine them into one:

Night Sound: on
Subwoofer Enabled: off

I created a Helper and have the Actions working properly. I’m suggling to get the value template working how I want. Basically I’m trying to do this:

{% if is_state('switch.bedroom_night_sound', 'off') == 'on' and is_state('switch.bedroom_subwoofer_enabled', 'on') == 'off' %}on{% else %}off{% endif %}

But when I do, I get the message that HomeAssistant is only listening for switch.bedroom_night_sound changed events and the state does not update.

If I instead use the following, it works, but the switch isn’t quite as accurate as I would like:

{{ is_state('switch.bedroom_night_sound', 'on') }}

Weirdly, if I use or instead of and (not the behavior I want), HA says it’s listening for both events, but it still does not track the state properly:

{% if is_state('switch.bedroom_night_sound', 'off') == 'on' or is_state('switch.bedroom_subwoofer_enabled', 'on') == 'off' %}on{% else %}off{% endif %}

Same story for this:

{{ is_state('switch.bedroom_night_sound', 'on') and is_state('switch.bedroom_subwofer_enabled', 'off') }}

Is there a way to ensure the value is tracking both switches?

true is not “on”

The template editor only says it’s listening to one entity because the first clause of your and always fails… false is also no “on”. It doesn’t need to listen to the other entity until the first clause passes.

There are 4 possible combinations, which ones should return an “on” state vs. an “off” state?

1 Like

It’s not clear to me if I’m supposed to be recording on/off or true/false for the result of value_template. It’s also not clear to me if is_state is returning boolean or “on” and “off”. I believed it to be the latter, with the second parameter being a default when the entity is not initialized.

In any case, this does not work either. I edited my original post with a few other variations I tried:

{{ is_state('switch.bedroom_night_sound', 'on') and is_state('switch.bedroom_subwoofer_enabled', 'off') }}

This syntax still gives the following note:

The goal is that the template switch only reports ON if both conditions are true: night sound is ON, subwoofer is OFF.

If only one condition is true, I want it to report off.

Ok, that’s what you posted just above:

{{ is_state('switch.bedroom_night_sound', 'on') and 
is_state('switch.bedroom_subwoofer_enabled', 'off') }}

edit: I take it back. The simplified version is working. I must’ve had poor testing technique. Thanks again.


Yeah, that doesn’t work. This does though:

{% if is_state('switch.bedroom_night_sound', 'on') and is_state('switch.bedroom_subwoofer_enabled', 'off') %}on{% else %}off{% endif %}

Thanks for the help!