Legacy switch template deprecation with include files

Hi,

I read a lot about the template migration to the new format but still don’t get it entirely.
I got 61 warnings for migrating my templates.

I use a splitting setup and these are the corresponding lines in my configuration yaml:

group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml
binary_sensor: !include binary_sensors.yaml
switch: !include switches.yaml
sensor: !include sensors.yaml
notify: !include notifications.yaml
alarm_control_panel: !include alarm_control_panel.yaml

In order to understand the procedure I started with the small file switches.yaml. This looks like this:

#====================================#
#   Switch - Smart Mi Humidifier 2   #
#====================================#
- platform: template
  switches:
    smart_mi_humidifier_2_dry:
      friendly_name: "Dry Mode"
      value_template: "{{ is_state_attr('fan.smart_mi_humidifier_2', 'dry', true) }}"
      turn_on:
        action: xiaomi_miio_airpurifier.fan_set_dry_on
        data:
          entity_id: fan.smart_mi_humidifier_2
      turn_off:
        action: xiaomi_miio_airpurifier.fan_set_dry_off
        data:
          entity_id: fan.smart_mi_humidifier_2
      icon_template: mdi:lightbulb-outline

    smart_mi_humidifier_2_child_lock:
      friendly_name: "Child lock"
      value_template: "{{ is_state_attr('fan.smart_mi_humidifier_2', 'child_lock', true) }}"
      turn_on:
        action: xiaomi_miio_airpurifier.fan_set_child_lock_on
        data:
          entity_id: fan.smart_mi_humidifier_2
      turn_off:
        action: xiaomi_miio_airpurifier.fan_set_child_lock_off
        data:
          entity_id: fan.smart_mi_humidifier_2
      icon_template: "mdi:lock-outline"

    smart_mi_humidifier_2_buzzer:
      friendly_name: "Buzzer"
      value_template: "{{ is_state_attr('fan.smart_mi_humidifier_2', 'buzzer', true) }}"
      turn_on:
        action: xiaomi_miio_airpurifier.fan_set_buzzer_on
        data:
          entity_id: fan.smart_mi_humidifier_2
      turn_off:
        action: xiaomi_miio_airpurifier.fan_set_buzzer_off
        data:
          entity_id: fan.smart_mi_humidifier_2
      icon_template: "mdi:volume-high"

Based on the recommendation

Note: If you are using switch: !include <filename>.yaml in configuration.yaml, remove the switch definition from the included <filename>.yaml.

I ended up with this but it doesn’t seem right and of course also doesn’t work:

#====================================#
#   Switch - Smart Mi Humidifier 2   #
#====================================#
template:
  - turn_on:
    - action: xiaomi_miio_airpurifier.fan_set_dry_on
      data:
        entity_id: fan.smart_mi_humidifier_2
    turn_off:
    - action: xiaomi_miio_airpurifier.fan_set_dry_off
      data:
        entity_id: fan.smart_mi_humidifier_2
    default_entity_id: switch.smart_mi_humidifier_2_dry
    icon: mdi:lightbulb-outline
    name: Dry Mode
    state: '{{ is_state_attr(''fan.smart_mi_humidifier_2'', ''dry'', true) }}'


    - turn_on:
    - action: xiaomi_miio_airpurifier.fan_set_child_lock_on
      data:
        entity_id: fan.smart_mi_humidifier_2
    turn_off:
    - action: xiaomi_miio_airpurifier.fan_set_child_lock_off
      data:
        entity_id: fan.smart_mi_humidifier_2
    default_entity_id: switch.smart_mi_humidifier_2_child_lock
    icon: mdi:lock-outline
    name: Child lock
    state: '{{ is_state_attr(''fan.smart_mi_humidifier_2'', ''child_lock'', true)

  - turn_on:
    - action: xiaomi_miio_airpurifier.fan_set_buzzer_on
      data:
        entity_id: fan.smart_mi_humidifier_2
    turn_off:
    - action: xiaomi_miio_airpurifier.fan_set_buzzer_off
      data:
        entity_id: fan.smart_mi_humidifier_2
    default_entity_id: switch.smart_mi_humidifier_2_buzzer
    icon: mdi:volume-high
    name: Buzzer
    state: '{{ is_state_attr(''fan.smart_mi_humidifier_2'', ''buzzer'', true) }}'

Can anyone point me to the right way of migrating this switches.yaml?

thanks in advance

In configuration.yaml, remove the switch: includes line and add:

template: !include templates.yaml

In templates.yaml:

- switch:
    - name: Dry Mode
      default_entity_id: switch.smart_mi_humidifier_2_dry
      state: "{{ is_state_attr('fan.smart_mi_humidifier_2', 'dry', true) }}"
      turn_on:
        action: xiaomi_miio_airpurifier.fan_set_dry_on
        data:
          entity_id: fan.smart_mi_humidifier_2
      turn_off:
        action: xiaomi_miio_airpurifier.fan_set_dry_off
        data:
          entity_id: fan.smart_mi_humidifier_2
      icon: mdi:lightbulb-outline

    - name: Child lock
      default_entity_id: switch.smart_mi_humidifier_2_child_lock
      state: "{{ is_state_attr('fan.smart_mi_humidifier_2', 'child_lock', true) }}"
      turn_on:
        action: xiaomi_miio_airpurifier.fan_set_child_lock_on
        data:
          entity_id: fan.smart_mi_humidifier_2
      turn_off:
        action: xiaomi_miio_airpurifier.fan_set_child_lock_off
        data:
          entity_id: fan.smart_mi_humidifier_2
      icon: mdi:lock-outline

See the Template docs for how to set up the entities:

great. thank you.
I followed your advise and got 3 warnings less. :slight_smile:

some questions for better understanding the procedure.

  • by using a single file templates.yaml instead of different yaml files for switches, sensors, binary_senors etc., it gets a little more confusing for me. Is that change on purpose or are there options to still have separated files for different kind of templates?
  • the order of lines of these switches seems to be based on the template tutorial you linked to. However the proposal under warnings uses a different order. I understand, that it doesn’t make difference for functionally but which one is, let’s say, the better practice?

OK, so [docs]:

template: !include_dir_merge_list templates

Then you can have templates/switches.yaml, templates/sensors.yaml or however you want to name them. You can mix different template entities in the same file if you want:

- sensor:
    - name: my template sensor
      [rest of config]

    - name: another template sensor
      [rest of config]

- binary_sensor:
    - name: my template binary sensor
      [rest of config]

The order of declarations in the config (except for sequences of actions etc) is purely down to preference and makes no difference to performance. I started by copying and updating your config, then realised that for exact 1:1 behaviour I needed to convert the “slug” to a default_entity_id line.

1 Like