Template Switch not holding State in UI

Hello Members,

I hope you all are doing well in this difficult time.

I am now to HA so if I ask for some common issue please let me know.

I want a switch that can run a script when it is on and when it is off. (Individual script)

I set up the switch using a template but not able to get success. I think I am missing something very silly :frowning:

configuration.yaml (Showing the part on which i am facing issue)

- platform: template
    switches:
      study_b:
        friendly_name: study b
        turn_on:
          - service: switch.turn_on
            target:
              entity_id: switch.study_b
          - service: script.bon
        turn_off:
          - service: switch.turn_off
            target:
              entity_id: switch.study_b
          - service: script.boff
        value_template: "{{ states('switch.study_b') }}"

What I am getting as result is when I turn on the switch it turns on GUI for few second and come back to off. But it only triggers the on script.

When I see on developer tools → state it wont change when we press the switch.

Looking for your help.

Thanks
Bhavik

A template switch binds a pair of entities to act together as one.
What you have shown is not that.
From what you have written I would suggest you just use a couple of automations

automation:
  ## Switch On
  - alias: au_switch_on
    mode: single
    max_exceeded: silent
    trigger:
      - platform: state
        entity_id: switch.study_b
        from: 'off'
        to: 'on'
    action:
      - service: script.bon
  - alias: au_switch_off
    mode: single
    max_exceeded: silent
    trigger:
      - platform: state
        entity_id: switch.study_b
        from: 'on'
        to: 'off'
    action:
      - service: script.boff

This could be simplified (well … reduced to one automation) but this answers your question

Thanks for the reply.

As I am new to the HA i dont know much about it.

Can you please let me know where i have to put this script?

Also for switch where i have to write the code and what code i have to write?

Thanks
Bhavik

This is not a ‘script’ this is two automations.
It says automation: at the top (one header, multiple automations can go under this header)
Automationv ‘react’ to things that happen that HA can detect.

Scripts are lists of actions that can be called from multiple locations. Thus a complex bit of code that needs to be consistent but called from different actions.
They are headed by : -
script:
Multiple scripts can go under that header

Location
That’s dependent on your setup
I use packages so this could go into there, or direct into configuration.yaml - but if you have a new installation then it will assume stuff will go into automation.yaml. That being the case you need to modify the yaml to something like : -

- id: test01
  alias: au_switch_on
  mode: single
  max_exceeded: silent
  trigger:
    - platform: state
      entity_id: switch.study_b
      from: 'off'
      to: 'on'
  action:
    - service: script.bon
- id: test02
  alias: au_switch_off
  mode: single
  max_exceeded: silent
  trigger:
    - platform: state
      entity_id: switch.study_b
      from: 'on'
      to: 'off'
  action:
    - service: script.boff

Switch: what switch ?

I’m assuming that you have the actual switch (the physical one denoted by switch.study_b) fully integrated ?
What is in the scripts you mentioned
Why aren’t you using the gui editor ?

I don’t know whether you understand my problem or not?

Yes, I have a physical switch (controller + relay).

The thing I am trying to create is as below.

switch_ha

If you want a switch with an assumed state, use an input boolean instead.

Add an input boolean here, name it input_boolean.study_bOpen your Home Assistant instance and show your helper entities.

Then, change your switch template to:

  - platform: template
    switches:
      study_b:
        friendly_name: study b
        turn_on:
          - service: input_boolean.turn_on
            target:
              entity_id: input_boolean.study_b
          - service: script.bon
        turn_off:
          - service: input_boolean.turn_off
            target:
              entity_id: input_boolean.study_b
          - service: script.boff
        value_template: "{{ states('input_boolean.study_b') }}"
2 Likes

Thanks @petro

Your suggestion works for me.

But I have a question about the approach which is why we use boolean?

Is there any other way using which we can do the same?

turn_off:
          - service: switch.turn_off
            target:
              entity_id: switch.study_b
          - service: script.boff

Why this state of code is not working?

As I am new, I need to understand the working of these all thing that’s the reason I am asking this.

Appreciate your answer.

Thanks
Bhavik

That doesn’t work because it’s referencing itself. Template integrations pull information from other integrations, not itself. When you reference itself, and itself doesn’t have a state, you end up with no state at all times. Which is what you’re doing.

Thanks, @petro Ok I got your point.

Is there any other thing which we can use to do the same (instead of boolean).

Also, one thing I want to know is why can’t we use a variable in mqtt switch. As it is not possible I have to use this thing to send value when the switch is turned on or off.

Thanks
Bhavik

not when you need to make an assumed state.

I have no clue what you’re saying here. MQTT switches have the ability to do pretty much anything.

we can’t send payload_template in mqtt switch that is what see.

switch:
  - platform: mqtt
    name: "test"
    state_topic: "feedback/xyz"
    command_topic: "control/xyz"
    payload_on: "a:b:c"              #a, b, c are variable and is the value of different entity.  
    payload_off: "a:b:c"
    state_on: "i:j:k"                    #i, j, k is value sent by device in reply.
    state_off: "i:j:k"

here we can not pass the variable we can only pass the static string.
a, b, c are variable and is the value of a different entity.

This is what I know. (might be wrong)

Thanks
Bhavik

value_template allows you to set the state of the switch based on the response of the device.

Thanks @petro

Is it possible to give you an example for the same?

Thanks
Bhavik

No, I don’t use them. There are plenty of examples on the forums.

https://community.home-assistant.io/search?q=mqtt%20switch%20value_template

Thanks @petro

Thanks for your valuable time and inputs I learn something new from you which will help me to solve my problem.

Thanks
Bhavik

@petro Just a quick question. Let’s say that switch is off from the hardware side but it is turn on at HA. How to change it according to hardware. (We always send state via mqtt when it changed).

your device has to report a state. If it doesn’t then you won’t be able to capture that.

Thanks @petro
it is report state.
What changes need to done in HA?