Skip to content

ThingSpeak

Once the WattWächter WiFi/USB is reading your electricity meter data, you may want to store and visualize it. Not everyone runs a home automation system that can handle this. In that case, you can upload the data to the ThingSpeak cloud and process it there.

The following guide covers the setup in 3 steps. Additional options for processing and displaying data can be found in steps 4 and 5:

  1. Register with ThingSpeak
  2. Create a channel
  3. Adapt the script
  4. Create bar charts
  5. Export data
  6. ThingView app

Register with ThingSpeak

To create a new account at ThingSpeak, open www.ThingSpeak.com and click the "Get Started For Free" button.

ThingSpeak homepage

On the next page, click "No Account? Create one" to create a new account. Then fill in your personal details and confirm with "Continue".

Create account

Personal details

If the "Personal Email Detected" notice appears, check "Use this email for my MathWorks Account" and confirm again with "Continue".

Personal Email Detected

A verification email will be sent to your address. Click "Verify email" in the email, then click "Continue" to set a password.

Verification

Password

The "Sign-up successful" page appears. After clicking "OK", you will be taken to the ThingSpeak overview page.


Create a new channel

On the ThingSpeak overview page, click the "New Channel" button.

ThingSpeak overview

Enter a name and optionally a description for the channel. Each channel can have up to eight fields. In the following example, four fields were defined for consumption, power, feed-in and current. Click "Save Channel" to save.

Create channel

The channel overview opens with trend charts already created.

Empty channel


Adapt the script

Now the script needs to be adapted so that data is sent to the ThingSpeak cloud. Go to "Tools" => "Edit Script". The following example uses a script for the Hager EHZ361 meter.

Unmodified script

Insert the following code block between >S and >M 1:

>S
if upsecs%30==0
  then
    =>WebSend [api.thingspeak.com] /update.json?api_key=WriteKey&field1=%sml[1]%&field2=%sml[2]%&field3=%sml[3]%&field4=%sml[4]%
endif

The following values need to be adapted:

  • 30 -- How often values are sent to ThingSpeak (in seconds).

    Important

    For the free ThingSpeak account, this value must not be less than 15 seconds.

  • WriteKey -- The write API key for your channel, found in ThingSpeak under "API Keys".

    Write API Key

  • field1-X -- Corresponds to the channel fields created above.

    Channel Fields

  • sml[1-X] -- Corresponds to the lines in the decoding section of the script.

    Script SML lines

The channel fields and script lines must be mapped correctly:

Measurement Channel field Script line
Consumption field1 sml[1]
Power field2 sml[5]
Voltage field3 sml[3]
Current field4 sml[4]

Note

The following example is for illustration only. You will need your own API key and likely a different mapping.

>S
if upsecs%30==0
  then
    =>WebSend [api.thingspeak.com] /update.json?api_key=86OBJRNKSXA9LSOG&field1=%sml[1]%&field2=%sml[5]%&field3=%sml[3]%&field4=%sml[4]%
endif

The complete script in Tasmota looks like this:

Modified script

In the console (Main menu => Tools => Console), you can see a data set being sent to ThingSpeak every 30 seconds:

Console

The data is displayed as charts in ThingSpeak after clicking "Private View":

Configured channel


Create bar charts

To create bar charts showing hourly consumption over the last 24 hours or daily consumption over the last 10 days, you can use the built-in MATLAB functionality.

Bar charts

Click the "MATLAB Visualization" button in your channel.

MATLAB Visualization

In the dialog, keep the default "Custom" setting and click "Create".

Create MATLAB Visualization

In the next dialog, enter a name for the module and paste the MATLAB code. In the first three lines of the code, set the channelID, fieldID and readAPIKey. The field ID must refer to the field storing the consumed kWh (in the example, field 1: consumption).

Paste code

MATLAB code for hourly bars
% ThingSpeak channel parameters
channelID = YOUR_CHANNELID;  % Replace with your Channel ID
fieldID = 1;          % The field with power consumption data
readAPIKey = 'YOUR_API_KEY'; % Replace with your API key

noHours = 24; % Number of hours to display
currentTime = datetime('now', 'TimeZone', 'Europe/Berlin');
currentTime = dateshift(currentTime, 'start', 'hour');
startTime = currentTime - hours(noHours);

hourlyTimestamps = [];
hourlyConsumption = [];

for h = 1:noHours
    hourStart = startTime + hours(h);
    hourEnd = hourStart + hours(1);

    if hourEnd > datetime('now', 'TimeZone', 'Europe/Berlin')
        hourEnd = datetime('now', 'TimeZone', 'Europe/Berlin');
    end

    data = thingSpeakRead(channelID, 'Fields', fieldID, 'DateRange', [hourStart, hourEnd], ...
                          'ReadKey', readAPIKey, 'OutputFormat', 'table');

    if ~isempty(data)
        firstValue = data.(2)(1);
        lastValue = data.(2)(end);
        consumption = lastValue - firstValue;
    else
        consumption = 0;
    end

    hourlyTimestamps = [hourlyTimestamps; hourStart];
    hourlyConsumption = [hourlyConsumption; consumption];
end

figure;
bar(hourlyTimestamps, hourlyConsumption, 'b');
xlabel('Time');
ylabel('Consumption (kWh)');
title(sprintf('Power consumption over the last %d hours', noHours));
grid on;
datetick('x', 'HH:MM', 'keeplimits');
MATLAB code for daily bars
% ThingSpeak channel parameters
channelID = YOUR_CHANNELID;  % Replace with your Channel ID
fieldID = 1;          % The field with power consumption data
readAPIKey = 'YOUR_API_KEY'; % Replace with your API key

noDays = 10; % Number of days to display
currentTime = datetime('now', 'TimeZone', 'Europe/Berlin');
currentTime = dateshift(currentTime, 'start', 'day');
startTime = currentTime - days(noDays);

dailyTimestamps = [];
dailyConsumption = [];

for d = 1:noDays
    dayStart = startTime + days(d);
    dayEnd = dayStart + days(1);

    if dayEnd > datetime('now', 'TimeZone', 'Europe/Berlin')
        dayEnd = datetime('now', 'TimeZone', 'Europe/Berlin');
    end

    data = thingSpeakRead(channelID, 'Fields', fieldID, 'DateRange', [dayStart, dayEnd], ...
                          'ReadKey', readAPIKey, 'OutputFormat', 'table');

    if ~isempty(data)
        firstValue = data.(2)(1);
        lastValue = data.(2)(end);
        consumption = lastValue - firstValue;
    else
        consumption = 0;
    end

    dailyTimestamps = [dailyTimestamps; dayStart];
    dailyConsumption = [dailyConsumption; consumption];
end

figure;
bar(dailyTimestamps, dailyConsumption, 'b');
xlabel('Date');
ylabel('Consumption (kWh)');
title(sprintf('Daily power consumption over the last %d days', noDays));
grid on;
datetick('x', 'dd-mmm', 'keeplimits');

Click "Save and Run" to verify everything works.

Save and Run

Navigate back to your channel via "Channels" => "My Channels".

My Channels

Select your channel and click "Add Visualization" to add the chart to the overview.

Add Visualization

Select the chart and click "Save" to add it to the overview.

Select visualization

The result should look like this:

Result


Export data

To export the data stored in ThingSpeak, e.g. for analysis in Excel, go to the "Data Import / Export" section of your channel. Click "Download" in the "Export" section to download all stored data as a CSV file.

Export data


ThingView app

The ThingSpeak website is accessible worldwide via any browser. For easier access, you can download the paid ThingView app:

On the app's start screen, tap "+" to add a new channel.

ThingView Start

The channel ID can be found in ThingSpeak below the channel name. Since the channel is private by default, disable "Public" and enter the read API key from ThingSpeak (found under "API Keys" => "Read API Keys").

Read API Key

Add channel

After clicking "Search", confirm the channel on the next page. Then select the channel once more.

Confirm channel

Select channel

The trends are now displayed in the app:

ThingView result