+ Antworten
Ergebnis 1 bis 9 von 9
  1. #1
    Registriert seit
    Sep 2017
    Ort
    Tromsø, Norway
    Beiträge
    22

    Standard Script for API for energy consumption smart meter.

    Hi Symcon experts,

    First of all thanks for a great forum, even if I have to use Google Translate to understand anything :-)

    I am a new user of the Symcon software and have next to no programming experience so it has been a steep learning curve.
    Mostly I have copy/pasted PHP scripts for my setup from the forum and other places.

    I would really appriciate some help with integrating my automatic smart meter into Symcon.
    In Norway we last year got smart home energy meters with automatic reading and a HAN interface for extracting data.
    I have selected the Norwegian company Tibber for getting the data to the cloud since they supplied an API. Tibber Developer
    It is possible to get all energy consumption data, energy prices etc.

    I have found a basic PHP script for sending queries to the API but I do not know how I can write the values I get into variables for use in Symcon.
    Could anyone please help with this?

    This script is just for getting the hourly energy price:

    PHP-Code:
    $json '{"query":"{viewer {homes {currentSubscription {priceInfo {current {total energy tax startsAt }}}}}}"}';

    # Create a connection
    $ch curl_init('https://api.tibber.com/v1-beta/gql');
    # Setting our options
    curl_setopt($chCURLOPT_URL'https://api.tibber.com/v1-beta/gql');
    curl_setopt($chCURLOPT_HTTPHEADER
       array(
    'Content-Type: application/json',  
       
    'Authorization: Bearer d1007ead2dc84a2b82f0de19451c5fb22112f7ae11d19bf2bedb224a003ff74a')); // Demo token
    curl_setopt($chCURLOPT_POSTtrue);
    curl_setopt($chCURLOPT_POSTFIELDS$json);
    curl_setopt($chCURLOPT_RETURNTRANSFERtrue);

    # Get the response
    $response curl_exec($ch);
    curl_close($ch);

    echo 
    '<pre>';
    echo 
    $json.'<br>';
    echo 
    json_encode(json_decode($response), JSON_PRETTY_PRINT);
    ?> 

    Best regards
    Andreas

  2. #2
    Registriert seit
    Oct 2011
    Ort
    Hattersheim (Hessen)
    Beiträge
    6,530

    Zitat Zitat von dreeas Beitrag anzeigen
    I have found a basic PHP script for sending queries to the API but I do not know how I can write the values I get into variables for use in Symcon.
    If you send a curl request to Tibber what is the output of
    PHP-Code:
    var_export($response); 
    You get data in JSON. First you have to create the variable in Symcon for the value you want to save.
    After that you can get the data as described How can I ... 2.0.

    If you post the content here from $response (var_export) you can get help, if you have problems to read out individual values.

  3. #3
    Registriert seit
    Oct 2011
    Ort
    Hattersheim (Hessen)
    Beiträge
    6,530

    Zitat Zitat von dreeas Beitrag anzeigen

    This script is just for getting the hourly energy price:

    PHP-Code:
    $json '{"query":"{viewer {homes {currentSubscription {priceInfo {current {total energy tax startsAt }}}}}}"}';

    # Create a connection
    $ch curl_init('https://api.tibber.com/v1-beta/gql');
    # Setting our options
    curl_setopt($chCURLOPT_URL'https://api.tibber.com/v1-beta/gql');
    curl_setopt($chCURLOPT_HTTPHEADER
       array(
    'Content-Type: application/json',  
       
    'Authorization: Bearer d1007ead2dc84a2b82f0de19451c5fb22112f7ae11d19bf2bedb224a003ff74a')); // Demo token
    curl_setopt($chCURLOPT_POSTtrue);
    curl_setopt($chCURLOPT_POSTFIELDS$json);
    curl_setopt($chCURLOPT_RETURNTRANSFERtrue);

    # Get the response
    $response curl_exec($ch);
    curl_close($ch);

    ?> 
    For this example, you get the values with

    PHP-Code:
    $data json_decode($response);

    $current $data->data->viewer->homes[0];

    $total $current->currentSubscription->priceInfo->current->total;
    var_dump($total);

    $energy $current->currentSubscription->priceInfo->current->energy;
    var_dump($energy);

    $tax $current->currentSubscription->priceInfo->current->tax;
    var_dump($tax); 
    Then write the values to a variable with SetValue.

  4. #4
    Registriert seit
    Sep 2017
    Ort
    Tromsø, Norway
    Beiträge
    22

    Thank you so much for your quick reply.
    That worked perfect :-)

    Later this week I will get access to my full energy consumption with value updates every 2 seconds as well.
    I might come back and ask a few more questions then.

    Thanks again.

  5. #5
    Registriert seit
    Sep 2017
    Ort
    Tromsø, Norway
    Beiträge
    22

    Hi again,

    I do now have access to a "subscription" with live consumption from the Tibber API over a websocket.
    The address apparently is: wss://api.tibber.com/v1-beta/gql/subscriptions

    This is from the Tibber developer page:

    "
    While queries and mutations are typically handled over HTTP some data are better suited to handled as streams over a persistent connection (web socket). Subscriptions follows the same semantics as queries and mutations - the shape of what is returned/sent from the server is determined by the client when invoking the api:

    subscription{
    liveMeasurement(homeId:"195f75d5-9d10-472b-b78b-3976904a01fb"){
    timestamp
    power
    accumulatedConsumption
    accumulatedCost
    currency
    minPower
    averagePower
    maxPower
    }
    }

    "


    Could anyone explain how I can set that up? Do I use the Webhook module? Also how the script should look for getting live data from the API?

  6. #6
    Registriert seit
    Oct 2011
    Ort
    Hattersheim (Hessen)
    Beiträge
    6,530

    Zitat Zitat von dreeas Beitrag anzeigen
    Could anyone explain how I can set that up?
    This is not so easy because Websockets are not natively supported by Symcon at the moment, so you have to use a PHP Module like WebSocketClient for IP-Symcon.

    Zitat Zitat von dreeas Beitrag anzeigen
    Do I use the Webhook module?
    No, Webhook would only work if the device would send activity data to an URL.
    Zitat Zitat von dreeas Beitrag anzeigen
    Also how the script should look for getting live data from the API?
    This depends on what the data exactly looks like. Perhaps you can try first to set up a web socket and then see what data is sent to Symcon. If you know what data is received in Symcon then you could get help in what the script should look like.

    Another possibility would be to get the data from the device via a timer and a curl request (see above). It depends on if you need the data in realtime or if it is enough to get the data in a defined interval..

  7. #7
    Registriert seit
    Sep 2017
    Ort
    Tromsø, Norway
    Beiträge
    22

    Zitat Zitat von Fonzo Beitrag anzeigen
    This depends on what the data exactly looks like. Perhaps you can try first to set up a web socket and then see what data is sent to Symcon. If you know what data is received in Symcon then you could get help in what the script should look like
    I don't quite see where in the Websocket client a script goes?
    In the Tibber API developer webpage there is an API explorer where queries can be made.
    The "live subscription" is entered like this:

    PHP-Code:
    subscription{
      
    liveMeasurement(homeId:"195f75d5-9d10-472b-b78b-397690******"){
        
    timestamp
        power
        accumulatedConsumption
        accumulatedCost
        currency
        minPower
        averagePower
        maxPower
      
    }

    And the data received looks like this:

    PHP-Code:
    {
      
    "liveMeasurement": {
        
    "timestamp""2019-06-25T22:24:00.000+02:00",
        
    "power"6882,
        
    "accumulatedConsumption"38.304167,
        
    "accumulatedCost"14.621026,
        
    "currency""NOK",
        
    "minPower"313,
        
    "averagePower"1709.8,
        
    "maxPower"11329
      
    }

    The Client seems to connect OK as I see in the debug that I receive data in HEX format.
    But I am not sure how I use this client correctly.

  8. #8
    Registriert seit
    Sep 2017
    Ort
    Tromsø, Norway
    Beiträge
    22

    Hi,

    I still have not been able to use the websocket client to read live data from the server.
    Have anyone else used this module before and could assist in making a script to use it?
    Or have a demo script that could point me in the right direction?

    The module is setup to the server but keeps losing connection once a minute.
    Will Symcon come out with native support for Websocket in the near future?

    Br
    Andreas

  9. #9
    Registriert seit
    Sep 2017
    Ort
    Tromsø, Norway
    Beiträge
    22

    Good morning people,

    I'll give my Websocket issue another try.
    I still struggle to setup my connection towards the Tibber API Tibber Developer.

    When opening the websocket it keeps reconnecting all the time creating fault messages.
    Could anyone help and see my logs what could be wrong? I have attached the debug from the websocket and the client socket and also message log.

    Just as a side note, I had never even heard of a websocket before starting this so please in small words :-)
    Angehängte Dateien Angehängte Dateien

Ähnliche Themen

  1. Gubino 3 phasen smart meter
    Von spx im Forum Z-Wave
    Antworten: 14
    Letzter Beitrag: 17.10.19, 15:35
  2. SMA Energy Meter
    Von rene im Forum Allgemeine Diskussion (2.x/3.x)
    Antworten: 68
    Letzter Beitrag: 17.10.19, 06:19
  3. Smart Meter - Discovergy Erfahrungen?
    Von Hollowman im Forum Off Topic
    Antworten: 6
    Letzter Beitrag: 29.04.19, 10:05
  4. B-Control Smart Meter
    Von jgoller im Forum Haustechnik
    Antworten: 2
    Letzter Beitrag: 06.07.16, 11:53
  5. Aeon Labs Home Energy Meter
    Von Marko im Forum Z-Wave
    Antworten: 4
    Letzter Beitrag: 17.01.13, 14:55