Menu

example for LUA script

Help
Ronny H
2009-06-10
2012-12-14
  • Ronny H

    Ronny H - 2009-06-10

    Hello,

    i want to know an example of lua script. The wiki page http://linknx.wiki.sourceforge.net/Lua+Scripting does not really show me a good example. In the sample configuration doesnt exists a lua part. Can anyone show me a example to use this? I have a few questions:

    Where must be the <condition type="script"> or the <action type="script"> part? In objects or rules? How must i use the return variable?

     
    • jef2000

      jef2000 - 2009-06-10

      Hi,

      I'm still not sure the way I implemented scripting is the right one, so I don't really have good examples. You can use the conditions and actions in rules,just like any other condition or action. For condition, your LUA code must return a boolean value that will tell if the condition is true or false.

      One problem I already detected with scripts, is that a script can't trigger the rule. It should be possible to set a list of objects which trigger the rule evaluation if their value change.

      In the example below, I need to put a timer in the condition to force its evaluation every 30 seconds. If temperature is below set-point, the action script will compute a new value for heat_room1. If temp is above set-point, the value is set to zero :

      <rule>
        <condition type="and">
          <condition type="timer" trigger="true">
            <every>30s</every>
          </condition>
          <condition type="script">
          return tonumber(obj("setpoint_room1")) > tonumber(obj("temp_room1"));
          </condition>
        </condition>
        <actionlist>
          <action type="script">
            delta = obj("setpoint_room1")-obj("temp_room1");
            value = math.max(100*delta, 255);
            print ("value = ", value);
            set("heat_room1", value);
          </action>
        </actionlist>
        <actionlist type="on-false">
          <action type="set-value" id="heat_room1" value="0"/>
        </actionlist>
      </rule>

      Kr,

      Jean-François

       
    • Erik

      Erik - 2009-06-19

      Hi hronny,

      my first LUA script for Linknx works fine:

              <rule id="rule_heating_mode_comfort">
                     <condition type="object" id="heating_mode_comfort" trigger="true" value="on" />
                  <actionlist>
                      <action type="script">
                          value = obj("heating_mode_comfort");
                          set("heating_mode_comfort_living", value);
                          set("heating_mode_comfort_office", value);
                      </action>
                  </actionlist>
                  <actionlist type="on-false">
                      <action type="script">
                          value = obj("heating_mode_comfort");
                          set("heating_mode_comfort_living", value);
                          set("heating_mode_comfort_office", value);
                      </action>
                  </actionlist>
              </rule>

      Note, that the repetition of the script contained in <actionlist> in <actionlist type="on-false"> is just because Linknx does not execute the actions, if a condition results in the same results as a previous evaluation.

      May be, a future version of Linknx can provide a pure trigger functionality, where an actionlist is executed each time a telegram of a given goup address is received, regardless of the data submitted.

      Example:

              <rule id="rule_heating_mode_comfort">
                     <condition type="object" id="heating_mode_comfort" trigger="true"/>
                  <actionlist type="always">
                      <action type="script">
                          value = obj("heating_mode_comfort");
                          set("heating_mode_comfort_living", value);
                          set("heating_mode_comfort_office", value);
                      </action>
                  </actionlist>
              </rule>

      The same result could be achieved without scripting, if Linknx would support a "condition" value for the attribute "value":

      Example:
              <rule id="rule_heating_mode_comfort">
                  <condition type="object" id="heating_mode_comfort" trigger="true" />
                  <actionlist type="always">
                      <action type="set-value" id="heating_mode_comfort_living" value="condition" />
                      <action type="set-value" id="heating_mode_comfort_office" value="condition" />
                  </actionlist>
              </rule>

      I am glad to read your response,
      Erik

       
    • jef2000

      jef2000 - 2009-06-24

      Hi,

      Yes, I plan to add the possibility to configure "stateless" objects that will not "filter" telegrams when they have the same value as the actual object state . I also need to find a way to allow "stateless" rules that will execute one of the action lists every time the rule is evaluated (not only when the result of eveluation is diffferent from the previous evaluation)

      But for the problem you describe, you don't really need LUA scripts. You can use the "copy-value" action:

      <rule id="rule_heating_mode_comfort">
      <condition type="object" id="heating_mode_comfort" trigger="true" value="on" />
      <actionlist>
      <action type="copy-value" from="heating_mode_comfort" to="heating_mode_comfort_living"/>
      <action type="copy-value" from="heating_mode_comfort" to="heating_mode_comfort_office"/>
      </actionlist>
      <actionlist type="on-false">
      <action type="copy-value" from="heating_mode_comfort" to="heating_mode_comfort_living"/>
      <action type="copy-value" from="heating_mode_comfort" to="heating_mode_comfort_office"/>
      </actionlist>
      </rule>

      Regards,

      Jean-François

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.