How to have input boolean icon change color when switched?

@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.

Hi, is this working without any additional instalation? Or should I install something else?

what are you referring to?

I’m trying to make this code working with no luck :roll_eyes:

please use the </> code blocks…

did you install custom-ui ? https://github.com/andrey-git/home-assistant-custom-ui/blob/master/docs/installing.md

1 Like

Thank you, this is it! It works now

Hello there! Thanks for this great advice. I have put it into my configuration.yaml but it doesn’t seem to do anything. Does this solution still work? I am running 0.89.2 of Hassio. (No custom UI installed as I am using lovelace)

Otherwise, is there any way to check if this is doing anything, yes or no?

Thanks again!

not sure what you’ve ‘put in’ but if you’re referring to the template, it won’t work unless you install custom-ui. Using Lovelace is by no means any reason not to install custom-ui.

Thanks for the quick response. I was indeed referring to your template. I will have a look again into custom-ui. I was in the opinion I read somewhere on their github that lovelace wasn’t supported yet, so I cancelled the idea of installing it.

Thanks for the confirmation the above script doesn’t work without custom-ui. I was trying to get it working for quite some time already.

we’re all hoping a dedicated custom-ui for Lovelace might (soon) see the light, but for now, many customizations do work under Lovelace. Not all though, and even if HA in its latest version deprecates some of its features, it is imho a valuable extra to the default ui/ux Ha offers without it.

You actually want this option:

          state_color: true

no, this only for a single entity, or per card.
What is want (and have for now…) is this:

homeassistant:

##########################################################################################
# Domain
##########################################################################################
  customize_domain:

    automation:
      templates: &state_color
        icon_color: >
          if (state == 'on') return 'gold';
          return 'steelblue';

    binary_sensor:
      templates:
        <<: *state_color

    input_boolean:
      templates:
        <<: *state_color

    switch:
      templates:
        <<: &toggle_icon
          icon: >
            if (state == 'on') return 'mdi:toggle-switch';
            return 'mdi:toggle-switch-off';
        <<: *state_color

custom-ui makes this and other customizations globally available to the entities.

It works to turn the icon highlight on, and was actually mentioned in the release notes awhile back when they changed the behavior.