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
Custom System Variables
Moderators: grovkillen, Stuntteam, TD-er
Re: Custom System Variables
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.
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.
Re: Custom System Variables
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).
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).
Re: Custom System Variables
Very interesting! Would you care to share/create a PR with that plugin?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).
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)
Re: Custom System Variables
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.
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);
}
}
Re: Custom System Variables
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
Re: Custom System Variables
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
viewtopic.php?f=2&t=8664
Who is online
Users browsing this forum: No registered users and 1 guest