Help using Templates

Hi, if possible I would like some help with templates, the code below works fine, I can select from the front end which light I want associated with my wifi IOT button, push the button and it toggles the associated light …

is there a way using a template to make the code more eloquent rather than having multiple automations, I’ve tried but I just can’t get my head around the template format and need some pointers.

Thanks

Config yaml:
sensor:

  • platform: mqtt
    name: “wifi_IOT”
    state_topic: “wifi_button_1”

input_select:
wifi_button_1_mode:
name: WIFI Button mode
options:
- “Bedroom Light”
- “Computer Light”
- “Dinning Room Light”
- “Outside Lights”
icon: mdi:target

Automation yaml:

  • alias: “WIFI IOT Button Computer”
    hide_entity: true
    trigger:

    • platform: state
      entity_id: sensor.wifi_iot
      to: ‘connected’
      condition:
      condition: state
      entity_id: input_select.wifi_button_1_mode
      state: “Computer Light”

    action:

    • service: homeassistant.toggle
      entity_id: switch.computer_lamp
  • alias: “WIFI IOT Button Dinning room”
    hide_entity: true
    trigger:

    • platform: state
      entity_id: sensor.wifi_iot
      to: ‘connected’
      condition:
      condition: state
      entity_id: input_select.wifi_button_1_mode
      state: “Dinning Room Light”

    action:

    • service: homeassistant.toggle
      entity_id: switch.dinning_room
  • alias: “WIFI IOT Button Outside Lights”
    hide_entity: true
    trigger:

    • platform: state
      entity_id: sensor.wifi_iot
      to: ‘connected’
      condition:
      condition: state
      entity_id: input_select.wifi_button_1_mode
      state: “Outside Lights”

    action:

  • service: homeassistant.toggle
    entity_id: switch.outside_lights

  • alias: “WIFI IOT Button Bedroom Light”
    hide_entity: true
    trigger:

    • platform: state
      entity_id: sensor.wifi_iot
      to: ‘connected’
      condition:
      condition: state
      entity_id: input_select.wifi_button_1_mode
      state: “Bedroom Light”

    action:

  • service: homeassistant.toggle
    entity_id: switch.bedroom_lamp

First, when posting any code please follow the directions at the top of the page about formatting the code.

Yes, this is fairly straight forward:

- alias: WIFI IOT Button
  hide_entity: true
  trigger:
    platform: state
    entity_id: sensor.wifi_iot
    to: connected
  action:
    service: homeassistant.toggle
    data_template:
      entity_id: >
        {{ {'Computer Light': 'switch.computer_lamp',
            'Dinning Room Light': 'switch.dinning_room',
            'Outside Lights': 'switch.outside_lights',
            'Bedroom Light': 'switch.bedroom_lamp'}
           [states('input_select.wifi_button_1_mode')] }}

Basically this is using the state of input_select.wifi_button_1_mode to select one of the entries in the dictionary whose keys are the options of that input_select and whose values are the entity_id’s of the corresponding switches.

Thank you so much, I’ve been trying for ages to get a template set up …with no success, your code works a treat, thanks again.

1 Like