Custom System Variables

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
garnold
Normal user
Posts: 93
Joined: 17 Jun 2019, 03:59

Custom System Variables

#1 Post by garnold » 05 Feb 2021, 21:36

Is it possible to create my own customer system variables? I would like to be able to describe the sensor in more detail.

%building%

%room%

%door%

I was also wondering if we can get access to the lat and long via variables. I did check here to see if they are available but do not seem to be.
https://espeasy.readthedocs.io/en/lates ... -variables

TD-er
Core team member
Posts: 8643
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Custom System Variables

#2 Post by TD-er » 05 Feb 2021, 22:42

Lat/lon is indeed not present as a variable (nobody asked for them before)
If you're using a GPS, then they are of course available as task variables.

Currently the use of string types is very limited, so I don't really know how useful it will be to store those "location metadata" like variables.
For sure we don't have the option right now to create your own variables, but I can imagine it would make sense to have the kind of information you mention present as some fields that can be stored and later referenced via variables.
It is also the kind of information you also see in SNMP like configuration for devices.

martinus
Normal user
Posts: 129
Joined: 15 Feb 2020, 16:57

Re: Custom System Variables

#3 Post by martinus » 06 Feb 2021, 11:55

I'm using a custom plugin to support the use of named string variables with this command:

LetString <varname>,<string expression>

This is convenient in rules where url paths are used multiple times. First rule at boot sets a var like "URL" to "http://192.168.0.123/xxx/aaaa/xxx"
Then other rules can do things like "sendtohttp %URL%=123"

I also added string manipulation commands to do fancy stuff with incoming topic values (json parsing from custom payloads).

User avatar
Ath
Normal user
Posts: 3416
Joined: 10 Jun 2018, 12:06
Location: NL

Re: Custom System Variables

#4 Post by Ath » 06 Feb 2021, 13:30

martinus wrote: 06 Feb 2021, 11:55 I'm using a custom plugin to support the use of named string variables with this command:

LetString <varname>,<string expression>

This is convenient in rules where url paths are used multiple times. First rule at boot sets a var like "URL" to "http://192.168.0.123/xxx/aaaa/xxx"
Then other rules can do things like "sendtohttp %URL%=123"

I also added string manipulation commands to do fancy stuff with incoming topic values (json parsing from custom payloads).
Very interesting! Would you care to share/create a PR with that plugin?

I have an open PR (#3424) that extends P037 - MQTT Import plugin with JSON parsing and filtering (have some rework to do there as it uses too much memory/re-allocates strings too often), so I'm interested in having a look at your code. ;)
/Ton (PayPal.me)

martinus
Normal user
Posts: 129
Joined: 15 Feb 2020, 16:57

Re: Custom System Variables

#5 Post by martinus » 06 Feb 2021, 16:54

The plugin is quite a huge plugin that holds most of my customization's. But the string handling parts basically comes down to this: (i fear that this is quite a poor implementation to accomplish string var support, so it's not PR worthy)

I think if Gijs wants to support string variables, he's able to do a much better coding job.

Code: Select all

Plugin declare section
=====================
// String variable support, create a pointer to struct
struct P201_svarStruct
{
  String *Name;
  String *Value;
};
struct P201_svarStruct *P201_sUserVar;


Plugin init section
=====================
// hook custom code into core parsing routine

  parseTemplate_CallBack_ptr = &P201_parseTemplate;


Plugin write section
=====================

// dynamically init storage for string vars, best done at boot.

        if (cmd.equalsIgnoreCase(F("userStringVarInit")))
        {
          success = true;
          int max = P201_parseString(string, 2).toInt();
          P201_Settings.sUserVarMax = max;
          if (P201_Settings.sUserVarMax) {
            P201_sUserVar = (P201_svarStruct*)malloc(sizeof(P201_svarStruct) * P201_Settings.sUserVarMax);
            if (P201_sUserVar == NULL) {
              P201_Settings.sUserVarMax = 0;
            }
            for (byte x = 0; x < P201_Settings.sUserVarMax; x++) {
              P201_sUserVar[x].Value = new String();
              P201_sUserVar[x].Name = new String();
            }
          }
        }

// create a new string var

        if (cmd.equalsIgnoreCase(F("LetString")))
        {
          success = true;
          String varName = P201_parseString(string, 2, ',');
          int pos = P201_getParamStartPos(string, 3);
          if (pos != -1) {
            String value = string.substring(pos);
            P201_setSvar(varName, value);
          }
        }


//********************************************************************************************
//  Set string Uservar by name
//********************************************************************************************
void P201_setSvar(String varName, String value) {
  int pos = -1;
  for (byte x = 0; x < P201_Settings.sUserVarMax; x++) {
    if (*P201_sUserVar[x].Name == varName) {
      *P201_sUserVar[x].Value = value;
      return;
    }
    if (pos == -1 && (*P201_sUserVar[x].Name).length() == 0)
      pos = x;
  }
  if (pos != -1) {
    *P201_sUserVar[pos].Name = varName;
    *P201_sUserVar[pos].Value = value;
  }
}

//********************************************************************************************
// Add custom parsing on generic parseTemplate calls. This is processed for rule events, commands, plugins like LCD etc
//********************************************************************************************
void P201_parseTemplate(String& string, bool encode) {

  // check named uservar strings
  for (byte x = 0; x < P201_Settings.sUserVarMax; x++) {
    String varname = "%" + *P201_sUserVar[x].Name + "%";
    String svalue = String(*P201_sUserVar[x].Value);
    string.replace(varname, svalue);
  }

}

martinus
Normal user
Posts: 129
Joined: 15 Feb 2020, 16:57

Re: Custom System Variables

#6 Post by martinus » 06 Feb 2021, 17:05

Ath wrote: 06 Feb 2021, 13:30 ... MQTT Import plugin with JSON parsing and filtering...
I also added some custom json parsing stuff to support my 'private' payload conventions on MQTT messages.
Basically inspired by this work: https://github.com/SmartNodeRules/Documentation/wiki

Comes down to this idea:
Outdoor/Temperature={"Value":21.5,"Type":"Sensor"}

And i've enhanced it with unique (mac) based device id's and optional other values for advanced node discovery.
The nodelist is maintained using MQTT so it no longer depends on UDP messages from the P2P controller.

My MQTT implementation is not bound to any Home Automation controller, so i have the freedom to make my own convention.

Within rules, the "value" part is converted to a float value by additions to the C005 plugin. So this MQTT message:

Outdoor/Temperature={"Value":21.5,"Type":"Sensor"}

Will turn into a rule event like this:

Outdoor/Temperature=21.5

garnold
Normal user
Posts: 93
Joined: 17 Jun 2019, 03:59

Re: Custom System Variables

#7 Post by garnold » 13 Jul 2021, 22:29

Martinus, I think your custom plugin might be able to help me with another project I'm working on. You be kind enough to check out this link and let me know. I need to create a customer var that can hold a string and be updated via a command call.

viewtopic.php?f=2&t=8664

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 35 guests