Using input_boolean helpers in templates (configuration.yaml)

Hi.

I have numerous automation that make announcements over our SONOS system. Before any announcement is made, the automation checks a flag (an input_boolean flag called sonos_announcements that can be toggled on or off). If the flag is set to on, then the announcements are made; if not, then no announcements.

I’d like to change the icon for the helper based on whether it is ‘on’ or ‘off’. I’ve done this successfully with various switches in my configuration.yaml, but for some bizarre reason, can’t get it to work with a boolean ‘helper’

Can somebody point me in the right direction?

switch:
    - platform: template
      switches:

         [...other switches here that seem to work OK...]

          sonos_announce:
            friendly_name: "SONOS Announcements"
            value_template: "{{ states('input_boolean.sonos_announcments')}}"
            turn_on:
              service: input_boolean.turn_on
              target:
                  entity_id: input_boolean.sonos_annoucements
            turn_off:
              service: input_boolean.turn_off
              target:
                entity_id: input_boolean.sonos_annoucements
            icon_template: >-
              {% if states('input_boolean.sonos_annoucements') == 'on' %}
                mdi:speaker
              {% else %}
                mdi:speaker-off
              {% endif %}
            unique_id: "c7310e44-451b-11ee-be56-0242ca120199"

I have searched the forum, but can’t find a suitable answer.

You have a typo either in input_boolean.sonos_announcments or in input_boolean.sonos_annoucements. :slight_smile: You’re missing an e or have it to much, depending on the real sensor name. :wink:

1 Like

Also instead of using:

{% if states('input_boolean.sonos_annoucements') == 'on' %}

You can use this:

{% if is_state('input_boolean.sonos_annoucements','on') %}

Or this depending on which spelling you actually used:

{% if is_state('input_boolean.sonos_annoucments','on') %}

Patrick/Tom.

Thanks for the suggestions. (Actually, I’d completely misspelt announcements anyway, not only dropping an ‘e’ but also an ‘n’. What a doofas!)

Anyway, I made the corrections, including Tom’s suggestion about using the different form of ‘if’ statement, but the results are still the same.

The modified code is now:

          sonos_announce:
            friendly_name: "SONOS Announcements"
            value_template: "{{ states('input_boolean.sonos_announcements') }}"
            turn_on:
              service: input_boolean.turn_on
              target:
                  entity_id: input_boolean.sonos_announcements
            turn_off:
              service: input_boolean.turn_off
              target:
                entity_id: input_boolean.sonos_announcements
            icon_template: >-
              {% if is_state('input_boolean.sonos_announcements', 'on') %}
                mdi:speaker
              {% else %}
                mdi:speaker-off
              {% endif %}
            unique_id: "c7310e44-451b-11ee-be56-0242ca120199"

And where I reference it in an Entities card is:

      - entity: input_boolean.sonos_announcements
        type: simple-entity
        name: Enable Sonos Announcements
        tap_action:
          action: toggle
        card_mod:
            :host {
              --card-mod-icon-color:
              {% if states('input_boolean.sonos_announcements') == 'on' %}
              green
              {% else %}
              red
              {% endif %}
              }
        state_color: false

Turning the helper off and on does change the color, but not the icon :frowning:

I wonder if the issue isn’t with that code but with when the icon template is interpreted. If u add a trigger on the input_boolean, does that help?

Thanks.

I’m assuming you’re suggesting that this code goes in the entities card (where it changes icon color based on the state) and not in the configuration.yaml? If so, I just tried that, and the symptoms were the same. Switching off the helper changes the color of the icon, but not the actual icon itself.

Now, this works. But, it is a switch (a Philips smart plug, which is treated as a light switch), not a helper. I wonder if that is some kind of restriction?

switch:
    - platform: template
      switches:
          water_pump:
            friendly_name: "Water Pump"
            value_template: "{{ states('light.water_pump')}}"
            turn_on:
              service: light.turn_on
              target:
                  entity_id: light.water_pump
            turn_off:
              service: light.turn_off
              target:
                entity_id: light.water_pump
            icon_template: >-
              {% if states('light.water_pump') == 'on' %}
                mdi:pump
              {% else %}
                mdi:pump-off
              {% endif %}
            unique_id: "c7310e44-451b-11ee-be56-0242ca120100"

Apologies, mis-read the problem — Friday afternoon…

Your entity card is referencing the input boolean helper rather than the template switch that has the variable icon, which is why the displayed icon is not changing.

- entity: input_boolean.sonos_announcements
  type: simple-entity

should be

- entity: switch.sonos_announce
  type: simple-entity
1 Like

Duh! You’re absolutely right. I made the change and the problem is now solved.

Many thanks.

1 Like

So easy to do that sort of thing, and then you get convinced the problem is elsewhere.

This does show the value of including full code in posts — without your card YAML we could have been chasing that one for a while!