Permanent dashboard status (avoid reset)

Good morning,
I try to explain my problem with a simple example:

  1. I have a dashboard with a control switch (ON or OFF).
  2. I open the dashboard and set the switch to ON (my ESP8266 device understands the status of that switch and performs actions).
  3. I close the dashboard, but my device is always on.
  4. The next day I open the dashboard again and I would like to see the switch as ON, in other words i would like to see my dashboard in the last state in which I left it, instead I see it resetted, with my switch in OFF position.

This makes it impossible to use it as a device configuration interface, for example a thermostat. (many controls as sliders, switches…)
Is there a way to make the last states / values ​​permanent in the dashboard? or update from come a switch that for device is an input resource? Do I need to upgrade my account?

In other words I would like open my dashboard and see it as the last time I’ve configured it.

Thanks in advance,
Davide

NOT Solved with:

thing[“resource”] << [](pson& in){
if(in.is_empty()){
in = currentState;
}
else{
currentState = in;
}
}

Hello @Davide_Alessandrini,

you have to avoid the situation in which the Pson is empty, check this part of the documentation: https://docs.thinger.io/coding#show-input-resources-state-in-dashboards-and-api

best!

Hello @jtrinc26,
thanks for the reply. I tried to use the lambda expression to define the behavior in case of empty Pson, but unfortunately the switch on the dashboard, when opened, does not present the position of currentState, defined as Boolean. If my currentStateis “true” I see control switch on dashboard always as “OFF”.

thing[“resource”] << [](pson& in){
if(in.is_empty()){
in = currentState;
}
else{
currentState = in;
}
}

Maybe I must call another function in the sw loop near thing.handle() ?

hi @Davide_Alessandrini,

Ok at this point you have to take care of when do the “currentState” pson receives its value. I recommend you changing it by a digitalRead:

thing[“resource”] << [](pson& in){
if(in.is_empty()){
   in = digitalRead(RELAY_PIN); 
}
   else{
      digitalWrite(RELAY_PIN, in ? HIGH : LOW);
   }
}

And you can even user easier resource definition as stated in: https://docs.thinger.io/coding#easier-resources. This kind of definition will show you the current resource state when opening the dashboard :wink:

thing["relay"] << digitalPin(PIN_NUMBER);
thing["relay"] << invertedDigitalPin(PIN_NUMBER);

Thank you,
in order to try if something change I’ll try with digitalRead (it returns an integer), but in my case Control switches and sliders control variables in my SW, not HW pin state.

Take for example to control with a slider a temperature that represent a threshold for a device like a thermostat. You leave the slider in a value, but when you open again the dashboard you find the slider to Min Value…

What is the way in the code by which, at re-opening the dashboard, I see the slider in the last position I leave it?

I thought it was the mechanism with evaluation on pson empty, like you have indicated me, but in my case it doesn’t work, I don’t understand why.

Thank you again for your support.

After hundreds of attempts, I identified the problem: in order for the control switch, on the dashboard, to open at the last value, the resource name must be written in block capitals.

thing[“led”] << [](pson& in){ …etc WORKS

thing[“LED”] << [](pson& in){ …etc DOESN’T WORK

I’ve made many many test…that is what I’ve found…

Could you make a test on this and give me a feedback?

THIS WORKS: (control switch in dashboard linked to resource “LED”)

#include <ESP8266WiFi.h>       //ESP8266 WiFi connection library
#include <ThingerESP8266.h>    //THINGER.IO library

// Thinger.io connection parameters
#define USERNAME "davidealessandrini"
#define DEVICE_ID "esp8266"
#define DEVICE_CREDENTIAL "WRLbbOtuaYb&"

#define SSID "******"
#define SSID_PASSWORD "********"

ThingerESP8266 thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);

// Global variable
int led = 2;       // GPIO02 - D4 
int led_status = 0;
void setup() {

 // Initialization of the WiFi connection with THINGER.IO
   thing.add_wifi(SSID, SSID_PASSWORD);

 // Initialization of the LED
   pinMode(led, OUTPUT);

 // Resource for changing LED status from THINGER.IO
  thing["LED"] << [](pson& in){
    if(in.is_empty()){
      in = (bool)led_status;//(bool) led_status;//digitalRead(led);
      }
    else{
      //digitalWrite(led, in ? HIGH : LOW);
      led_status = in;
      }
    };
  }

void loop() {
  thing.handle();
  digitalWrite(led, led_status);
  }

THIS DOESN’T WORK: (control switch in dashboard linked to resource “led”)

#include <ESP8266WiFi.h>       //ESP8266 WiFi connection library
#include <ThingerESP8266.h>    //THINGER.IO library

// Thinger.io connection parameters
#define USERNAME "davidealessandrini"
#define DEVICE_ID "esp8266"
#define DEVICE_CREDENTIAL "WRLbbOtuaYb&"

#define SSID "******"
#define SSID_PASSWORD "********"

ThingerESP8266 thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);

// Global variable
int led = 2;       // GPIO02 - D4 
int led_status = 0;
void setup() {

 // Initialization of the WiFi connection with THINGER.IO
   thing.add_wifi(SSID, SSID_PASSWORD);

 // Initialization of the LED
   pinMode(led, OUTPUT);

 // Resource for changing LED status from THINGER.IO
  thing["led"] << [](pson& in){
    if(in.is_empty()){
      in = (bool)led_status;//(bool) led_status;//digitalRead(led);
      }
    else{
      //digitalWrite(led, in ? HIGH : LOW);
      led_status = in;
      }
    };
  }

void loop() {
  thing.handle();
  digitalWrite(led, led_status);
  }

This is not enough, casting to bool is required, even if I use “LED”, casting to bool is mandatory for dashbord to work correctly.

Hello @Davide_Alessandrini,

Glad to know that you made it work. It is strange because, I have tested both options and always works well for me. I recommend you not making the digitalWrite in the loop.

Best!

Thanks for recomendation,
I can’t help you on investigation I’m using hotspot in my tests, if it could modify resource manager (I don’t think so).
I’ve made other tests and I confirm my previous analysis: upper capital letters are mandatory.
If you need some datails on my dashboard configuration let me know by private message.

Best regards