<?xml version="1.0" encoding="utf-8"?>
<Templates format="2" protocolVersion="83">
  <Template id="6f1c2b7e-3d4a-4c8e-9b2f-5a7d1e3c9f04" revision="1.0">
    <RevisionHistory>
      <Revision id="1.0">Initial version</Revision>
    </RevisionHistory>
    <Name>Shelly 1 Gen3</Name>
    <CcuModel>PacketParserCCU</CcuModel>
    <Producer>Shelly</Producer>
    <Model>Shelly 1 Gen3</Model>
    <Description/>
    <Category>6</Category>
    <ImportParameters>
      <Parameter>
        <Id>IpAddress</Id>
        <Name>${ipAddress}</Name>
        <Value>192.168.0.1</Value>
      </Parameter>
    </ImportParameters>
    <Module>
      <Name>Shelly 1 Gen3</Name>
      <Id>-1</Id>
      <Model>PacketParserModuleHttp</Model>
      <DeviceProperties>
        <IpAddress>$[IpAddress]</IpAddress>
        <Port>80</Port>
        <UseHttps>False</UseHttps>
        <HttpAuthentification>0</HttpAuthentification>
        <InternalPollInterval>15000</InternalPollInterval>
        <CustomVariables>[{&quot;Name&quot;:&quot;SysStatus&quot;,&quot;ValueTypeId&quot;:2,&quot;NumericValue&quot;:0,&quot;StringValue&quot;:&quot;&quot;},{&quot;Name&quot;:&quot;WiFiStatus&quot;,&quot;ValueTypeId&quot;:2,&quot;NumericValue&quot;:0,&quot;StringValue&quot;:&quot;&quot;},{&quot;Name&quot;:&quot;SwitchStatus&quot;,&quot;ValueTypeId&quot;:2,&quot;NumericValue&quot;:0,&quot;StringValue&quot;:&quot;&quot;}]</CustomVariables>
        <ReadScriptPacketParser>VAR r := SENDHTTPREQUEST(&quot;/rpc/Sys.GetStatus&quot;);
IF r.IsSuccess
    SysStatus := r.Content;
ELSE
    IF r.StatusCode = 401
        ADDERROR(401, &quot;Shelly asks for a password (HTTP 401). Turn off password protection in the Shelly web interface, TapHome does not support HTTP Digest.&quot;);
    ELSE
        ADDERROR(1, &quot;Shelly answered HTTP &quot; + TOSTRING(r.StatusCode));
    END
    RETURN(0);
END
r := SENDHTTPREQUEST(&quot;/rpc/WiFi.GetStatus&quot;);
IF r.IsSuccess
    WiFiStatus := r.Content;
END
r := SENDHTTPREQUEST(&quot;/rpc/Switch.GetStatus?id=0&quot;);
IF r.IsSuccess
    SwitchStatus := r.Content;
END
IF ! ISNULL(PARSEJSON(SysStatus, &quot;available_updates.stable&quot;, true))
    ADDWARNING(2, &quot;New firmware update is available&quot;);
END
IF PARSEJSON(SysStatus, &quot;restart_required&quot;, true)
    ADDWARNING(3, &quot;Restart required&quot;);
END</ReadScriptPacketParser>
        <ServiceAttributesScriptsPacketParser>[{&quot;AttributeDefinition&quot;: {&quot;Name&quot;: &quot;Wi-Fi&quot;}, &quot;ReadFormula&quot;: &quot;PARSEJSON(WiFiStatus, \&quot;ssid\&quot;, true)&quot;}, {&quot;AttributeDefinition&quot;: {&quot;Name&quot;: &quot;${ipAddress}&quot;}, &quot;ReadFormula&quot;: &quot;PARSEJSON(WiFiStatus, \&quot;sta_ip\&quot;, true)&quot;}, {&quot;AttributeDefinition&quot;: {&quot;Name&quot;: &quot;${xml_signal_strength} [dB]&quot;}, &quot;ReadFormula&quot;: &quot;PARSEJSON(WiFiStatus, \&quot;rssi\&quot;, true)&quot;}, {&quot;AttributeDefinition&quot;: {&quot;Name&quot;: &quot;MAC&quot;}, &quot;ReadFormula&quot;: &quot;PARSEJSON(SysStatus, \&quot;mac\&quot;, true)&quot;}, {&quot;AttributeDefinition&quot;: {&quot;Name&quot;: &quot;${temperature} [°C]&quot;}, &quot;ReadFormula&quot;: &quot;PARSEJSON(SwitchStatus, \&quot;temperature.tC\&quot;, true)&quot;}]</ServiceAttributesScriptsPacketParser>
        <ServiceActionsScriptsPacketParser>[{&quot;ActionDefinition&quot;: {&quot;Name&quot;: &quot;${xml_update_fw}&quot;, &quot;NumericParameters&quot;: [], &quot;BoolParameters&quot;: [], &quot;EnumParameters&quot;: []}, &quot;Script&quot;: &quot;VAR r := SENDHTTPREQUEST(\&quot;/rpc/Shelly.Update?stage=\\\&quot;stable\\\&quot;\&quot;);\nIF r.IsSuccess\n    RETURN(\&quot;OK\&quot;);\nELSE\n    RETURN(\&quot;Error \&quot; + TOSTRING(r.StatusCode));\nEND&quot;}, {&quot;ActionDefinition&quot;: {&quot;Name&quot;: &quot;Reboot&quot;, &quot;NumericParameters&quot;: [], &quot;BoolParameters&quot;: [], &quot;EnumParameters&quot;: []}, &quot;Script&quot;: &quot;VAR r := SENDHTTPREQUEST(\&quot;/rpc/Shelly.Reboot\&quot;);\nIF r.IsSuccess\n    RETURN(\&quot;OK\&quot;);\nELSE\n    RETURN(\&quot;Error \&quot; + TOSTRING(r.StatusCode));\nEND&quot;}]</ServiceActionsScriptsPacketParser>
      </DeviceProperties>
      <Devices>
        <Device>
          <Name>Relay</Name>
          <Id>-2</Id>
          <Model>PacketParserSwitch</Model>
          <DeviceProperties>
            <DeviceType>0</DeviceType>
            <InternalPollInterval>2500</InternalPollInterval>
            <StartupValueSource>ReadFromDevice</StartupValueSource>
            <NegateState>False</NegateState>
            <ShouldDoPeriodicWrite>False</ShouldDoPeriodicWrite>
            <OnStateIconId>60</OnStateIconId>
            <OffStateIconId>61</OffStateIconId>
            <OnStateName>${general_on}</OnStateName>
            <OffStateName>${general_off}</OffStateName>
            <ReadSwitchStateScriptPacketParser>VAR r := SENDHTTPREQUEST(&quot;/rpc/Switch.GetStatus?id=0&quot;);
IF r.IsSuccess
    RETURN(PARSEJSON(r.Content, &quot;$.output&quot;));
END
IF r.StatusCode = 401
    ADDERROR(401, &quot;Shelly asks for a password (HTTP 401). Turn off password protection in the Shelly web interface, TapHome does not support HTTP Digest.&quot;);
ELSE
    ADDERROR(1, &quot;Shelly answered HTTP &quot; + TOSTRING(r.StatusCode));
END
RETURN(NaN);</ReadSwitchStateScriptPacketParser>
            <WriteSwitchStateScriptPacketParser>SENDHTTPREQUEST(&quot;/rpc/Switch.Set?id=0&amp;on=&quot; + IF(St, &quot;true&quot;, &quot;false&quot;));</WriteSwitchStateScriptPacketParser>
          </DeviceProperties>
        </Device>
        <Device>
          <Name>SW input</Name>
          <Id>-3</Id>
          <Model>PacketParserReedContact</Model>
          <DeviceProperties>
            <DeviceType>0</DeviceType>
            <InternalPollInterval>2500</InternalPollInterval>
            <OnStateIconId>91</OnStateIconId>
            <OffStateIconId>92</OffStateIconId>
            <OnStateName>${general_on}</OnStateName>
            <OffStateName>${general_off}</OffStateName>
            <ReadStateScriptPacketParser>VAR r := SENDHTTPREQUEST(&quot;/rpc/Input.GetConfig?id=0&quot;);
IF r.IsSuccess
    IF PARSEJSON(r.Content, &quot;$.type&quot;, true) != &quot;switch&quot;
        ADDERROR(2, &quot;SW input is not of type switch in Shelly (Input settings: Input mode = Switch)&quot;);
        RETURN(NaN);
    END
