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
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 ?
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.
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.
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.
@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).