How to add an unique_id to a restful switch

Hello,

Can I add an unique_id to a restful switch so that I can give it an area and icon setup through the GUI? Or how would I do this without the GUI?

I have several restful switches that each belong to an area. I like the switch to show up on the correct LoveLace card with the correct icon.

You can’t. As a work-around, you can create a template switch out of your restful switch, and add an unique_id there.

2 Likes

Thanks,

I’ve worked it out but I am confused. Maybe you can clear it up for me.

The restful switch is this:

switch:
  - platform: rest
    name: water_zone1
    resource: http://192.168.0.200/api/zone_1
    body_on: '{"relay":{"value":true}}'
    body_off: '{"relay":{"value":false}}'
    is_on_template: "{{ value_json['relay']['value'] }}"

Then I created the following template switch:

- platform: template
    switches:
      waterzone1:
        value_template: "{{ is_state('switch.water_zone1', 'on') }}"
        turn_on:
          service: switch.turn_on
          target:
            entity_id: switch.water_zone1
        turn_off:
          service: switch.turn_off
          target:
            entity_id: switch.water_zone1

This created a Lovelace card with 3 switches.

Group switch
water_zone1 switch (the restful switch)
waterzone1 switch (the template switch)

I then looked up the entity waterzone1 and applied the correct area to it and I change the icon. Once reloaded HAS, the group switch disappeared, restful switch (water_zone1) is on it’s own again and waterzone1 (template switch) was merged into the card of the area I assigned to it. Good!

But if I use entity_id: switch.water_zone1 to identify the restful switch in the template, it means it has an identity_id. So why this big detour?

And can you confirm that with:

- platform: template
    switches:
      waterzone1: <----

waterzone1 is here the defined entity_id? I do not have to add an extra line entity_id: <name>? I’ve seen this in some examples in this forum but when I use it, HAS seems to ignore it.

Is the code above clean or dirty?

Last question :slight_smile: the restful switch is now a bit useless in the Lovelace. What is the common way to have it not show up?

It seems you use the autogenerated lovelace. You can’t hide the restful switch unfortunately, only if you take control of lovelace.
Your template definition seems ok.

Wow, you are fast :slightly_smiling_face:

Yes you are right, I use the auto generated Lovelace. I will create another Dashboard that I will not auto generate.

Thanks again!

For anyone who finds this with the same question:
You can in fact set a unique ID.

switch:
  - platform: rest
    name: water_zone1
    unique_id: water_zone1
    resource: http://192.168.0.200/api/zone_1
    body_on: '{"relay":{"value":true}}'
    body_off: '{"relay":{"value":false}}'
    is_on_template: "{{ value_json['relay']['value'] }}"