Aller au contenu
Bloug

attempt to concatenate a function value

Recommended Posts

Bonjour,

 

Voila mon problème du jour, ( c'est les vacances ... il faut bien vous faire réviser(ou entrainer en fonction des footeux ) non ? !  :P)

 

J'ai une fonction qui récupère un Json contenant la consommation de la journée de ma chaudière. 

Ma requête cible la "semaine" pour pouvoir piocher le "jour" en les comparants avec ma variable today

Tout  allait bien jusqu'à Lundi car le Lundi ... c'est la mer*e !

 

if response.status == 200 then
               print('Connection Ok')     
  
jsonResponse = json.decode(response.data)

 if jsonResponse["body"][1]["dataset"][1].key == today then
    CHConsumedElectricalPower = jsonResponse["body"][1]["dataset"][1].value
elseif jsonResponse["body"][1]["dataset"][2].key == today then
    CHConsumedElectricalPower = jsonResponse["body"][1]["dataset"][2].value
elseif jsonResponse["body"][1]["dataset"][3].key == today then
    CHConsumedElectricalPower = jsonResponse["body"][1]["dataset"][3].value
elseif jsonResponse["body"][1]["dataset"][4].key == today then
    CHConsumedElectricalPower = jsonResponse["body"][1]["dataset"][4].value
elseif jsonResponse["body"][1]["dataset"][5].key == today then
    CHConsumedElectricalPower = jsonResponse["body"][1]["dataset"][5].value
elseif jsonResponse["body"][1]["dataset"][6].key == today then
    CHConsumedElectricalPower = jsonResponse["body"][1]["dataset"][6].value
elseif jsonResponse["body"][1]["dataset"][7].key == today then
    CHConsumedElectricalPower = jsonResponse["body"][1]["dataset"][7].value
else
    self:debug("Probleme avec CHConsumedElectricalPower ")
end
  

self:updateView("CHConsumedElectricalPower","text", " Chaudière - Consomation Electrique du jour  : " .. CHConsumedElectricalPower )
self:GetConfig6() -- Requette suivante
         else
                self:debug('Error : '  .. response.status .. response.data)

Le rapport de conso de la chaudière est capricieux est n'arrive pas à l'aube du coup le Json lorsqu'il est vide donne une valeur null

 

{
  "body": [
    {
      "key": "2022-12-19",
      "summaryOfValues": null,
      "dataset": [
        {
          "key": "2022-12-19",
          "value": null
        },
        {
          "key": "2022-12-20",
          "value": null
        },
        {
          "key": "2022-12-21",
          "value": null
        },
        {
          "key": "2022-12-22",
          "value": null
        },
        {
          "key": "2022-12-23",
          "value": null
        },
        {
          "key": "2022-12-24",
          "value": null
        },
        {
          "key": "2022-12-25",
          "value": null
        }
      ]
    }
  ],
  "meta": {
    
  }
}

 

j'ai testé de modifier la condition pour la faire échouer :

 

         if jsonResponse["body"][1]["dataset"][1].key == today and jsonResponse["body"][1]["dataset"][1].value ~= null then

ou

         if jsonResponse["body"][1]["dataset"][1].key == today and jsonResponse["body"][1]["dataset"][1].value ~= "null" then

mais le 'null ' semble être ignoré et le débug retourne toujours :

 

[DEBUG] [QA621_VAILLANT]: function: 0xaaab0568d480
[DEBUG] [QA621_VAILLANT]:
[ERROR] [QUICKAPP621]: QuickApp crashed
[ERROR] [QUICKAPP621]: main.lua:314: attempt to concatenate a function value (global 'CHConsumedElectricalPower')

la ligne 314 est : self:updateView("CHConsumedElectricalPower","text", " Chaudière - Consomation Electrique du jour  : " .. CHConsumedElectricalPower )

 

Pourquoi lorsqu'il y a une valeur , le print donne un nombre et là il me donne une fonction ? Et comment indiquer le 'null' dans la condition ?

 

 

 

merci :)

 

 

Modifié par Bloug

Partager ce message


Lien à poster
Partager sur d’autres sites

Et en testant si ce n'est pas une fonction :

 

 if jsonResponse["body"][1]["dataset"][1].key == today and type(jsonResponse["body"][1]["dataset"][1].value) ~= "function" then

ou encore en s'inspirant de cette solution : https://forum.fibaro.com/topic/57408-nil-null/

Modifié par Barelle
Complément
  • Like 1

Partager ce message


Lien à poster
Partager sur d’autres sites

merci @Barelle !

Cela fonctionne parfaitement !:16:

...mais je comprends pas trop pourquoi est-ce une fonction 

 

 

 

Partager ce message


Lien à poster
Partager sur d’autres sites
Il y a 4 heures, Bloug a dit :

thank you@Barelle !

It works perfectly!:16:

...but I don't really understand why is it a function 

Yes, 

A json value can contain null values. If we replace null with nil in a Lua table the key/value is removed from the table.

A proper json decoder for Lua would like to preserve the null values as it can be significant (key exist with null value, or key does not exist at all)

The json decoder choose json.null to be a function value is that they wanted a value that is unique and quick to compare.

In a QA, by providing the {others = {null=false}} option to json.decode it will replace null with Lua nil and the key removed.

It doesn't work in Scenes as they use a different json decoder/encoder that doesn't take options.

  • Like 3

Partager ce message


Lien à poster
Partager sur d’autres sites

×