How to have input boolean icon change color when switched?

Hey all, I have a hopefully simple question. When I toggle a switch in the dashboard, the icon changes color to show that it’s in the “on” state. However when I toggle an input boolean the icon doesn’t change:

There isn’t really anything to the config, just:

input_boolean:
  holiday_mode:
    name: Toggle for Holiday Mode
    icon: mdi:pine-tree

So is it possible to have the icon change color when the toggle is on?

4 Likes

Did you ever find a solution for this?

I have a Meross wifi plug that isn’t natively supported, so I’m using IFTTT and an input_boolean to toggle it on and off, but I’d like to be able to have the icon associated with it in the UI to change like the other light bulbs.

Nope. :frowning:

You would think there was an easy way to do that. I started looking into light templates and it all got too complicated for me. I’ll just leave it as is for now.

I would have thought so too. But I never came up with anything useful and it wasn’t that big of a deal to pursue any further. Seems like it should be something stupid simple, but I’ve learned to pick my battles. :stuck_out_tongue:

1 Like

I got it to work!

image

I’m using a Template Switch and scripts to turn the plug on and off.

platform: template
switches:
  meross_plug:
    value_template: '{{ states.input_boolean.meross_plug.state }}'
    friendly_name: 'Meross Plug'
    turn_on:
      service: script.ifttt_meross_plug_on
    turn_off:
      service: script.ifttt_meross_plug_off

<< scripts.yaml >>

  ifttt_meross_plug_on:
    sequence:
      - service: ifttt.trigger
        data_template: {"event":"Plug1_On"}
      - service: homeassistant.turn_on
        entity_id: input_boolean.meross_plug
  ifttt_meross_plug_off:
    sequence:
      - service: ifttt.trigger
        data_template: {"event":"Plug1_Off"}
      - service: homeassistant.turn_off
        entity_id: input_boolean.meross_plug
1 Like

Huh, that does in-fact work. If I could like it more than once I would. Thanks for figuring it out, cheers!

1 Like

Hello, I also have input_boolean

But I did not understand in the script how it changes the color of the icon …

I wanted to change the color … I tried to set the example, but I think I made a mistake.

I also use ifftt and sonoff

1 Like

In my opinion you don´t need the script.

Try this modified for your needs:

- platform: template
  switches:
    whatever:
      value_template: '{{ states.input_boolean.whatever.state }}'
      friendly_name: 'Whatever'
      turn_on:
        - service: ifttt.trigger
          data_template: {"event":"Whatever_On"}
        - service: homeassistant.turn_on
          entity_id: input_boolean.whatever
       turn_off:
        - service: ifttt.trigger
          data_template: {"event":"Whatever_Off"}
        - service: homeassistant.turn_off
          entity_id: input_boolean.whatever

The homeassistant.turn_on and turn_off also changes the color of the icon.

1 Like

I tried to make this example. to understand switches, but I have an error. see the log

starting version 3.2.4
Testing configuration at /config
Failed config
  switch.template: 
    - Invalid config for [switch.template]: expected a dictionary for dictionary value @ data['switches']. Got None. (See ?, line ?). Please check the docs at https://home-assistant.io/components/switch.template/
    - platform: template
      switches: None
      whatever: [source /config/configuration.yaml:96]
        friendly_name: Whatever
        turn_off: [source /config/configuration.yaml:104]
          - data_template: [source /config/configuration.yaml:105]
              event: Whatever_Off
            service: ifttt.trigger
          - entity_id: input_boolean.whatever
            service: homeassistant.turn_off
        turn_on: [source /config/configuration.yaml:99]
          - data_template: [source /config/configuration.yaml:100]
              event: Whatever_On
            service: ifttt.trigger
          - entity_id: input_boolean.whatever
            service: homeassistant.turn_on
        value_template: {{ states.input_boolean.whatever.state }}
Successful config (partial)
  switch.template:

What’s not being explained to you is that this is a switch template, not an input boolean. You’ll have to swap your input boolean control for a switch template:

yes, and those yellow switches are the default color switches , like using the device_class on (binary) sensors.

you can however change to any color you like using customization:

homeassistant:
  customize:
    input_boolean.notify_notify:
      templates:
        icon: >
          if (state === 'on') return 'mdi:bell';
          return 'mdi:bell-off';
        icon_color: >
          if (state === 'on') return 'rgb(251, 210, 41)';
          return 'rgb(54, 95, 140)';
4 Likes

@Mariusthvdb’s solution seems easier than a template switch if you are using an input_boolean. Just my opinion @iorifly

admitted, the difference between an inout-boolean and a template switch like this isnt always as clear as one would want. When to use which, im not 100% certain yet, and have them mixed here and there.

Customizing these switches btw is just as easy :wink:

switch.mode_developer:
  templates:
    icon: >
      if (state === 'on') return 'mdi:remote-desktop';
      return 'mdi:briefcase';
    icon_color: >
      if (state === 'on') return 'rgb(251, 210, 41)';
      return 'rgb(54, 95, 140)';

switch:
  - platform: template
    switches:
      mode_developer:
        friendly_name: Developer mode switch
        value_template: >
          {{ is_state('input_select.mode', 'Developer') }}
        turn_on:
          - service: script.mode_developer_direct
          - service: homeassistant.turn_on
            entity_id: input_boolean.notify_developing
        turn_off:
          - service: script.mode_normal_direct
          - service: homeassistant.turn_off
            entity_id: input_boolean.notify_developing

I have the switch. But when on, it does not allow. Back to off. And it does not change the color …

- platform: template
    switches:
      skylight:
        value_template: "{{ is_state('sensor.skylight', 'on') }}"
        turn_on:
          service: switch.turn_on
          data:
            entity_id: switch.skylight_open
        turn_off:
          service: switch.turn_on
          data:
            entity_id: switch.skylight_close

switch.turn_off …

1 Like

Sorry. It was not clear to me. Could be more specific. Thanks for your help.

It was this way and this is giving error:

- platform: template
    switches:
      skylight:
        switch.turn_on:
          entity_id: switch.skylight
        switch.turn_off:
          entity_id: switch.skylight

Log:

Invalid config for [switch.template]: [switch.turn_on] is an invalid option for [switch.template]. Check: switch.template->switches->skylight->switch.turn_on. (See ?, line ?). Please check the docs at Template Switch - Home Assistant

The example on the docs page uses another entity for Off than for On. You want the same switch to be either turned On or Off.

It all depends on the entities and states you want them to have.

as an example please see this:

sw_tester_template:
  friendly_name: "Tester"
  value_template: "{{ is_state('binary_sensor.tester_state_bin', 'on') }}"
    #      value_template: "{{ is_state('sensor.tester_state', 'on') }}"
  turn_on:
    service: switch.turn_on
    data:
      entity_id: switch.sw_tester_cl
  turn_off:
    service: switch.turn_off
    data:
      entity_id: switch.sw_tester_cl

switch.turn_on is of course a perfectly valid option, you must use the service: before it though … and you don’t have a value_template set yet? (in the last bit of code you show)

I’m actually testing to see if it changes color when I call. However, when I turn it does not stay on … Then it goes back to off.

If possible, send me a complete example. I’ll put it in my HA to see if I understand what you’re trying to explain … Putting fragments of code, it gets more confusing.