Changing text attribute into number to be used in rule machine

Gents,

I am using sonoff pow R3 (tasmotized) to read the all energy attributes using ohlalab driver. it works fine for basic functions like on/off and displaying values on the dashboard.

The issue have have is using these attributes in rule machine. they are all text. For example I cannot do a rule when the current value is less than 2 Amp for example.
I also noticed that in the rule machine itself, it does not show all the attributes in the device when trying to use a local or a global variables.

here is the attribute of the device:

I go to rule machine and try to select the custom attribute it shows the current but it is in text and i cannot do a comparision.

i thought about using a variable to set the current to a number but i cannot do that because it does not show all attributes as in the before screen.

Is there a way to change a text attribute into a number or a float number so that I can use it in a condition in rule machine?

Thank you.

The best solution would really be to fix the driver to report only the numeric component as the value of the standard attributes because that is how they are specified. It would probably also be a good idea to do the same for the custom attributes that are similar in nature, just because it makes this kind of thing easier (and it's consistent with other attributes where the unit is part of the event but not the attribute value), but at least with those, there aren't any hard and fast rules.

But because I assume you aren't interested in modifying this driver and the driver author has moved on, that leaves you two options. Either find a different driver that works (I'm not familiar with this device so don't know of any; there may be some), or use Rule Machine variable operations to get what you want.

To do the latter thing, which it seems is also more or less what you're asking: first, you'll need a variable (local or hub are both fine) to store the value of the desired attribute in. This should be a string variable (to match the attribute value type). You'll also need a number or decimal variable to store the actual value you're interested in--eventually. You will also need a third string variable to hold a string you split apart from the first variable (see below). Then, you can use various Rule Machine variable operators to extract the data you need from the first variable and, eventually, get it into the second. One such operation lets you split a variable based on some delimiting character, which in your case seems like it could be the space character--if there's always a space between the "real" value and the units. (In the "Set Variable" action, use "token" as the operation, a single space character as the delimiter, and use index...0, I think, unless they start at 1?) Then you can use another Rule Machine variable operation to convert the "numeric string" into an actual number variable.

But yeah, this would be a lot easier with a driver that followed the conventions. :smiley:

4 Likes

Hi Bert,

Thank you for your reply. Changing the code it mission impossible for me.

I have created three variables "miancurrentstring" as the variable to put the cuurent string from the device attribute. the second is "maincurrentvlaue" a decimal to store the value as a number and the third is "maincurrentsplit" to slpit the string from value and unit. see below

then i went to RM and set the maincurrentstring value to be the mainfeeder current value which is (0.000 A)

the second step i did wht you have said but i got stuck on the variable index. see below

I know i am doing something wrong. please can you guide me?

I have a general question about rule machine. it works with triggers. I was wondering if the RM will only trigger once when the trigger condition is true. or does it check the conditions over and over again as long as the trigger condition is true? my understanding of trigger it does its conditions once every time the trigger condition goes from false to true.
Why I am asking this question is because i want the conditions i put in the RM to be continuously checked?

Thank you

Hi Bert,

Sorry, I got 90% of it to work. I still do not know to convert the string to decimal in the RM.

I got the following done

set the current string to the maincurrentstring then I strip the value to the maincurrentsplit

Now how to convert the string in maincurrentsplit to the maincurrentvalue?

Thanks

Turn off the "variable index?" option. You are using a fixed index, the first position (I forget if this is 0 or 1 in RM...) and can just specify that value without using a variable for it.

Triggers represent events, not conditions, and as such they do not have truth values. They are just a moment in time, when that event happens. (The new "sticky triggers," the "stays" option, muddles this a bit, but it's still the same basic idea.) When a trigger event happens (unless a required expression is used and is false), the Rule actions run. A conditional in Rule axtikna will not do anything on its own. It will just be evaluated at the time when it is reached in execution.

Hi Bert,

Thank you for your help. I got the conversion to work properly. But my question now is how to make the below RM runs indefinitely without any triggers. I want to keep the "maincurrentvalue" updated all the time so that i can use it in a different RM.

My apologize for lots of questions. I am a beginner with HE.

Thank you

Seems like it should work if you trigger on the "real" device attribute changing (you may need to use "Custom Attribute" if Rule Machine chokes on a standard-numeric value being presented as a string).

Makes sense.

I will use a trigger by a user command (button) instead of automatic based on current value.

Thank you again for your help

Download the Hubitat app