Trying to understand execution of entity templates and scripts

Hello all,

I am trying to understand more about the inner workings of HA.

What I am trying to do right now is turn on a virtual switch (template entity) and then have code to turn it off immediately and execute another script.

The first thing I am trying is to call the script service and then call the service switch.turn_off on the same entity:

switch:
  - platform: template
    switches:
      hue_mbath_1_virt:
        unique_id: hue_mbath_1_virt
        friendly_name: "Mbath Tap 1 Virt"
        turn_on:
          - service: script.MasterBathroomTap1
          - service: switch.turn_off
            entity_id: switch.hue_mbath_1_virt
        turn_off:
          #

The script.MasterBathroomTap1 only contains this (from the UI editor):

alias: MasterBathroomTap1
sequence:
  - service: switch.toggle
    data: {}
    entity_id: switch.soldering_station_light
  - delay: '.1'
mode: single

This does not work. It will toggle the soldering_station_light the first time, but the hue_mbath_1_virt will remain on, and if I click it again it will not toggle the soldering_station_light. <-- EDIT: Sometimes it works just like below:

If I do the following:

 hue_mbath_2_virt:
        unique_id: hue_mbath_2_virt
        friendly_name: "Mbath Tap 2 Virt"
        turn_on:
          - service: script.MasterBathroomTap2
        turn_off:
          #

And the script being:

alias: MasterBathroomTap2
sequence:
  - service: switch.toggle
    data: {}
    entity_id: switch.soldering_station_light
  - delay: '.1'
  - service: switch.turn_off
    data: {}
    entity_id: switch.hue_mbath_2_virt
mode: single

When I turn on the hue_mbath_2_virt like in the first case, soldering_station_light will toggle and hue_mbath_2_virt will not turn off… but differently than the first case, if I send the on event on hue_mbath_2_virt then it will execute the MasterBathroomTap2 script again.

I’m all for ‘understanding the inner workings’ but I never understand people not using things properly.

The point of a template switch is that it has a value template that defines whether its state is on or off, and it performs an action when you physically switch it on or off.

You haven’t provided it with a value template and you haven’t provided it with a turn_off action, so :man_shrugging:

The use case you have defined is just to run the script, not a template switch. So just put the script entity_id on your lovelace and it will give you a button you can press to run it.

Thanks for your response, obviously there is more to it behind this…

I do not like the delay from Polling the Phillips Hue and I do not want to use deCONZ or anything similar. I tried but I have my reasons why not wanting to use it…
I also do not want to use the HomeKit controller on HA.

But Hue publishes to HomeKit and all button actions happen instantly. So what I am doing is creating “virtual switches” on HA and automations on HomeKit to turn-on the virtual switch. In a sense I am using these as variables so that one platform talks to the other.

It works really fast, so it is doing exactly what I am trying to achieve. But I do want to try to understand more in depth to avoid the caveats of why it sometimes works (the first method I posted) and find if I can simplify my config (by passing template data instead of having multiple scripts)

And that’s fine, but actually test with that, not some random thing that doesn’t do anything and is not the right tool for the job.

Coming to the forums with the example you posted is like me posting on the BMW forums that I’m trying to understand their cars better, so when I ran out of petrol this morning I threw a load of AAA batteries in the fuel tank and drew a picture of a unicorn and I’d like to know why the engine still won’t start :man_shrugging:

The example I posted is exactly what I am doing and how it is working… the only piece missing is the automation from HomeKit… but I can click on the entity… So I stand that the example I posted exactly depicts what I am wondering, which is what happens under the hood when services and events are triggered on HA. What is the order of execution, what happens if I call the switch.turn_on service and the entity writes its own state to off again, are there hidden issues I might run into like a race condition. Thus me stating that I am trying to understand the inner workings of HA…

One can test and live with the result of one test… Or one can try to understand what is actually happening below and make sure that it will always work, specially when things are used not precisely as intended.

Or one could use the correct tool for the job in the first place and not have to worry about the nuances and potential failures of using something ‘not precisely as intended’.

So, which is the correct tool for the job?

If the code you’ve posted is a representative example, then the correct tool for the job is the script, as I said in my first reply to this thread.

But what I am doing with the switch template is exporting it to HomeKit. So when I press the Hue button, it toggles the Switch Template on HomeKit “instantaneously”. The Template then calls the script which in turn turns on and off my lights. This allows me to use the Hue switches without the delay for the polling and without using deCONZ.
It also allows me to export HomeKit only (like iDevices) to HA without having to run a HomeKit controller on HA…

EDIT: So back to my original post, how could I go to try to understand what is really happening in the backend… Why I need to turn off the switch again (on a script or on the template) for the contraption to work again yet when I turn it off the switch in the GUI remains “on” (and also its state if I check with dev tools) even when the script is turning it off…