REST question

Your way maybe better than mine, honestly I just saw the dateString in the json pull and googled how to pull the local (non zulu) time from that. If you use value_json[0].dateString alone, your sensor will read the way it does in the browser if you navigate to the api extension. When you add “as_timestamp” in front of that, I think? that converts it to a unix? time format. The timestamp_custom portion then converts it to however you want to read it. So now the sensor reads 02/16/19 xx:xx:xx AM (local time). I display this in huge block letters along with the sensor reading on a display next to my bed. Its very easy to read.

Update: After further research I now know that epoch and unix time are the same?. So I assume I can just pull date instead of dateString and then use a custom format on that. I guess that would be the more correct way.

I like the idea of a symbol for a stale reading. That would be a whole lot simpler than having to look at current time vs last reading taken time. How are you implementing that?

I did experiment with dateString, but when i used the Node-Red Poll State node, all I got was “undefined”.

I am usually wrong talking about anything Unix, but this is my understanding of the two terms:

“Epoch time” and “unix Time” are interchangeable but “unix time” can be ambiguous. I’ve seen examples where the current time or a timestamp is saved as “unix time”. Think of “epoch time” as a constant- the singularity of when time started. “Unix time” is the format.

In Node-Red I have some Poll State nodes that send the API data every ten seconds:

cgmnodes

In the NodeMC sketch, I subscribe to those topics. When those topics come into the callback function, I handle them each a little differently.

Here’s the code. It’s a bit long because I have some debug print statements, and I am sure someone with more C experience could write it tighter. [Which is weird, because x=x_1 compiles exactly the same as x=+1, but the former is more readable. The same for if() tests. Experienced programmers like to put if-then on one line, but spread over three or four lines, it compiles exactly the same.]
(By the way, my ten minute timeout isn’t working yet.)

// **********************************  mqtt callback *************************************
// This function is executed when some device publishes a message to a topic that this ESP8266 is subscribed to.
void callback(String topic, byte * message, unsigned int length) {

  digitalWrite(ledPin, ledON);          // Turn on LED and start timing it.
  ledMillis = millis();

  bgTimestamp = now();                  // Used to flag if more than tenMinutes without a bg read has elapsed.

  Serial.println();
  Serial.println();
  Serial.print(F("Message arrived on topic: "));
  Serial.print(topic);
  Serial.println(F("."));

  Serial.print("messageString: ");

  // Convert the character array to a string
  String messageString;
  for (int i = 0; i < length; i++) {
    messageString += (char)message[i];
  }
  messageString.trim();

  Serial.print(messageString);
  Serial.println();
  Serial.print(F("Length= "));
  Serial.print(length);
  Serial.println();

  if (topic == dateTopic) {
    Serial.print (F("Date= "));
    Serial.println(messageString);
  }

  if (topic == bgTopic) {
    //What we really want to do is display the msg on the HT16K33 I2C display.
    //Format the array, right-justified, leading spaces, but leaving the rightmost display character empty.
    //for trend up/down arrows.

    //Send the data to the display
    if (length < 2) {
      //Should never see this
      alpha4.writeDigitAscii(0, '?');
      alpha4.writeDigitAscii(1, '?');
      alpha4.writeDigitAscii(2, '?');
      alpha4.writeDigitAscii(3, ' ');
      alpha4.writeDisplay();
    }
    else if (length == 2) {
      alpha4.writeDigitAscii(0, ' ');
      alpha4.writeDigitAscii(1, message[0]);
      alpha4.writeDigitAscii(2, message[1]);
      alpha4.writeDigitAscii(3, ' ');
      alpha4.writeDisplay();
    }
    else if (length == 3) {
      alpha4.writeDigitAscii(0, message[0]);
      alpha4.writeDigitAscii(1, message[1]);
      alpha4.writeDigitAscii(2, message[2]);
      alpha4.writeDigitAscii(3, ' ');
      alpha4.writeDisplay();
    }
    else if (length > 3) {
      //Should never see this
      alpha4.writeDigitAscii(0, '?');
      alpha4.writeDigitAscii(1, '?');
      alpha4.writeDigitAscii(2, '?');
      alpha4.writeDigitAscii(3, ' ');
      alpha4.writeDisplay();
    }
  }


  if (topic == trendTopic) {
    switch (messageString.toInt())
    {
      case 1:
        alpha4.writeDigitAscii(3, '1');
        alpha4.writeDisplay();
        break;
      case 2:
        alpha4.writeDigitRaw(3, 513);     //Segments A+J
        alpha4.writeDisplay();
        break;
      case 3:
        alpha4.writeDigitRaw(3, 1024);   //Segment K
        alpha4.writeDisplay();
        break;
      case 4:
        alpha4.writeDigitRaw(3, 128);    //Segment G2
        alpha4.writeDisplay();
        break;
      case 5:
        alpha4.writeDigitRaw(3, 8192);  //Segment N
        alpha4.writeDisplay();
        break;
      default:
        alpha4.writeDigitAscii(3, '?');
        alpha4.writeDisplay();
        Serial.print(F("Trend? "));
        Serial.print(messageString);

    }
  }
}

Did this help?

