Add a button in lovelace that toggles between two scenes

I’m trying to accomplish a similar thing, but it’s not working for me. I’d like to add a button in lovelace that toggles between two scenes, one that sets my indoor cameras as on and the other one as off.
I tried following the above solution but it’s not working because it says “no type provided”.
I tried adding “type: button” but in this case it gives an error for more-info missing information.
If I add the more info entity, it won’t act as a switch/button but it will open the specified entity card (tried with different ones)

Here’s my code:

type: button
show-name: true
show-status: true
name: Cam
switch:
  - platform: template
    switches:
      alarm:
        value_template: '{{ is_state(''select.tapo_cam_studio_person_detection'', ''off'') }}'
        turn_on:
          service: scene.turn_on
          target:
            entity_id: scene.cam_indoor_on
        turn_off:
          service: scene.turn_on
          target:
            entity_id: scene.cam_indoor_off
entity: switch.cam_studio

Any help would be appreciated. Thank you very much.

You’ve tried to put the definition of a template switch into a card. Those are two unrelated things.

Define the template switch in configuration.yaml, define the card in your dashboard.

I see. Sorry I am a noob :frowning:

Ok, I’ve now put these lines in my configuration.yaml:

switch:
  - platform: template
    switches:
      alarm:
        value_template: '{{ is_state(''select.tapo_cam_studio_person_detection'', ''off'') }}'
        turn_on:
          service: scene.turn_on
          target:
            entity_id: scene.cam_indoor_on
        turn_off:
          service: scene.turn_on
          target:
            entity_id: scene.cam_indoor_off

Is this correct?
Also, now I have no idea how to create the button in the dashboard…
Thank you for your help.

Run a config check and find out :wink:

You’ll have a switch entity (after you reload the YAML), just add it like any other entity.

Ok, I feel like an idiot but I tried:

ha core check

which I think it’s what you were suggesting, but I get this error:

permission denied while trying to connect to the Docker daem
on socket at unix:///var/run/docker.sock: Get "http://%2Fvar
%2Frun%2Fdocker.sock/v1.24/containers/hassio_cli/json": dial
 unix /var/run/docker.sock: connect: permission denied

:sweat:

Anyway it seems like it’s working. I managed to add the button, although it stays grey all the time. I was expecting it to become yellow when set to on. What am I doing wrong?

Here’s the button code:

show_name: true
show_icon: true
type: button
tap_action:
  action: toggle
entity: switch.cam_toggle
show_state: true
name: Indoor Cam

What am I doing wrong?
Thank you

https://www.home-assistant.io/dashboards/button#state_color?

I talked too soon.
Actually the button doesn’t work at all. It works inversely (when I click on it and it turns on it actually turns the cameras off) only when I go to Settings → Devices & Services → Entities and toggle the entity itself.
But in the button above, it doesn’t do anything.

Edit: ok, I managed to make it work. I had to delete the value_template line because it was unnecessary and it only made things complicated. Now everything works as it should.
Thank you very much for all the help.

The value template provides feedback on your switch state in case it is changed outside of HA. Without this you are operating in optimistic mode and the state of the switch shown in HA could get out of sync with the actual switch state.

A better solution than deleting the template would be to work out why you are not getting the correct result from it.

Add the template to Developer Tools → Template editor. Change the switch state and observe the template result to try and work out what you have incorrect. It could be as simple as an incorrect letter case, e.g.

'{{ is_state(''select.tapo_cam_studio_person_detection'', ''Off'') }}'
1 Like

Thanks tom_l for the advice and explanation.

I will definitely look into the matter, even though in this case there’s no way for the switch state to be changed outside of HA, because everything is managed through HA.

Also, the state of the switch is not really important because all the changes to it are made through scenes, which as I understand them, kind of “overwrite” the state in any case.

Still I understand why you pointed this out and since it’s probably going to be useful in the future I will do my best to look into this and understand more of how value_template works.

Thank you very much.

Ok, I have looked into this, but I can’t seem to find the solution.
I’m probably getting something wrong: in my intentions I want that the state of the switch template is reported as on when the state of switch.cam_sala_2 is on.

Since switch.cam_sala_2 is a Zigbee device I went to the Zigbee2MQTT integration and checked its reported state, which is ON (all capitals).

Therefore I put these lines in my configuration.yaml :

switch:
  - platform: template
    switches:
      alarm:
        value_template: '{{ is_state(''switch.cam_sala_2'', ''OFF'') }}'
        turn_on:
          service: scene.turn_on
          target:
            entity_id: scene.cam_indoor_on
        turn_off:
          service: scene.turn_on
          target:
            entity_id: scene.cam_indoor_off

But the reported state of the template switch is wrong (it’s set to off even if switch.cam_sala_2 is ON, and the button on the dashboard is not working.

I also tried changing it to:

value_template: '{{ is_state(''switch.cam_sala_2'', ''ON'') }}'

but nothing changes.

I tried using the Template Editor but I get this error:

TemplateSyntaxError: expected token ',', got 'switch'

which goes away if I change the line to

value_template: '{{ is_state('switch.cam_sala_2', 'ON') }}'

(removing one “apostrophe”) but that prevents the configuration to reload.

I’m really clueless. :disappointed_relieved:

That’s not the state though, the state is on or off - case matters.

value_template: "{{ is_state('switch.cam_sala_2', 'on') }}"

is what you want (note the different quotes outside and inside the template)

2 Likes

I tried but I get the following error when checking the configuration:

Error loading /config/configuration.yaml: while parsing a block mapping
in "/config/configuration.yaml", line 46, column 9
expected <block end>, but found '{'
in "/config/configuration.yaml", line 46, column 27

Line 46 is the one with the value_template written the way you suggested.

:sweat:

Edit: scratch all of the above, I think I made a typo somewhere because after copying and pasting your line it doesn’t give any errors anymore and everything works as intended.
Damn, HA is hard!!
Thank you so so much for your patience in helping me.

Another question though: is it possible to check two statuses? For example, what if one wanted the status of the template switch as on when the state of both switch.switch_1 and switch.switch_2 is on? Or when only one of the two switches report its state as on?
Is this doable?

Very

value_template: "{{ is_state('switch.switch_1', 'on') and is_state('switch.switch_2', 'on') }}"

Great, thank you!
Would this work too?

value_template: "{{ is_state('switch.switch_1', 'on') or is_state('switch.switch_2', 'on') }}"

Yours will be true when either is on. The one I wrote only when both are on.

Have a read of the template docs.

1 Like

Of course, that was what I intended. Thank you for the clarification.

I have read it all but, frankly, it didn’t clarify much to me because it’s way too technical and beyond my comprehension level. I acknowledge I am not very skilled in this kind of things, and I will do my best to learn more.

Then I’d recommend joining the HA Discord and asking for help in the templates channel. The folks there are really good at helping people learn templating.

Thanks for the suggestion, I will definitely give it a try!