Template Switch - Value

Hi all,

I know, I’m a disaster but I keep on trying in creating my configuration. I have created a template switch which activate a script for powering on a light and another one to power it off.
Both scripts are working fine but the Switch is only powering on the light, because after few seconds the switch itself goes to off (and the light stays on).
I think the problem is the value_template, but I don’t know how I should address this problem.

 - platform: template
   switches:
      luce:
        value_template: "{{ is_state('switch.luce', 'on') }}"
        turn_on:
          service: script.beamit_a2on
#          entity_id: script.beamit_a2on
        turn_off:
          service: script.beamit_a2off
#          entity_id: script.beamit_a2off

Any help ?

Thanks a lot

your service should be script.turn_on

Thanks but I don’t have any script.turn_on object defined… and I have multiple scripts, I cannot have them all with the same name.
I did try your suggestion, by enabling also the entity id, but I still have the same problem.
The Switch immediatelly go back to off, with the light staying on. :frowning:

Try this…

 - platform: template
   switches:
      luce:
        value_template: "{{ is_state('switch.luce', 'on') }}"
        turn_on:
          service: script.turn_on
          entity_id: script.beamit_a2on
        turn_off:
          service: script.turn_on
          entity_id: script.beamit_a2off
1 Like

Thanks BurnsHA

Nothing. It switches on the light but the “switch button” goes back to the off position immediately and the light stay on. Something is preventig the switch to stay on the ON position.
:frowning:

ive never tried to use the state of the switch itself as the value template for the same switch, let me do some testing and see if i can get it to work

Thanks a lot for your help. Really appreciated.
In all my test, I’ve also seen a strange thing with Script. If I had a delay option, the UI shows the toggles button instead of the ACTIVATE link. Any idea if this is a bug or the script can be also used as a switch ?

It depends on how many services you have attached to your script, for example if you just have 1 service to power off the devices, then it will show ACTIVATE, but if you say turn off 2 devices or something where you call more than 1 service in the script, it shows a toggle instead.

Adding to that, you can customize the script to always appear as Activate, if you set property can_cancel: false (or as switch with can_cancel: true).

I didnt even know that, thats great!! Do you happen to know if you can use the actual switch for the value template of that switch? i was unable to get it to work either…

I have a switch for my Samsung TV and it is setup like this:

### TV Switch ###
- platform: template
    switches:
      tvsala:
        friendly_name: "TV"
        value_template: "{{ is_state('media_player.tv_sala', 'on') }}"
        turn_on:
          service: media_player.kodi_call_method
          data:
            entity_id: media_player.kodi
            method: Addons.ExecuteAddon
            addonid: script.json-cec
            params:
              command: activate
        turn_off:
          service: media_player.turn_off
          entity_id: media_player.tv_sala

I don’t think it will work properly that way. All documentation points to value_template being from a different entity (as mine above).

I had a similar issue before with an MQTT switch and it worked well like this (actually the trick was to put command_topic and state_topic the same):

### MQTT ###
- platform: mqtt
  name: 'Not@Home'
  state_topic: 'switch/not_at_home'
  command_topic: 'switch/not_at_home'
  retain: true
  optimistic: false
  payload_on: "on"
  payload_off: "off"

If this is a switch to turn on/off a light, why don’t you get the light entity state instead on the value_template?

Any reason to call a script instead of light.turn_on, light.turn_off or even light.toggle?

Hi,
thanks for the help. I’m trying to create switches for the Harmony Hub device, which I’m using to control the lights and cover control in the living room.
For doing that I need to send specific command and I’m not using the activities.
It seems I can only send command with script and nothing else.
When you say about the light state, do you have a simple example ? I couldn’t find anything easy to understand.

Thanks a lot

What is the entity id for the light you’re controlling? Do you also have it shown in HASS?

Hi pplucky, I don’t have any entity id for the light. I only have the entity of the IR Controller, which is capable of sending specific commands to the IR sensor in the living room and activate lights, covers and so on.
So I don’t have any entities for each lights or cover, only the remote controller.
I saw there is something specific for Harmony HUB on HA, but it is only able to control activity, not single command which I need.
That’s why I’ve started creating script for the Harmony HUB command and now I would like to create switches on top of them to properly use them for the lights, cover…

Thanks

Have you looked at the send_command service for the Harmony Hub Remote component? I don’t have such a device, but by the description it looks like being able to do what you want/need:

Send Command:
Send a single command or a set of commands to one device, device ID and available commands are written to the configuration file at startup. You can optionally specify the number of times you wish to repeat the command(s) and delay you want between repeated command(s).

Maybe someone else who also owns such a device can help you more, sorry.

I had an idea that might work.

I don’t know if you use MQTT, but if you can create an MQTT switch (similar to the one I have shown above), you could use the script and call it in an automation when the MQTT switch was turned on/off.

What you would need to make sure is that you also change the MQTT state topic value accordingly (so that your switch displays the state of your light) in the automation itself.

This way your script would serve to physically turn on/off via the automation and the MQTT switch would serve to show the light status and logically turn on/off (triggering the automation).

Something like this (not tested):

switch:
  - platform: mqtt
    name: 'Luce'
    state_topic: 'switch/luce'
    command_topic: 'switch/luce'
    retain: true
    optimistic: false
    payload_on: "on"
    payload_off: "off"

automation:
  - alias: 'Luce light on'
    trigger:
      platform: state
      entity_id: 'switch.luce'
      from: 'off'
      to: 'on'
    action:
      - service: script.turn_on
        entity_id: script.beamit_a2on
      - service: mqtt.publish
        data:
          topic: 'switch/luce'
          payload: 'on'
          retain: true
  - alias: 'Luce light off'
    trigger:
      platform: state
      entity_id: 'switch.luce'
      from: 'on'
      to: 'off'
    action:
      - service: script.turn_on
        entity_id: script.beamit_a2off
      - service: mqtt.publish
        data:
          topic: 'switch/luce'
          payload: 'off'
          retain: true

Let me know how it goes.

1 Like

Hi !
Thanks for the suggestion, I will also try your solution but I’m not very familiar with MQTT.
Yesterday I’ve put togheter this:

input_boolean:
  light_main_left:
    name: Controls Main Left Light
    initial: off
  cover_main_right:
    name: Controls Main Right Cover
    initial: off

- alias: 'Prova Beamit Tapparella Su'
  trigger:
    platform: state
    entity_id: input_boolean.cover_main_right
    state: 'on'
  action:
    service: script.beamit_a5on

- alias: 'Prova Beamit Tapparella Giu'
  trigger:
    platform: state
    entity_id: input_boolean.cover_main_right
    state: 'off'
  action:
    service: script.beamit_a5off

In this way I have the switches and it seems to work fine. Now the question is, do because this script are driving lights and cover, is there a way to change the icon and have the correct one ? So, light “switch” style and same for cover ?

Thanks a lot

It is possible, but you need a switch template for that and use icon_template to determine the icon.

Hi,

thanks. I meant not really the icon but the UI of the Switch, I would like to have those:

and

for the cover.
Any way to have those instead of this ?

Thanks again for your patience! Really appreciated.

Ok, I found out to do it for the light.

 input_boolean.light_main_left:
      assumed_state: true

I cannot find for making it like a cover. I’ve used this but doesn’t work. with the input_boolean. :frowning:

  input_boolean.cover_main_left:
      assumed_state: true
      device_class: cover