json Text sensor

Topics (not sure which fora)
when not sure where to post, post here and mods will move it to right forum.

Moderators: leecollings, remb0

Post Reply
DomoticaRob
Posts: 80
Joined: Sunday 29 November 2015 14:03
Target OS: Raspberry Pi / ODroid
Domoticz version: V4.9700
Location: Halle, the Netherlands
Contact:

json Text sensor

Post by DomoticaRob »

In Domoticz I have made a Text sensor and in a ESP8266 I have a function that must update the text.

In the Wiki it says:
/json.htm?type=command&param=udevice&idx=IDX&nvalue=0&svalue=TEXT

IDX = id of your device (This number can be found in the devices tab in the column "IDX")
TEXT = Text you want to display

If I place the next code in my ESP8266 it works:

Code: Select all

url = "/json.htm?type=command&param=udevice&idx=83&nvalue=0&svalue=Relais%201%20is%20uit.";
              http.setAuthorization("My InlogName for Domoticz", "My PassWord for Domoticz");
              sendDomoticz(url);    
Now I want to make a long text out of different variables. String variables, Integer variables, Long variables etc.

For instance:

Variable1 = "Het weer is"
Variable2 = "super"
Variable3 = "."
Variable4 = "De temperatuur ="
Variable5 = 21,5
Variable6 = "'C"
Variable7 = "en de luchtvochtigheid ="
Variable8 = 65,40
Variable9 = %

I want in my Text sensor the text shown as:

Het weer is super.
De temperatuur = 21,5 'C en de luchtvochtigheid = 65,40%.



How do I do that?

In dzVents I can do it but I want to do it from my ESP8266 module.
User avatar
waltervl
Posts: 5733
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: json Text sensor

Post by waltervl »

Check the documentation of your esp8266 programming language how to create strings of multiple values.
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
DomoticaRob
Posts: 80
Joined: Sunday 29 November 2015 14:03
Target OS: Raspberry Pi / ODroid
Domoticz version: V4.9700
Location: Halle, the Netherlands
Contact:

Re: json Text sensor

Post by DomoticaRob »

I do programm the ESP8266 with the Ardiuino IDE.

So, I think it is C?

And in the C documentation I get different solutions.

When I use these I get error messages as:

Compilation error: cannot convert 'String' to 'const char*' in assignment

It would be helpfull if I get an example of the Domoticz json syntaxt wit a variable for the TEXT part at the end of the line.
User avatar
heggink
Posts: 978
Joined: Tuesday 08 September 2015 21:44
Target OS: Raspberry Pi / ODroid
Domoticz version: 12451
Location: NL
Contact:

Re: json Text sensor

Post by heggink »

Not sure I fully understand but let's try anyway: normally, you need to use some urlencode function to convert special characters. '\n' is a newline which you should be able to insert in the text string. You can also do things manually but then need to know how to translate. A space character would be %20 and so on...
Docker in Truenas scale, close to latest beta
DASHTICZ 🙃
RFXCOM, zwavejs2mqtt, zigbee2mqtt,
P1 meter & solar panel
Google home, Wifi Cams motion detection
Geofence iCloud, Bluetooth & Wifi ping
Harmony hub, Nest, lots more :-)
Jieffe
Posts: 18
Joined: Wednesday 07 April 2021 19:33
Target OS: Linux
Domoticz version: 2023.2
Location: Belgium
Contact:

Re: json Text sensor

Post by Jieffe »

Have you tried converting your "String" to a "const char*" ?

Code: Select all

String str = "My_String";
const char* c = str.c_str();
DomoticaRob
Posts: 80
Joined: Sunday 29 November 2015 14:03
Target OS: Raspberry Pi / ODroid
Domoticz version: V4.9700
Location: Halle, the Netherlands
Contact:

Re: json Text sensor

Post by DomoticaRob »

Hello Jieffe,

I do understand this.

But how do I put this const char* c into the TEXT part of the json syntax?

url = "/json.htm?type=command&param=udevice&idx=IDX&nvalue=0&svalue=TEXT";

If I do the next line:

url = "/json.htm?type=command&param=udevice&idx=83&nvalue=0&svalue=c";

I get:

json.jpg
json.jpg (49 KiB) Viewed 1472 times
BartSr
Posts: 395
Joined: Sunday 03 July 2016 16:16
Target OS: Raspberry Pi / ODroid
Domoticz version: V2024.7
Location: Netherlands
Contact:

Re: json Text sensor

Post by BartSr »

I would post your question on the arduino forum.
Raspberry pi 3b
Arduino
KAKU
RfxCom
Zwave2MQTT
OTGW
Chinese sensors temp (Dallas),movement
Tasmota
Esp8266 / 32 espeasy
Zigbee2MQTT
Kedi
Posts: 569
Joined: Monday 20 March 2023 14:41
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Somewhere in NL
Contact:

Re: json Text sensor

Post by Kedi »

Just use VariableResult = Variable1+"%20"+Variable2+"%20"; etc....
And add the VariableResult to the end of the url string like

Code: Select all

url = "/json.htm?type=command&param=udevice&idx=83&nvalue=0&svalue="+VariableResult;
Logic will get you from A to B. Imagination will take you everywhere.
Jieffe
Posts: 18
Joined: Wednesday 07 April 2021 19:33
Target OS: Linux
Domoticz version: 2023.2
Location: Belgium
Contact:

Re: json Text sensor

Post by Jieffe »

I would construct the whole URL as a String and then convert it as a const char*

Something like that :

Code: Select all

int IDX_variable = 83;
int NVALUE_variable = 0;
float TEMP_variable = 20.50;
float HUMD_variable = 60;

String tmp = "/json.htm?type=command&param=udevice&idx=";  

tmp += IDX_variable; 
tmp += "&nvalue="; 
tmp += NVALUE_variable;
tmp += "&svalue='Het weer is super. De temperatuur = ";
tmp += TEMP_variable;
tmp += "°C en de luchtvochtigheid = ";
tmp += HUMD_variable;
tmp += " %'";

const char* url = tmp.c_str();
The result is : /json.htm?type=command&param=udevice&idx=83&nvalue=0&svalue='Het weer is super. De temperatuur = 20.50 °C en de luchtvochtigheid = 60.00 %'
DomoticaRob
Posts: 80
Joined: Sunday 29 November 2015 14:03
Target OS: Raspberry Pi / ODroid
Domoticz version: V4.9700
Location: Halle, the Netherlands
Contact:

Re: json Text sensor

Post by DomoticaRob »

A thousand apologies.
Sorry Sorry Sorry.


I had defined the 'url' as const char* instead of a string.

Now 'url' is a string and it works.


Now placing a new line in the text.

I use "\n".

Code: Select all

url = "/json.htm?type=command&param=udevice&idx=83&nvalue=0&svalue=Voorbeeldtekst\nRegel2\nRegel3";
This produces a new line with Serial.print(url); of the Arduino edi (monitor), which is good.
But this also splits my URL string and gives me a httpCode = : 400 after a URL request. This should be httpCode = : 200.

Code: Select all

connecting to 192.168.178.200
poort 8080
Requesting URL: /json.htm?type=command&param=udevice&idx=83&nvalue=0&svalue=Voorbeeldtekst
Regel2
Regel3
httpCode = : 400
So I am almost at the ponit where I want to be. Only new lines in my Domoticz Text Device and not in my json string.
Kedi
Posts: 569
Joined: Monday 20 March 2023 14:41
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Somewhere in NL
Contact:

Re: json Text sensor

Post by Kedi »

Try using %0A for newline and %0D for carriage return.
Logic will get you from A to B. Imagination will take you everywhere.
Jieffe
Posts: 18
Joined: Wednesday 07 April 2021 19:33
Target OS: Linux
Domoticz version: 2023.2
Location: Belgium
Contact:

Re: json Text sensor

Post by Jieffe »

Try enclosing the text you want to show in Domoticz with single quotes, i.e :

Code: Select all

url = "/json.htm?type=command&param=udevice&idx=83&nvalue=0&svalue='Voorbeeldtekst\nRegel2\nRegel3'";
DomoticaRob
Posts: 80
Joined: Sunday 29 November 2015 14:03
Target OS: Raspberry Pi / ODroid
Domoticz version: V4.9700
Location: Halle, the Netherlands
Contact:

Re: json Text sensor

Post by DomoticaRob »

Code: Select all

url = "/json.htm?type=command&param=udevice&idx=83&nvalue=0&svalue='Voorbeeldtekst\nRegel2\nRegel3'";
Unfortanataly this is not working.

Thanks for the tip.

=================================================================================

Try using %0A for newline and %0D for carriage return.

This is working.

Super!


Muchas gracias everybody for all thoughts.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest