Turn TV automatically ON if you select a channel

Hi,

I control my TV 99% of the time via voice assistant and always was annoyed that I need to issue two commands:

  1. Turn TV on
  2. Select the correct channel I want to watch

Therefore I created the rule below to automatically turn on the TV once the channel items receives a command. Once this rule is setup I only need to choose a specific channel and TV will automatically turn on and switch to the correct channel
Tested with OH4.0.4 & LG WebOS binding

Prerequisite:

  • Disable auto-update of power item via meta-data
  • Power item as switch
  • Channel item as string

Steps to do:

  1. Copy rule to your system
  2. Replace power & channel items within the rule triggers with your specific items
  3. Open the blockly rule and replace power & channel items at top with your specific items
  4. Have fun
Rule Code
configuration: {}
triggers:
  - id: "2"
    configuration:
      itemName: EG_Wohnzimmer_Fernseher_AnAus
      state: ON
      previousState: OFF
    type: core.ItemStateChangeTrigger
  - id: "3"
    configuration:
      itemName: EG_Wohnzimmer_Fernseher_Kanal
    type: core.ItemCommandTrigger
conditions: []
actions:
  - inputs: {}
    id: "1"
    configuration:
      blockSource: <xml
        xmlns="https://developers.google.com/blockly/xml"><variables><variable
        id="*!u(+kP%@D46e^V7^.k7">powerItem</variable><variable
        id="64yehp5:`nn-u^d2,92W">channelItem</variable></variables><block
        type="variables_set" id="GL=@n4~cU0e3dI0oO~.q" x="24" y="34"><field
        name="VAR" id="*!u(+kP%@D46e^V7^.k7">powerItem</field><value
        name="VALUE"><block type="oh_getitem" id="ry%C:!Qrod`mCYJ9UOI$"><value
        name="itemName"><shadow type="oh_item"
        id="Mrq$CB9(aEhw`4LoeMV7"><mutation itemName="MyItem"
        itemLabel="MyItem"></mutation><field
        name="itemName">MyItem</field></shadow><block type="oh_item"
        id="O0w!n7}Xf).?L-;Pm~WP"><mutation
        itemName="EG_Wohnzimmer_Fernseher_AnAus" itemLabel="An /
        Aus"></mutation><field
        name="itemName">EG_Wohnzimmer_Fernseher_AnAus</field></block></value></block></value><next><block
        type="variables_set" id="/Qa},%4MfmU4uI[B@pbf"><field name="VAR"
        id="64yehp5:`nn-u^d2,92W">channelItem</field><value name="VALUE"><block
        type="oh_getitem" id="T9tGIIu}]r?!tYLY8?OP"><value
        name="itemName"><shadow type="oh_item"
        id="Mrq$CB9(aEhw`4LoeMV7"><mutation itemName="MyItem"
        itemLabel="MyItem"></mutation><field
        name="itemName">MyItem</field></shadow><block type="oh_item"
        id="9KQ-|Js;Kpjd6N,uMlfM"><mutation
        itemName="EG_Wohnzimmer_Fernseher_Kanal"
        itemLabel="Kanal"></mutation><field
        name="itemName">EG_Wohnzimmer_Fernseher_Kanal</field></block></value></block></value><next><block
        type="controls_if" id="r4up)Z+5m$6Q*vjXfo8q"><mutation
        elseif="1"></mutation><value name="IF0"><block type="logic_compare"
        id="T]SuJZMLmXP~5E_3o-bo"><field name="OP">EQ</field><value
        name="A"><block type="oh_getitem_attribute"
        id="69y#kO$?waWVXITk3)J6"><mutation
        attributeName="Name"></mutation><field
        name="attributeName">Name</field><value name="item"><shadow
        type="oh_getitem" id="`p%!gPZk4xFgt*GU]jBh"><value
        name="itemName"><shadow type="oh_item"
        id="8U-+9u~k;$f,`=dKQ9k."><mutation itemName="MyItem"
        itemLabel="MyItem"></mutation><field
        name="itemName">MyItem</field></shadow></value></shadow><block
        type="variables_get" id="b7A_ZjPmxaY.t=:%XA`V"><field name="VAR"
        id="64yehp5:`nn-u^d2,92W">channelItem</field></block></value></block></value><value
        name="B"><block type="oh_context_info" id="I-kT%M88S=9tU{zrdEKh"><field
        name="contextInfo">itemName</field></block></value></block></value><statement
        name="DO0"><block type="controls_if" id="_WI.{td-}$5n9^mZL?fe"><value
        name="IF0"><block type="logic_compare" id="by9dQMe-j[|{(tKJYPDk"
        inline="false"><field name="OP">EQ</field><value name="A"><block
        type="oh_getitem_attribute" id="R}ptBHDr%al4%iNk)_={"><mutation
        attributeName="State"></mutation><field
        name="attributeName">State</field><value name="item"><shadow
        type="oh_getitem" id="MGw2C+4Z{ViXYwjcoDIL"><value
        name="itemName"><shadow type="oh_item"
        id="b2XlC5xB|+AqAQS[h=w["><mutation itemName="MyItem"
        itemLabel="MyItem"></mutation><field
        name="itemName">MyItem</field></shadow></value></shadow><block
        type="variables_get" id="Y`*Fs^AbPI7}2Gb?$$UN"><field name="VAR"
        id="*!u(+kP%@D46e^V7^.k7">powerItem</field></block></value></block></value><value
        name="B"><block type="text" id="ax7y6h{$}m*+HaHZ{%6["><field
        name="TEXT">OFF</field></block></value></block></value><statement
        name="DO0"><block type="oh_event" id="Y^s`BuemM-b:kyOOeHVq"><field
        name="eventType">sendCommand</field><value name="value"><shadow
        type="text" id="b!I6A;X|sbP|HBQ[ip3y"><field
        name="TEXT">value</field></shadow><block type="text"
        id="mjfQT?Lo1Ey9]L_HCc1x"><field
        name="TEXT">ON</field></block></value><value name="itemName"><shadow
        type="oh_item" id="M`KGO:Ha9/*5Z{$eqGv@"><mutation itemName="MyItem"
        itemLabel="MyItem"></mutation><field
        name="itemName">MyItem</field></shadow><block type="variables_get"
        id="+%j,kTkWg+NWkf,J8eG0"><field name="VAR"
        id="*!u(+kP%@D46e^V7^.k7">powerItem</field></block></value><next><block
        type="oh_store_value" id="c0^~_3Bgpc31,UL_N,[4"><field
        name="cacheType">.private</field><value name="value"><shadow type="text"
        id="[pm`Um_hB8rAenyfL:2c"><field
        name="TEXT">value</field></shadow><block type="oh_context_info"
        id="yYQ0mmi{p7mx{!alZ!Ba"><field
        name="contextInfo">itemCommand</field><field
        name="asType">asString</field></block></value><value name="key"><shadow
        type="text" id="z;jJ0U,cp:mk`l,^M$ig"><field
        name="TEXT">key</field></shadow><block type="text"
        id="z;jJ0U,cp:mk`l,^M$ig"><field
        name="TEXT">channel</field></block></value></block></next></block></statement></block></statement><value
        name="IF1"><block type="logic_compare" id="`7mDe32;%,fVr?k/I_nZ"><field
        name="OP">EQ</field><value name="A"><block type="oh_getitem_attribute"
        id="g;lV5S.?/VR[rl}{;Szn"><mutation
        attributeName="Name"></mutation><field
        name="attributeName">Name</field><value name="item"><shadow
        type="oh_getitem" id="`p%!gPZk4xFgt*GU]jBh"><value
        name="itemName"><shadow type="oh_item"
        id="8U-+9u~k;$f,`=dKQ9k."><mutation itemName="MyItem"
        itemLabel="MyItem"></mutation><field
        name="itemName">MyItem</field></shadow></value></shadow><block
        type="variables_get" id="M9cx;C?u2HU|BR=@MZK["><field name="VAR"
        id="*!u(+kP%@D46e^V7^.k7">powerItem</field></block></value></block></value><value
        name="B"><block type="oh_context_info" id="lpfKX(J-,v7`)AI1epOm"><field
        name="contextInfo">itemName</field></block></value></block></value><statement
        name="DO1"><block type="controls_if" id="SG5!fe.ee3?STwEtV:-t"><value
        name="IF0"><block type="logic_negate" id="hEA9?dzx~]aAyGi7!/JQ"><value
        name="BOOL"><block type="logic_operation" id="~lbN]_LzW::$}V7tj-H2"
        inline="false"><field name="OP">OR</field><value name="A"><block
        type="logic_compare" id="?GTv?oz]ZJo}g9BI+a[@"><field
        name="OP">EQ</field><value name="A"><block type="text"
        id="P=VwYQu#p(mmqzB`F+v!"><field
        name="TEXT">undefined</field></block></value><value name="B"><block
        type="oh_get_value" id="|`g)xqXYB/oZ``ZV/4S_"><field
        name="cacheType">.private</field><value name="key"><shadow type="text"
        id="cg]bIXZo$?%l./hHSHjb"><field name="TEXT">key</field></shadow><block
        type="text" id="cg]bIXZo$?%l./hHSHjb"><field
        name="TEXT">channel</field></block></value></block></value></block></value><value
        name="B"><block type="logic_compare" id=",a,6GUv*(L8Q`HVnSh98"><field
        name="OP">EQ</field><value name="A"><block type="text"
        id="EHuo#*tVg{~yGk6[Q0ye"><field
        name="TEXT"></field></block></value><value name="B"><block
        type="oh_get_value" id="-}Id|ekTBH$o?-Y,xkDs"><field
        name="cacheType">.private</field><value name="key"><shadow type="text"
        id=".i%.kfvB!`xI0Z%7oG-Y"><field name="TEXT">key</field></shadow><block
        type="text" id="78`T,?sS`5Wm52$l#{1."><field
        name="TEXT">channel</field></block></value></block></value></block></value></block></value></block></value><statement
        name="DO0"><block type="oh_event" id="n2B[%#!~$W3P`g|}8_`s"><field
        name="eventType">sendCommand</field><value name="value"><shadow
        type="text" id="b!I6A;X|sbP|HBQ[ip3y"><field
        name="TEXT">value</field></shadow><block type="oh_get_value"
        id="rJ3c6v_]VCU%za)0I/]1"><field name="cacheType">.private</field><value
        name="key"><shadow type="text" id="q-IA:Lj2{vMqZ?W(aYAj"><field
        name="TEXT">key</field></shadow><block type="text"
        id="eKR)q=iMWn~TFp)}){R7"><field
        name="TEXT">channel</field></block></value></block></value><value
        name="itemName"><shadow type="oh_item"
        id="M`KGO:Ha9/*5Z{$eqGv@"><mutation itemName="MyItem"
        itemLabel="MyItem"></mutation><field
        name="itemName">MyItem</field></shadow><block type="variables_get"
        id="+PJI!V(%c;;}|RfzfR(L"><field name="VAR"
        id="64yehp5:`nn-u^d2,92W">channelItem</field></block></value><next><block
        type="oh_store_value" id="ZMOp[1Ugjr,5E/O]S7}o"><field
        name="cacheType">.private</field><value name="value"><shadow type="text"
        id="[pm`Um_hB8rAenyfL:2c"><field
        name="TEXT">value</field></shadow><block type="text"
        id="[pm`Um_hB8rAenyfL:2c"><field
        name="TEXT"></field></block></value><value name="key"><shadow
        type="text" id="EEkG,flw_]B@)GjSXh%d"><field
        name="TEXT">key</field></shadow><block type="text"
        id="RuK(..}iBRE4?Ao4O0o2"><field
        name="TEXT">channel</field></block></value></block></next></block></statement></block></statement></block></next></block></next></block></xml>
      type: application/javascript
      script: >
        var powerItem, channelItem;



        powerItem = items.getItem('EG_Wohnzimmer_Fernseher_AnAus');

        channelItem = items.getItem('EG_Wohnzimmer_Fernseher_Kanal');

        if (channelItem.name == event.itemName) {
          if (powerItem.state == 'OFF') {
            powerItem.sendCommand('ON');
            cache.private.put('channel', event.itemCommand.toString());
          }
        } else if (powerItem.name == event.itemName) {
          if (!('undefined' == (cache.private.get('channel')) || '' == (cache.private.get('channel')))) {
            channelItem.sendCommand((cache.private.get('channel')));
            cache.private.put('channel', '');
          }
        }
    type: script.ScriptAction


2 Likes