Variable use of Smart Plugs, with dynamic icons & names

Hi guys,

While I already had 1 Philips Hue Smart Plug, in preparation for the Holiday Season, I bought a few more (innr sp 220-2) to automatically turn on/off different lights around the house. At the same time, I didn’t want to limit the use of these smart plugs to the Holiday Season only, so I worked on a way to

  1. Tell HA what’s plugged into each smart plug
  2. Dynamically create some groups of smart plugs to enable automations that adapt to what’s plugged into each smart plug
  3. Ensure I can manually control the smart plugs from Lovelace UI
  4. Enable dynamic names and icons so the UI is user-friendly

The only tools I used are HA Core and card-mod.

Firstly, I’d be happy to learn from better programmers than myself, if there would’ve been better ways to do what I’ve done.
Second, I wanted to share what I’ve done, so someone else can benefit from what I’d think would be a more common use-case for smart plugs.

1. What’s Connected

For each smart plug, I’ve created a dropdown helper / Input Select [input_select.innr_smart_plug_#] from which to choose what’s plugged in.


If anyone has a suggestion on how to use 1 list of dropdown entries to use across all helpers, please do let me know!

1.1 Add dropdown helpers to a dashboard

To simplify how to tell HA what’s plugged into your smart plug, include the input_select entities in an entities card in your Lovelace dashboard. I’ve done it in a separate tab/view, where I do detailed settings, but don’t need go too often. This avoids cluttering your main tabs.

type: entities
entities:
  - entity: input_select.hue_smart_plug
  - entity: input_select.innr_smart_plug_1
  - entity: input_select.innr_smart_plug_2
  - entity: input_select.innr_smart_plug_3
  - entity: input_select.innr_smart_plug_4

2. Create Groups

Depending on what’s connected, I want to include my smart plugs into automations (e.g. to turn on/off). Of course I could include every smart plug into every automation and check what’s plugged in (i.e. what’s selected for the relevant input_select. This would however become very redundant, an awful lot of work to do and maintain and also not very flexible to scale. So I auomated the creation of groups of smart plug(s), based on what’s plugged into them as soon as any of the input_select is changed:

alias: Groups Create
description: >-
  Create groups for all Christmas lights (inlcuding Christmas tree) and/or WiFi
  Extenders plugged into a Smart Plug (identified through 'input_select'
  helpers)
trigger:
  - platform: state
    entity_id:
      - input_select.hue_smart_plug
      - input_select.innr_smart_plug_1
      - input_select.innr_smart_plug_2
      - input_select.innr_smart_plug_3
      - input_select.innr_smart_plug_4
condition: []
action:
  - service: group.remove
    data:
      object_id: kerstverlichting
  - service: group.set
    data:
      name: Kerstverlichting
      object_id: kerstverlichting
      icon: mdi:string-lights
      entities: >
        {{ states.input_select | selectattr('state', 'search', 'Kerst') |
        map(attribute='entity_id') | join(', ') | regex_replace('input_select',
        'switch') }}
  - service: group.remove
    data:
      object_id: wifi_boven
  - service: group.set
    data:
      name: WiFi Boven
      object_id: wifi_boven
      icon: mdi:wifi-plus
      entities: >
        {{ states.input_select | selectattr('state', 'eq', 'WiFi Boven') |
        map(attribute='entity_id') | join(', ') | regex_replace('input_select',
        'switch') }}
mode: single

3. Control from Lovelace

3.1 Create dynamic Buttons

To enable

  1. Switching individual smart plugs on/off from the HA UI / Lovelace
  2. Icons that match the device plugged into a smart plug AND
  3. A name that helps clarify which device you’re switching on/off

I’ve created template sensors/buttons:

# Smart Plug Buttons with dynamic name and icon, depending on what's plugged in
- button:
  - unique_id: "45DC43"
    press:
    - service: switch.toggle
      entity_id: "switch.hue_smart_plug"
    name: >
      {% if is_state('input_select.hue_smart_plug','WiFi Beneden') %}Beneden
      {% elif is_state('input_select.hue_smart_plug','WiFi Boven') %}Boven
      {% else %}Hue
      {% endif %}
    icon: >
      {% if is_state('input_select.hue_smart_plug','Kerstverlichting') %} mdi:string-lights
      {% elif is_state('input_select.hue_smart_plug','Kerstboom') %} mdi:pine-tree
      {% elif states.input_select.hue_smart_plug.state is search('WiFi') %} mdi:wifi-plus
      {% else %} mdi:connection
      {% endif %}
  - unique_id: "innr1"
    press:
    - service: switch.toggle
      entity_id: "switch.innr_smart_plug_1"
    name: >
      {% if is_state('input_select.innr_smart_plug_1','WiFi Beneden') %}Beneden
      {% elif is_state('input_select.innr_smart_plug_1','WiFi Boven') %}Boven
      {% else %}innr1
      {% endif %}
    icon: >
      {% if is_state('input_select.innr_smart_plug_1','Kerstverlichting') %} mdi:string-lights
      {% elif is_state('input_select.innr_smart_plug_1','Kerstboom') %} mdi:pine-tree
      {% elif states.input_select.innr_smart_plug_1.state is search('WiFi') %} mdi:wifi-plus
      {% else %} mdi:connection
      {% endif %}
  - unique_id: "innr2"
    press:
    - service: switch.toggle
      entity_id: "switch.innr_smart_plug_2"
    name: >
      {% if is_state('input_select.innr_smart_plug_2','WiFi Beneden') %}Beneden
      {% elif is_state('input_select.innr_smart_plug_2','WiFi Boven') %}Boven
      {% else %}innr2
      {% endif %}
    icon: >
      {% if is_state('input_select.innr_smart_plug_2','Kerstverlichting') %} mdi:string-lights
      {% elif is_state('input_select.innr_smart_plug_2','Kerstboom') %} mdi:pine-tree
      {% elif states.input_select.innr_smart_plug_2.state is search('WiFi') %} mdi:wifi-plus
      {% else %} mdi:connection
      {% endif %}
  - unique_id: "innr3"
    press:
    - service: switch.toggle
      entity_id: "switch.innr_smart_plug_3"
    name: >
      {% if is_state('input_select.innr_smart_plug_3','WiFi Beneden') %}Beneden
      {% elif is_state('input_select.innr_smart_plug_3','WiFi Boven') %}Boven
      {% else %}innr3
      {% endif %}
    icon: >
      {% if is_state('input_select.innr_smart_plug_3','Kerstverlichting') %} mdi:string-lights
      {% elif is_state('input_select.innr_smart_plug_3','Kerstboom') %} mdi:pine-tree
      {% elif states.input_select.innr_smart_plug_3.state is search('WiFi') %} mdi:wifi-plus
      {% else %} mdi:connection
      {% endif %}
  - unique_id: "innr4"
    press:
    - service: switch.toggle
      entity_id: "switch.innr_smart_plug_4"
    name: >
      {% if is_state('input_select.innr_smart_plug_4','WiFi Beneden') %}Beneden
      {% elif is_state('input_select.innr_smart_plug_4','WiFi Boven') %}Boven
      {% else %}innr4
      {% endif %}
    icon: >
      {% if is_state('input_select.innr_smart_plug_4','Kerstverlichting') %} mdi:string-lights
      {% elif is_state('input_select.innr_smart_plug_4','Kerstboom') %} mdi:pine-tree
      {% elif states.input_select.innr_smart_plug_4.state is search('WiFi') %} mdi:wifi-plus
      {% else %} mdi:connection
      {% endif %}

After you’ve created the buttons, they’ll adopt an entity_id based on their initial ‘name’, which is driven by the template. To harmonize their entity_id's and simplify the next step (customizing button format), make sure to edit the button’s entity_id's from the UI!

Don’t forget to assign a unique_id to every button. Otherwise you will not be able to edit the entity_id!

3.2 Customize button format

Unfortunately buttons are stateless, which doesn’t stop the button from toggling on/off, but it does stop Lovelace from showing the current state (on/off/unavailable). To correct this in the UI, I’ve used card-mod in the implementation of the buttons on my dashboard (example for 1 button):

type: button
entity: button.innr_smart_plug_1
tap_action:
  action: toggle
icon_height: 50px
style: |
  ha-card {
    font-size: 1rem !important;
    --paper-item-icon-color:
        {% if is_state('switch.innr_smart_plug_1','unavailable') %} var(--state-unavailable-color)
        {% elif is_state('switch.innr_smart_plug_1','on') %} var(--state-icon-active-color)
        {% elif is_state('input_select.innr_smart_plug_1','Kerstboom') %} darkgreen
        {% else %} var(--paper-item-icon-color)
        {% endif %}
      ;
  }

4. Include the group(s) in your automations

Voila… you can now:

  • Use the drop-down helpers to tell HA what you’ve plugged into your smart plug
  • Control them through dynamic buttons with a helpful icon & name
  • Include the dynamic device groups in your automations to control the devices plugged into your smart plugs, depending on what device is plugged in
2 Likes

Yeah!
I was searching for this.
In the meantime, any update?