Define virtual switches to command sequences (scripts) Home Assistant

5 minutes of reading
Guide purposes:
  • Trigger script sequences via virtual switches defined by the component "Input Boolean".
  • Difficulty level: basso
Concepts dealt with:
  • Software configuration
Software components used:
Prerequisites:
Physical devices used:
GUIDE more indicated for:

All environments

Notes and disclaimer
  • qualsiasi modifica all'impianto elettrico dev'essere effettuata da personale qualificato;
  • any changes implemented in probefore is a propersonal responsibility as well as a profirst risk and danger (this guide is for educational purposes only);
  • any changes implemented in probefore a device will void the warranty.
Driving review: 1.0

Abstract

Often and willingly in home automation it happens that there are some sequences of actions that are actually considered, by the user, as a unicum; a case in point could be that of turning on the news: turning on the TV, maybe the satellite box, choosing the source (for example HDMI1), choosing the channel and voilà, the newscast is served.

Still remaining in the example a very good result, for the user, would be in itself that of having integrated presso Home Assistant la profirst TV and maybe the satellite box, maybe using an effective technique like infrared; there remains the difficulty of defining a sequence which, starting from the entities deriving from the above integrations, allows to obtain the aforementioned purpose.

To support this need the "Script" component of Home Assistant that, as sappIAMO, allows to define in configuration proof the sequences which, when triggered, use services and entities to perform, for theappgrease, of the actions in sequence.

Such "Script" type entities can also be exposed to smart speakers in order to be checked by voice; in the example above, a hypothetical sequence called script.telegiornale could easily be triggered by asking ad Alexa / Google Assistant / Siri of "turn on the newscast"

But how is it possible, otherwise, trigger such scenes from the frontend of Home Assistant?
This guide will illustrate how to define "virtual switches" using the "Input Boolean" component in order to trigger, appgrease, one or more sequences.

assumptions

For this guide we will assume the presence of a script called "script.telegiornale_on_ir_sequence" who proWatch the TV ("media_player.tv"), The Sky decoder ("media_player.sky'), proselect 100 channel (corresponding to Sky TG 24) by sending infrared codes related to the remote control keys and finally select the TV's HDMI1 source (on which the Sky box is presumably connected).

Just for culture, the script could be defined as follows:

script:
  telegiornale_on_sequence:
    alias: "Telegiornale"
    sequence:
      - service: media_player.turn_on
        entity_id: media_player.tv
      - service: media_player.turn_on
        entity_id: media_player.sky
      - delay: 0:00:03
      - service: broadlink.send #Sky tasto 1
        data:
          host: ip_del_broadlink
          packet:
            - "codice_infrarosso_relativo_al_tasto_1"
      - service: broadlink.send #Sky tasto 0
        data:
          host: ip_del_broadlink
          packet:
            - "codice_infrarosso_relativo_al_tasto_0"
      - service: broadlink.send #Sky tasto 0
        data:
          host: ip_del_broadlink
          packet:
            - "codice_infrarosso_relativo_al_tasto_0"
      - delay: 0:00:2
      - service: media_player.select_source
        data:
          entity_id: media_player.tv
          source: 'HDMI 1'

The extent script.telegiornale_on_ir_sequence - as a "Script" type - provides for the use of the service "script.turn_on", Which allows forappto execute the sequence of a script (indicated in the evocation phase of the service itself).

Switch&Automation

At this point all we need are two things: a virtual switch and an automation which, when this switch is activated, executes the sequence of a script.

Nb This guide illustrates how to trigger a script via a virtual switch, but it is clear how the illustrated technique can be freely customized to trigger one or more different types of services available on Home Assistant.
SWITCH

Define a virtual switch with Home Assistant it is really child's play: it is in fact sufficient to use the "Input Boolean" component, which is specifically designed for this purpose.

We will then define in configuration an element of this type:

input_boolean:
  telegiornale:
    name: Accensione telegiornale
    initial: off
    icon: mdi:television

After reboot di Home Assistant, this configuration will generate an entity called "input_boolean.telegiornale", Which will come rappshown at the frontend as follows:

Home Assistant - Input Boolean Newscast

AUTOMATION

After creating the switch, its activation will not cause any event, other than the state change of the entity of Home Assistant not collected, however, by anyone.

È proWe will intercept this change of state: the automation we need will trigger probefore the change of status from "off" to "on" of the entity "input_boolean.telegiornale"

automation:
- alias: "Telegiornale"
  trigger:
    platform: state
    entity_id: input_boolean.telegiornale
    to: 'on'
  condition: []
  action:
    service: script.turn_on
    entity_id: script.telegiornale_on_sequence

Once restarted Home Assistant, the activation of the virtual swtich will therefore cause the activation of the sequence indicated in the script, obtaining the purpose.

However, we must perfect everything: what must happen when the switch is deactivated, or from the "on" state to the "off" state. First, we write another sequence (another script) that carries out counter-actions in sequence such as switching off the TV and the Sky box:

script:
  telegiornale_off_sequence:
    alias: "Telegiornale"
    sequence:
      - service: media_player.turn_off
        entity_id: media_player.tv
      - service: media_player.turn_off
        entity_id: media_player.sky

this configuration will generate an entity of type "Script" called script.telegiornale_off_sequence.

Now we modify the first automation in this way:

automation:
- alias: "Telegiornale"
  trigger:
    platform: state
    entity_id: input_boolean.telegiornale
  condition: []
  action:
    service: script.turn_on
    data_template:
      entity_id: >
        {% if trigger.to_state.state == 'on' %}
        script.telegiornale_on_sequence
        {% elif trigger.to_state.state == 'off' %}
        script.telegiornale_off_sequence
        {% endif %}

This automation is triggered by any change of state of input_boolean.telegiornale (therefore both from "on" to "off" and vice versa) and, in the block "action”, Decides what to do, if the sequence of ignition or shutdown.


A further perfectionnamento is the one that allows you to manage the auto-off of the virtual switch in case it was previously activated manually, but then the TV, for example, has been switched off manually. In that case the switch should appunto automatically return to "off", and now we'll see how. We will in fact define an automation that is triggered when the state of the "media_player.tv" entity changes from "on" to "off".

Nb Obviously this automation only works if the entity "Media Player"That rappresents the TV is able to register this operating status, which is dependent on the way it is integrated.

We therefore define such automation:

automation:
- alias: "TV OFF"
  trigger:
    platform: state
    entity_id: media_player.tv
    to: 'off'
  condition:
    condition: state
    entity_id: input_boolean.telegiornale
    state: 'on'
  action:
    service: input_boolean.turn_off
    entity_id: input_boolean.telegiornale

conclusions

As anticipated, this guide it's just a trace to make people understand and become familiar with a very specific concept, or the possibility of triggering sequences via virtual switches. The field of applation is very wide: this approccio can really be used in many different scenarios.

Another example could be that of home automation of an infrared fan heater: this technique can help to abstract the home-based fan heater in a single entity on / off, when in fact its activation and deactivation will correspond to several very specific sequences (obviously varying from model to model and from scenario to scenario).


Home Assistant Official LogoATTENZIONE: remember that there is on our FORUM community an ad hoc section dedicated to Home Assistant, for any doubt, question, information on the specific merit of these components.


Please comment below