Navigation

    Logo
    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unread
    • Categories
    • Unreplied
    • Popular
    • GitHub
    • Docu
    • Hilfe
    1. Home
    2. English
    3. Scripting / Logic
    4. Blockly
    5. Turn on switch at certain time when other switch turned on

    NEWS

    • [erledigt] 15. 05. Wartungsarbeiten am ioBroker Forum

    • ioBroker goes Matter ... Matter Adapter in Stable

    • Monatsrückblick - April 2025

    Turn on switch at certain time when other switch turned on

    This topic has been deleted. Only users with topic management privileges can see it.
    • S
      sekino last edited by sekino

      Hi, I want to make script that will turn on SWITCH2 when SWITCH1 is already turned on and is between SUNSET or DUSK and 23:59 or until SUNRISE next day.
      Then turn off SWITCH2 5 minutes later after SWITCH1 is turned off.
      I created this but it's not working:
      switch.png

      Asgothian 2 Replies Last reply Reply Quote 0
      • Asgothian
        Asgothian Developer @sekino last edited by Asgothian

        @sekino
        You need to swap the Trigger / if/else logic

        Build it like this:

        Trigger on switch, has been changed
        Inside the trigger the Block If time is in range use the "write" block to set the state, and add a "timeout 5 minutes - disable switch 2" block.

        If you take a close look at the Blockly system section where the triggers are located, you will find a block which just shows "object id". You can grab that one and use inside the "write" block, since you can change it to "state value", which is the value of the state which fired the trigger.

        I can build a sample blockly when i get home later.
        A.

        1 Reply Last reply Reply Quote 0
        • Asgothian
          Asgothian Developer @sekino last edited by

          @sekino
          OK.. seems i didn't read all you wanted to do correctly earlier. When trying to build the blockly, i ran into an issue which is unclear in your description:

          What i read from above:

          • If sunset comes AND switch 1 is already on, you want switch 2 to be turned on, and turned off after 5 minutes.
          • If switch 1 is turned on during night time, you want the same to happen ?

          Here is a blockly set which does that:
          Screen Shot 2020-09-03 at 21.39.55 .png

          Code to import behind the spoiler

          <xml xmlns="https://developers.google.com/blockly/xml">
            <variables>
              <variable id="e.dW%KumhN7|TQ#:?l@l">Sw1IsOn</variable>
              <variable id="gu+_Mq3HBCLql`T,T2FP">Sw2IsOnAtNight</variable>
            </variables>
            <block type="comment" id="`ubXK{/}C4yhsr$oP@6#" x="-612" y="238">
              <field name="COMMENT">Trigger on switch 1</field>
              <next>
                <block type="on" id="~yGu`~m4T4iavoP1^s-G">
                  <field name="OID">Object ID</field>
                  <field name="CONDITION">gt</field>
                  <field name="ACK_CONDITION"></field>
                  <statement name="STATEMENT">
                    <block type="controls_if" id="`pWfTrW=#x96R#_?`ux8">
                      <mutation else="1"></mutation>
                      <value name="IF0">
                        <block type="logic_operation" id="BcA;[3StEi.:gIP)k]-c">
                          <field name="OP">OR</field>
                          <value name="A">
                            <block type="time_compare_ex" id="Mhhd5aw_tuKtfXt;!TWi">
                              <mutation xmlns="http://www.w3.org/1999/xhtml" end_time="false" actual_time="true"></mutation>
                              <field name="USE_ACTUAL_TIME">TRUE</field>
                              <field name="OPTION">&lt;</field>
                              <value name="START_TIME">
                                <shadow type="text" id="dvCCEVGKGuusC}gtM$R3">
                                  <field name="TEXT">12:00</field>
                                </shadow>
                                <block type="time_astro" id=";oy#)ZgB8zl6y^XyNGPU">
                                  <field name="TYPE">sunrise</field>
                                  <field name="OFFSET">0</field>
                                </block>
                              </value>
                            </block>
                          </value>
                          <value name="B">
                            <block type="time_compare_ex" id="XoKL5|C4)W}HrFU/^~`1">
                              <mutation xmlns="http://www.w3.org/1999/xhtml" end_time="true" actual_time="true"></mutation>
                              <field name="USE_ACTUAL_TIME">TRUE</field>
                              <field name="OPTION">between</field>
                              <value name="START_TIME">
                                <shadow type="text" id="jk:D]lfai@_ZTxK}|%h8">
                                  <field name="TEXT">12:00</field>
                                </shadow>
                                <block type="time_astro" id="vA+{~GPbciV7|/zAiFc^">
                                  <field name="TYPE">dusk</field>
                                  <field name="OFFSET">0</field>
                                </block>
                              </value>
                              <value name="END_TIME">
                                <shadow type="text" id="8HvVQ?7qV#5YqNOtI2u0">
                                  <field name="TEXT">23:59</field>
                                </shadow>
                              </value>
                            </block>
                          </value>
                        </block>
                      </value>
                      <statement name="DO0">
                        <block type="comment" id="w%H7/XN9s0(,(LFOv|{a">
                          <field name="COMMENT">turn switch 2 on</field>
                          <next>
                            <block type="control" id="6D:j{dSGp`K;rXuf.qoq">
                              <mutation xmlns="http://www.w3.org/1999/xhtml" delay_input="false"></mutation>
                              <field name="OID">Object ID</field>
                              <field name="WITH_DELAY">FALSE</field>
                              <value name="VALUE">
                                <block type="logic_boolean" id="m2t5//Kz~O$^/JBIca3`">
                                  <field name="BOOL">TRUE</field>
                                </block>
                              </value>
                            </block>
                          </next>
                        </block>
                      </statement>
                      <statement name="ELSE">
                        <block type="variables_set" id="vS.c4vA*|A*-FMGR:5|E">
                          <field name="VAR" id="e.dW%KumhN7|TQ#:?l@l">Sw1IsOn</field>
                          <value name="VALUE">
                            <block type="on_source" id="FHghmyvbZfa6vWz/2e*$">
                              <field name="ATTR">state.val</field>
                            </block>
                          </value>
                        </block>
                      </statement>
                    </block>
                  </statement>
                  <next>
                    <block type="comment" id="yK!gJZa`Y1{mXjdB!~Vz">
                      <field name="COMMENT">Trigger on switch 1</field>
                      <next>
                        <block type="on" id="u96s3Fjhu-mS:PnZY@_:">
                          <field name="OID">Object ID</field>
                          <field name="CONDITION">lt</field>
                          <field name="ACK_CONDITION"></field>
                          <statement name="STATEMENT">
                            <block type="variables_set" id="[dt5Ei;cd+[OOeV]ot66">
                              <field name="VAR" id="e.dW%KumhN7|TQ#:?l@l">Sw1IsOn</field>
                              <value name="VALUE">
                                <block type="on_source" id="ph|PJ9[f9*t0_f|!7/s7">
                                  <field name="ATTR">state.val</field>
                                </block>
                              </value>
                              <next>
                                <block type="controls_if" id="S`sR]mZ.x}n{,6n/hcE6">
                                  <value name="IF0">
                                    <block type="variables_get" id="Dst}xp`EVVA(`:(%#g96">
                                      <field name="VAR" id="gu+_Mq3HBCLql`T,T2FP">Sw2IsOnAtNight</field>
                                    </block>
                                  </value>
                                  <statement name="DO0">
                                    <block type="comment" id="U@RC-lS-[Z:!dx_{J](Z">
                                      <field name="COMMENT">turn switch 2 off</field>
                                      <next>
                                        <block type="control" id="WD~POu@?;-h=taTZu8rj">
                                          <mutation xmlns="http://www.w3.org/1999/xhtml" delay_input="true"></mutation>
                                          <field name="OID">Object ID</field>
                                          <field name="WITH_DELAY">TRUE</field>
                                          <field name="DELAY_MS">5</field>
                                          <field name="UNIT">min</field>
                                          <field name="CLEAR_RUNNING">TRUE</field>
                                          <value name="VALUE">
                                            <block type="logic_boolean" id="U$X6cTnb6IOc@#@[F6sq">
                                              <field name="BOOL">FALSE</field>
                                            </block>
                                          </value>
                                        </block>
                                      </next>
                                    </block>
                                  </statement>
                                </block>
                              </next>
                            </block>
                          </statement>
                          <next>
                            <block type="comment" id="pUv[FY5CZRZskW((/_{S">
                              <field name="COMMENT">Trigger on switch 1</field>
                              <next>
                                <block type="on" id="5Fc[9hiWqSVUiO{Ujx0$">
                                  <field name="OID">Object ID</field>
                                  <field name="CONDITION">ne</field>
                                  <field name="ACK_CONDITION"></field>
                                  <statement name="STATEMENT">
                                    <block type="variables_set" id="y^u:v,09bwMC;B5j3_Y#">
                                      <field name="VAR" id="gu+_Mq3HBCLql`T,T2FP">Sw2IsOnAtNight</field>
                                      <value name="VALUE">
                                        <block type="on_source" id="49{yMXapE}A)^dVBX.gR">
                                          <field name="ATTR">state.val</field>
                                        </block>
                                      </value>
                                    </block>
                                  </statement>
                                  <next>
                                    <block type="comment" id="J(^TL]9aCFhx^K2@dCQ[">
                                      <field name="COMMENT">Fix switch 2 at sunset</field>
                                      <next>
                                        <block type="astro" id="l}tX?IzdXYN~a(JW}%.e">
                                          <field name="TYPE">sunset</field>
                                          <field name="OFFSET">0</field>
                                          <statement name="STATEMENT">
                                            <block type="controls_if" id="Hn:ug~:o$xcB4.:`Ewb]">
                                              <value name="IF0">
                                                <block type="variables_get" id="/5}J.tDB0~AeD^Z8-pDt">
                                                  <field name="VAR" id="e.dW%KumhN7|TQ#:?l@l">Sw1IsOn</field>
                                                </block>
                                              </value>
                                              <statement name="DO0">
                                                <block type="comment" id="k%iPli4lT?zAO~K2.n15">
                                                  <field name="COMMENT">turn switch 2 on</field>
                                                  <next>
                                                    <block type="control" id=":%4U`;(=^;#(A#}q]7Nd">
                                                      <mutation xmlns="http://www.w3.org/1999/xhtml" delay_input="false"></mutation>
                                                      <field name="OID">Object ID</field>
                                                      <field name="WITH_DELAY">FALSE</field>
                                                      <value name="VALUE">
                                                        <block type="logic_boolean" id="M]j4Z[mHt_dk}Or2RSE`">
                                                          <field name="BOOL">TRUE</field>
                                                        </block>
                                                      </value>
                                                    </block>
                                                  </next>
                                                </block>
                                              </statement>
                                            </block>
                                          </statement>
                                        </block>
                                      </next>
                                    </block>
                                  </next>
                                </block>
                              </next>
                            </block>
                          </next>
                        </block>
                      </next>
                    </block>
                  </next>
                </block>
              </next>
            </block>
          </xml>
          

          S 1 Reply Last reply Reply Quote 0
          • S
            sekino @Asgothian last edited by

            @Asgothian I will make it in points:

            • If sunset comes AND switch 1 is already on, I want switch 2 to be turned on
            • If switch 1 is turned on during night time, I want the same to happen (immediatelly)
            • turn off SWITCH2 > 5 minutes later after SWITCH1 is turned off.
            Asgothian 1 Reply Last reply Reply Quote 0
            • Asgothian
              Asgothian Developer @sekino last edited by

              @sekino The script is already in the post above. It should do just that.

              A.

              S 1 Reply Last reply Reply Quote 0
              • S
                sekino @Asgothian last edited by

                @Asgothian I will try and let you know if works, for now I would like to ask what logic is behind : ObjectID is greater/less than last ... if values are true or false ???

                should I keep variables Sw1IsOn and Sw1IsOnAtNight ? Or should I changed them for states of my Sw1?

                Homoran Asgothian 2 Replies Last reply Reply Quote 0
                • Homoran
                  Homoran Global Moderator Administrators @sekino last edited by

                  @sekino sagte in Turn on switch at certain time when other switch turned on:

                  Or should I changed them for states of my Sw1?

                  no, You should online exchange objectID with your states

                  1 Reply Last reply Reply Quote 0
                  • Asgothian
                    Asgothian Developer @sekino last edited by

                    @sekino sagte in Turn on switch at certain time when other switch turned on:

                    @Asgothian I will try and let you know if works, for now I would like to ask what logic is behind : ObjectID is greater/less than last ... if values are true or false ???

                    should I keep variables Sw1IsOn and Sw1IsOnAtNight ? Or should I changed them for states of my Sw1?

                    as @Homoran said - only replace the object id's.

                    Triggering for greater than and lesser than enables you to not have to check true or false value later.

                    In JS, true > false, so a trigger on "greater than before" will only activate when the object value changes from false to true.

                    A.

                    S 1 Reply Last reply Reply Quote 0
                    • S
                      sekino @Asgothian last edited by

                      @Asgothian @Homoran Thank you all! Working great!

                      1 Reply Last reply Reply Quote 0
                      • First post
                        Last post

                      Support us

                      ioBroker
                      Community Adapters
                      Donate

                      664
                      Online

                      31.6k
                      Users

                      79.5k
                      Topics

                      1.3m
                      Posts

                      3
                      9
                      1347
                      Loading More Posts
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes
                      Reply
                      • Reply as topic
                      Log in to reply
                      Community
                      Impressum | Datenschutz-Bestimmungen | Nutzungsbedingungen
                      The ioBroker Community 2014-2023
                      logo