You are definitely more advanced than me! I ended up just using this to convert how old then sensor read is into minutes.

  - platform: rest
    name: bg_dateString
    resource: https://yoursitehere.herokuapp.com/api/v1/entries/current.json
    value_template: "{{ ( (as_timestamp(now()) - as_timestamp(value_json[0].dateString) ) / 60) | round(0) }}"

Then I just read the sensor on a display. Works for now. Once I get some more time I am going to see if I can add trending info.

No way- I just have more time to experiment. I found an old clock that might make the perfect box for my project. Here’s the breadboard showing my version of the up arrow:

And here is the stale data indicator. (It is working now).

I am also interested in your mirror installation. What is the display?

That is awesome!

The display I used is a 800x600 one that I pulled out of an old Kodak pulse digital picture frame. It was more of a see if I can get it to work project. I had to get a controller board from aliexpress. That prob cost more than just buying an hdmi capable monitor from a thrift store. It would have had better resolution as well. PC is an old raspberry pi.

Hmm, I have a couple of these that we aren’t using. Not the Kodak model, though. How did you figure what controller to use?

When I pulled the frame apart I noticed the display had a part number on it. I googled it and found a few compatible controllers.

Progress. I put the device into a box in the bedroom for the intended purpose- a near realtime display of my BG.

Question: I tried to get the dateString based on the same template that I use to get sgv, date, and trend data, but datestring2
The debug node just says: “undefined”.

Any clues?

Does it show up in Homeassistant as undefined or just node red?

Only in Node Red. The DateString is OK in Home Assistant.

By the way, have you figured out what “CGM direction” of “FortyFiveUp” means?

I assumed it meant arrow forty five degrees up .

The way I have the datestring in my yaml file, it shows up the same way in node red.

I haven’t watched it closely enough to conclude that, but I use the trend value to determine the trend icon on the display. I have figured for 0 through 8 and haven’t seen anything other than 0-8.

After a fairly serious incident a week or so ago involving an ambulance, 3 days in hospital and a week off work I have decided it is time to up my management game again. Mea culpa.

So I stumbled across xdrip+ when my new meter (finger pricker not cgm) wouldn’t connect to it’s proprietary software, and from xdrip+ found nightscout. Wonderful I thought, open source diabetes management. I could probably build a hass component and finally contribute some code.

Dammit, you guys have beaten me to it. Good work, nice to see.

Madseason41 deserves all the praise for cutting through the maze of inter-connected clouds to get the Dexcom CGM reading from your sensor to your phone to Dexcom where Nightscout accesses it and makes it available as a REST reply.

In my case, I am sending the CGM reading to my local network as an mqtt payload. I have built a couple of devices to display the data- similar to the breadboard shots above. I’ve changed to the Wemos D1 Mini instead of the NodeMCU, and I added a second display to show the current time. I get the time data from the Node-Red Simpletime node which gets published every ten seconds. The idea of the second iteration is that I plan to put a display in the Family Room (the first is in my bedroom on top of my clock).

Since I know little to nothing about making a Home Assistant Component. I have sensors in sensors.yaml that read the REST data from HerokuApp, then do the rest in Node-Red.

If you can make it easier, I am all for it.

BTW, sorry about your incident. Judging from the timeline I think we know what happened. It makes my minor back injury about the same time seem pale in consideration.

I am stuck and not sure where I messed up or where to go from here.

If manually run script it tells me that Moms blood sugar is unknown.

script.yaml

bg_tts_alarm:
    alias: BG TTS Alarm
    sequence: 
      - service: media_player.volume_set
        data:
        entity_id: media_player.josh_and_caila_mini
        entity_id: media_player.coral
        entity_id: media_player.xbox_mini
        volume_level: "0.50"
      - service: tts.google_say
        entity_id: media_player.josh_and_caila_mini
        entity_id: media_player.coral
        entity_id: media_player.xbox_mini
        data_template:
         message: "Wake up. Mom blood sugar is {{states('sensor.json_sgv')}}."

bg_tts_current:
    alias: BG TTS Current
    sequence:
      - service: tts.google_say
        entity_id: media_player.josh_and_caila_mini
        entity_id: media_player.coral
        entity_id: media_player.xbox_mini
        data_template:
          message: "{{states('sensor.json_sgv')}}."

monitoron_mirror:
    alias: Mirror Monitor On
    sequence:
      - service: shell_command.monitoron_mirror

cofig.yaml

sensor:
  - platform: rest
    name: JSON sgv
    resource: https://private.herokuapp.com/api/v1/entries/current.json
    value_template: '{{ value_json[0].sgv }}'

shell_commands.yaml

Not sure what ip or the port that goes in here but I tried my HA
monitoron_mirror: curl -s http://192.168.1.42:9411/remote?action=MONITORON

Any help would be great.

Thank You in Advance

Please format your code properly. Highlight it then use the </> to format it and then fix the indentation.

Tried to fix the code but not sure if I did it correctly. Tried to do it from work. But HA did not yell at me for formatting issue.

OK, then we will ‘yell’ at you … fix the indentation. :slightly_smiling_face:

The standard is two spaces but your scripts seem to randomly use from 1 to 4 spaces. Look at the examples in the Script documentation.