Lua String skipping white spaces and substituting unexpected characters

Moderator: leecollings

Post Reply
coppo23
Posts: 5
Joined: Sunday 05 April 2015 20:08
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Lua String skipping white spaces and substituting unexpected characters

Post by coppo23 »

I'm running Domoticz Version: 3.4834 on a Raspberry Pi.

I have run into a couple of string problems I don't understand, These could well be beginners silly issues - so I apologize in advance.
I would appreciate any helpful advice as I am completely stuck with these issues at the moment.

Issue a) Something is substituting groups of characters in a print statement:
I have a lua script for heating control and one function scans for and updates device IDs using a curl call. If I have a missing device I report the error in the log and give some helpful info as follows:

Code: Select all

	print(string.format("try updating it from the real device or URL as follows"))
      	print(string.format("http://1.2.3.4:8080/json.htm?type=command&param=udevice&idx=%d&nvalue=0&svalue=andywasere",   zoneInfoIDX))
 
When the result of that appears in the log the characters &param are changed and &para is substituted by the reverse P paragraph sign as shown below. If I change the text from &para to &Para - it doesn't do the substitution - but the suggested URL json call then won't work.
2016-03-22 12:12:00.308 LUA: try updating it from the real device or URL as follows
2016-03-22 12:12:00.308 LUA: http://1.2.3.4:8080/json.htm?type=comma ... andywasere
Issue b) Something is stripping whitespace.
I use a text type device per zone for communicating a lot of general heating zone info from a mysensors battery thermostat. Part of that string is the zone name, which I try and pad to fixed length using spaces. Something is stripping all but the first space from the string. I have worked around it by using an underscore instead of space - but it looks untidy.

Example 1: Works using underscore

Code: Select all

      local newZoneName = zoneName.."________"
      newZoneInfo = string.format("%s%03d%03d%03d%c ",string.sub(newZoneName,1,12), newZoneTimer, 
                            newHouseTimer,tonumber(newZoneSetpoint)*10,flgs1)
This gives an output string "Kitchen_____000000052" This is the correct length.

Example 2: Doesn't work using spaces

Code: Select all

      local newZoneName = zoneName.."         "
      newZoneInfo = string.format("%s%03d%03d%03d%c ",string.sub(newZoneName,1,12), newZoneTimer, 
                            newHouseTimer,tonumber(newZoneSetpoint)*10,flgs1)
This gives an output string "Kitchen 000000052" . There is only 1 space after 'Kitchen'

Example 3: Doesn't work using spaces

Code: Select all

      newZoneInfo = string.format("%-12s%03d%03d%03d%c ",zoneName, newZoneTimer, 
                            newHouseTimer,tonumber(newZoneSetpoint)*10,flgs1)
This gives an output string "Kitchen 000000052" . There is only 1 space after 'Kitchen'

Basically any way I can find of padding the zonename with spaces ONLY ever adds 1 space to the end output. If I pad the string to 20 characters with spaces and then check its length at that point it will correctly be 20. As soon as I use it in string.format it looses the last few spaces, leaving only 1
User avatar
PsychoMark
Posts: 3
Joined: Monday 21 March 2016 19:11
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.5204
Location: The Netherlands
Contact:

Re: Lua String skipping white spaces and substituting unexpected characters

Post by PsychoMark »

a) it looks like the log isn't escaping HTML characters coming from Lua (not sure if bug or feature). I confirmed this by using print("<b>Bold?</b>"), which did exactly what you'd expect :). The workaround is to escape it yourself before passing it to print: ...&param=...

b) I'm thinking it's the same issue, since HTML strips away duplicate whitespace. Replacing it with &nbsp; before sending it out would most likely work, but feels kinda hacky and I'm not sure what else would break because of it. Depends on if the whitespace is for display purposes only or if you're using the values somewhere else programmatically I think.
coppo23
Posts: 5
Joined: Sunday 05 April 2015 20:08
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Lua String skipping white spaces and substituting unexpected characters

Post by coppo23 »

Thanks - escaping the ampersand in issue a worked OK and have implemented that as per your suggestion.

In the second one I am using the space(or underscores) both visually in Domoticz and programmatically in Mysensors. For the latter I have just done a simple substitution space for underscore and the only real problem was that I didn't understand what was happening, The visual appearance in domoticz doesn't really matter as I only view this device for debug purposes and it doesn't need a high WAF. I'll stick with the workaround now I know the reason.

Many thanks for this. I have learnt something newI

Regards
Coppo
civi
Posts: 3
Joined: Friday 21 October 2016 12:14
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.2
Location: Madrid, Spain
Contact:

Re: Lua String skipping white spaces and substituting unexpected characters

Post by civi »

Hello. I'm currently using beta 3.5033 in a Raspberry Pi 2 and I have been using the json interface to manage domoticz elements without problem. I normaly update to beta versions if I find something solving any issue I have, or any interesting feature.

Some weeks ago I tried to update to the latest beta via Domoticz web interface, but it failed to restart. I also tried to reboot the Raspberry but no luck, I got errors running Domoticz (sorry, no log stored). As I previously did a backup of the 'domoticz' executable and the database, I reverted both and my system started to work again....(I know it is not the best way to backup/restore the system, but this time it worked)

Since this change, I have the Issue A related by Coppo (substitution of &param by ¶m) in any script (lua, sh) executed from Domoticz. I have checked it in Domoticz log, but I think the problem is not in the logger, because the results of the commands are not OK. The same commands are working if sent from a PC web browser. I haven't tried if they work in the bash shell of the RPi (outside Domoticz, but in the same machine). I'll provide this information later if I check it.

Since this problem I have refused to update Domoticz to newer betas. And I don'k know if it is better to do it from the command line.
Luckily I have found a solution in this post. I will try it....Thanks PsychoMark!....but, anyone can tell me where is the origin of the problem?

Thanks,
Civi
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests