Hi looking for some help.
So I have an mqtt switch and when it turns on, I want to issue a second mqtt command, I know I could do this in an automation but I really want this it be part of the switch setup.
So from the template example Template - Home Assistant
I am trying this
mqtt:
switch:
- unique_id: ideal_thermostat_disable
name: "Thermostat Disable"
command_topic: "IdealBoiler/set/idealC40/command"
availability:
- topic: "IdealBoiler/value/idealC40"
payload_on: "CS=4.0"
payload_off: "CS=0.0"
state_topic: "IdealBoiler/value/idealC40/TSet"
value_template: "{% if value=='4.00' %}on{% else %}off{% endif %}"
state_on: "on"
state_off: "off"
optimistic: false
retain: false
template:
select:
name: "Disable CH"
unique_id: diable_CH
state: "{{ states.switch.ideal_thermostat_disable }}"
options: "{{ ['on'] }}"
select_option:
- service: mqtt.publish
data:
topic: "IdealBoiler/set/idealC40/command"
payload: "CH=0"
But the template never fires, is this a bug or am I doing something wrong?
tom_l
May 2, 2023, 8:41am
2
You are putting the template integration inside the mqtt integration. This is not allowed. In your configuration.yaml file these two integrations must be completely separate and have the same indentation.
Like this:
mqtt:
...
template:
...
Not like this:
mqtt:
...
template:
...
Sorry @tom_l , that was a copy paste error, corrected, it is at the same level, the validator produces no errors etc, just doesn’t fire.
tom_l
May 2, 2023, 8:47am
4
There is no template select. https://www.home-assistant.io/integrations/template/
And even if there were the select option service does not allow you to execute other services, only to make an option selected.
tom_l
May 2, 2023, 8:51am
5
You could hide your mqtt switch and use a template switch. The turn on and turn off actions of the template switch allow more than one service.
switch:
- platform: template
switches:
thermostat_disable_plus:
name: Thermostat Disable Plus
value_template: "{{ is_state('switch.ideal_thermostat_disable', 'on') }}"
turn_on:
- service: switch.turn_on
target:
entity_id: switch.ideal_thermostat_disable
- service: mqtt.publish
data:
topic: "IdealBoiler/set/idealC40/command"
payload: "CH=0"
turn_off:
service: switch.turn_off
target:
entity_id: switch.ideal_thermostat_disable
1 Like
Thank you I’ll give that a try, but I was only going of the page Template - Home Assistant you referred to, it does show a template select at the bottom of the page:
STATE BASED SELECT - CONTROL DAY/NIGHT MODE OF A CAMERA
This show how a state based template select can be used to call a service.
template:
select:
- name: "Porch Camera Day-Night Mode"
unique_id: porch_camera_day_night_mode
state: "{{ state_attr('camera.porch_camera_sd', 'day_night_mode') }}"
options: "{{ ['off', 'on', 'auto'] }}"
select_option:
- service: tapo_control.set_day_night_mode
data:
day_night_mode: "{{ option }}"
target:
entity_id: camera.porch_camera_sd
tom_l
May 2, 2023, 8:57am
7
mark.carter:
STATE BASED SELECT
You’re absolutely right. I misread that.
A template switch still makes more sense, unless you have other options you want to include later?
No works ok, just strange the select did not fire?
tom_l
May 2, 2023, 9:18am
9
What does the select do when the switch is off?
You need more options for it to be valid. Even if you never use that option.
Also your state template is invalid. Try this
template:
select:
name: "Disable CH"
unique_id: diable_CH
state: "{{ states('switch.ideal_thermostat_disable') }}"
options: "{{ ['on', 'off'] }}"
select_option:
- service: mqtt.publish
data:
topic: "IdealBoiler/set/idealC40/command"
payload: "CH=0"
You would also need to construct a service template to only publish the packet for the off state.
I’ll give this a try as well and get back to you thank you!!
So I tried the above, it doesn’t fire the service either, not for either on or off must be a bug?
Has this been confirmed as a bug? I can’t for the life of me get my template select to trigger anything under the select_option either.
edit: seems to be only when it’s updated by state: - would that be expected?
petro
(Petro)
November 21, 2024, 6:36pm
13
That’s how template entities work, unless you omit the value_template/state and allow optimistic mode.
1 Like