ELSE
    IF r.StatusCode = 401
        ADDERROR(401, &quot;Shelly asks for a password (HTTP 401). Turn off password protection in the Shelly web interface, TapHome does not support HTTP Digest.&quot;);
    ELSE
        ADDERROR(1, &quot;Shelly answered HTTP &quot; + TOSTRING(r.StatusCode));
    END
    RETURN(NaN);
END
r := SENDHTTPREQUEST(&quot;/rpc/Input.GetStatus?id=0&quot;);
IF r.IsSuccess
    RETURN(PARSEJSON(r.Content, &quot;$.state&quot;));
END
IF r.StatusCode = 401
    ADDERROR(401, &quot;Shelly asks for a password (HTTP 401). Turn off password protection in the Shelly web interface, TapHome does not support HTTP Digest.&quot;);
ELSE
    ADDERROR(1, &quot;Shelly answered HTTP &quot; + TOSTRING(r.StatusCode));
END
RETURN(NaN);</ReadStateScriptPacketParser>
          </DeviceProperties>
        </Device>
        <Device>
          <Name>Garage door</Name>
          <Id>-4</Id>
          <Model>PacketParserSwitch</Model>
          <DeviceProperties>
            <DeviceType>0</DeviceType>
            <InternalPollInterval>2500</InternalPollInterval>
            <StartupValueSource>ReadFromDevice</StartupValueSource>
            <NegateState>False</NegateState>
            <ShouldDoPeriodicWrite>False</ShouldDoPeriodicWrite>
            <OnStateIconId>99</OnStateIconId>
            <OffStateIconId>100</OffStateIconId>
            <OnStateName>Open</OnStateName>
            <OffStateName>Closed</OffStateName>
            <CustomVariables>[{&quot;Name&quot;:&quot;Invert&quot;,&quot;ValueTypeId&quot;:1,&quot;NumericValue&quot;:0},{&quot;Name&quot;:&quot;PulseSeconds&quot;,&quot;ValueTypeId&quot;:1,&quot;NumericValue&quot;:1}]</CustomVariables>
            <ReadSwitchStateScriptPacketParser>VAR r := SENDHTTPREQUEST(&quot;/rpc/Input.GetStatus?id=0&quot;);
IF r.IsSuccess = false
    IF r.StatusCode = 401
        ADDERROR(401, &quot;Shelly asks for a password (HTTP 401). Turn off password protection in the Shelly web interface, TapHome does not support HTTP Digest.&quot;);
    ELSE
        ADDERROR(1, &quot;Shelly answered HTTP &quot; + TOSTRING(r.StatusCode));
    END
    RETURN(NaN);
END
VAR s := PARSEJSON(r.Content, &quot;$.state&quot;, true);
IF ISNULL(s)
    ADDERROR(2, &quot;SW input has no state - set it to type switch in Shelly&quot;);
    RETURN(NaN);
END
IF Invert = 1
    RETURN(s);
END
RETURN(! s);</ReadSwitchStateScriptPacketParser>
            <WriteSwitchStateScriptPacketParser>VAR r := SENDHTTPREQUEST(&quot;/rpc/Input.GetStatus?id=0&quot;);
IF r.IsSuccess = false
    RETURN(0);
END
VAR open := IF(PARSEJSON(r.Content, &quot;$.state&quot;, true), 0, 1);
IF Invert = 1
    open := 1 - open;
END
IF IF(St, 1, 0) != open
    SENDHTTPREQUEST(&quot;/rpc/Switch.Set?id=0&amp;on=true&amp;toggle_after=&quot; + TOSTRING(PulseSeconds));
END</WriteSwitchStateScriptPacketParser>
            <ReadScriptPacketParser>VAR r := SENDHTTPREQUEST(&quot;/rpc/Switch.GetConfig?id=0&quot;);
IF r.IsSuccess
    IF PARSEJSON(r.Content, &quot;$.in_mode&quot;, true) != &quot;detached&quot;
        ADDWARNING(3, &quot;Shelly input mode is not Detached - the reed contact on SW switches the relay by itself. Set Input/Output settings to Detached.&quot;);
    END
END</ReadScriptPacketParser>
          </DeviceProperties>
        </Device>
      </Devices>
    </Module>
  </Template>
</Templates>
