Newbie question about switch.turn_on switch.turn_off

Hi,

I have 2 switches that are tied to 2 device trackers and when the switch is turned on or off then he must do a MQTT publish.

When the switches aren’t tied to the device trackers and I switch the manualy everytings work.
When the switches are tied to the device trackers then the MQTT publish isn’t triggerd.

Below you can find my code


switch:
  - platform: template
    switches:
      lucie_switch:
        friendly_name: "Lucie thuis"
        value_template: "{{ is_state('device_tracker.gsm_lucie', 'home') }}"
        turn_on: 
          service: mqtt.publish
          data:
            topic: domoticz/ha
            payload: >-
                {"command":"switchlight",
                "idx": 387,
                "switchcmd": "On"
                } 
        turn_off:
          service: mqtt.publish
          data:
            topic: domoticz/ha
            payload: >-
                {"command": "switchlight",
                "idx": 387,
                "switchcmd": "Off"
                }
  - platform: template
    switches:
      koen_switch:
        friendly_name: "Koen thuis"
        value_template: "{{ is_state('device_tracker.gsm_koen', 'home') }}"
        turn_on:
          service: mqtt.publish
          data:
            topic: domoticz/ha
            payload: >-
                {"command": "switchlight",
                "idx": 382,
                "switchcmd": "On"
                } 
        turn_off:
          service: mqtt.publish
          data:
            topic: domoticz/ha
            payload: >-
                {"command": "switchlight",
                "idx": 382,
                "switchcmd": "Off"
                }

A Template Switch is not an automation, the turn_on and turn_off actions are only executed when a service call is applied to the switch itself. If you want the state of the device trackers to affect their corresponding switch you need an automation.

If you describe your goals and needs we can suggest other possible methods.