Hi,
I’m struggling with making a rest sensor with a template payload. I need the POST to fetch the power consumption for the current month, and not the last 31 days.
The query works fine with a fixed parameter of 31, and returns power consumption for the last 31 days.
sensor:
- platform: rest
name: Tibber This Month
resource: https://api.tibber.com/v1-beta/gql
method: POST
payload: '{ "query": "{ viewer { homes { consumption(resolution: DAILY, last: 31) { nodes { from to cost unitPrice unitPriceVAT consumption consumptionUnit }}}}}" }'
json_attributes_path: "$.data.viewer.homes[0].consumption"
json_attributes:
- nodes
value_template: 'Ok'
headers:
Authorization: !secret tibber_token
Content-Type: application/json
User-Agent: REST
It returns:
{
"data":{
"viewer":{
"homes":[
{
"consumption":{
"nodes":[
{
"from":"2021-11-06T00:00:00.000+01:00",
"to":"2021-11-07T00:00:00.000+01:00",
"cost":68.4396865,
"unitPrice":0.98016,
"unitPriceVAT":0.196032,
"consumption":69.825,
"consumptionUnit":"kWh"
},
{
"from":"2021-11-07T00:00:00.000+01:00",
"to":"2021-11-08T00:00:00.000+01:00",
"cost":45.1807827875,
"unitPrice":0.881164,
"unitPriceVAT":0.176233,
"consumption":51.274,
"consumptionUnit":"kWh"
},
{
"from":"2021-11-08T00:00:00.000+01:00",
"to":"2021-11-09T00:00:00.000+01:00",
"cost":78.4728869125,
"unitPrice":1.710059,
"unitPriceVAT":0.342012,
"consumption":45.889,
"consumptionUnit":"kWh"
},
{
"from":"2021-11-09T00:00:00.000+01:00",
"to":"2021-11-10T00:00:00.000+01:00",
"cost":77.3141810875,
"unitPrice":1.047816,
"unitPriceVAT":0.209563,
"consumption":73.786,
"consumptionUnit":"kWh"
},
{
"from":"2021-11-10T00:00:00.000+01:00",
"to":"2021-11-11T00:00:00.000+01:00",
"cost":43.9943054125,
"unitPrice":1.096841,
"unitPriceVAT":0.219368,
"consumption":40.11,
"consumptionUnit":"kWh"
},
{
"from":"2021-11-11T00:00:00.000+01:00",
"to":"2021-11-12T00:00:00.000+01:00",
"cost":54.1913124625,
"unitPrice":1.122973,
"unitPriceVAT":0.224595,
"consumption":48.257,
"consumptionUnit":"kWh"
},
{
"from":"2021-11-12T00:00:00.000+01:00",
"to":"2021-11-13T00:00:00.000+01:00",
"cost":57.560875225,
"unitPrice":1.160127,
"unitPriceVAT":0.232025,
"consumption":49.616,
"consumptionUnit":"kWh"
},
{
"from":"2021-11-13T00:00:00.000+01:00",
"to":"2021-11-14T00:00:00.000+01:00",
"cost":71.8537981375,
"unitPrice":1.158316,
"unitPriceVAT":0.231663,
"consumption":62.033,
"consumptionUnit":"kWh"
},
{
"from":"2021-11-14T00:00:00.000+01:00",
"to":"2021-11-15T00:00:00.000+01:00",
"cost":68.3418974375,
"unitPrice":1.221744,
"unitPriceVAT":0.244349,
"consumption":55.938,
"consumptionUnit":"kWh"
},
{
"from":"2021-11-15T00:00:00.000+01:00",
"to":"2021-11-16T00:00:00.000+01:00",
"cost":77.52910155,
"unitPrice":1.232166,
"unitPriceVAT":0.246433,
"consumption":62.921,
"consumptionUnit":"kWh"
},
{
"from":"2021-11-16T00:00:00.000+01:00",
"to":"2021-11-17T00:00:00.000+01:00",
"cost":74.0528257375,
"unitPrice":1.205346,
"unitPriceVAT":0.241069,
"consumption":61.437,
"consumptionUnit":"kWh"
},
{
"from":"2021-11-17T00:00:00.000+01:00",
"to":"2021-11-18T00:00:00.000+01:00",
"cost":63.9960246625,
"unitPrice":1.125383,
"unitPriceVAT":0.225077,
"consumption":56.866,
"consumptionUnit":"kWh"
},
{
"from":"2021-11-18T00:00:00.000+01:00",
"to":"2021-11-19T00:00:00.000+01:00",
"cost":45.6506907125,
"unitPrice":1.128766,
"unitPriceVAT":0.225753,
"consumption":40.443,
"consumptionUnit":"kWh"
},
{
"from":"2021-11-19T00:00:00.000+01:00",
"to":"2021-11-20T00:00:00.000+01:00",
"cost":43.0527578625,
"unitPrice":1.060935,
"unitPriceVAT":0.212187,
"consumption":40.58,
"consumptionUnit":"kWh"
},
{
"from":"2021-11-20T00:00:00.000+01:00",
"to":"2021-11-21T00:00:00.000+01:00",
"cost":53.608823525,
"unitPrice":1.130392,
"unitPriceVAT":0.226078,
"consumption":47.425,
"consumptionUnit":"kWh"
},
{
"from":"2021-11-21T00:00:00.000+01:00",
"to":"2021-11-22T00:00:00.000+01:00",
"cost":68.1558335125,
"unitPrice":1.229495,
"unitPriceVAT":0.245899,
"consumption":55.434,
"consumptionUnit":"kWh"
},
{
"from":"2021-11-22T00:00:00.000+01:00",
"to":"2021-11-23T00:00:00.000+01:00",
"cost":86.55705785,
"unitPrice":1.448629,
"unitPriceVAT":0.289726,
"consumption":59.751,
"consumptionUnit":"kWh"
},
{
"from":"2021-11-23T00:00:00.000+01:00",
"to":"2021-11-24T00:00:00.000+01:00",
"cost":87.7003419125,
"unitPrice":1.304017,
"unitPriceVAT":0.260803,
"consumption":67.254,
"consumptionUnit":"kWh"
},
{
"from":"2021-11-24T00:00:00.000+01:00",
"to":"2021-11-25T00:00:00.000+01:00",
"cost":72.8653619875,
"unitPrice":1.374325,
"unitPriceVAT":0.274865,
"consumption":53.019,
"consumptionUnit":"kWh"
},
{
"from":"2021-11-25T00:00:00.000+01:00",
"to":"2021-11-26T00:00:00.000+01:00",
"cost":82.81616345,
"unitPrice":1.434121,
"unitPriceVAT":0.286824,
"consumption":57.747,
"consumptionUnit":"kWh"
},
{
"from":"2021-11-26T00:00:00.000+01:00",
"to":"2021-11-27T00:00:00.000+01:00",
"cost":74.0476812,
"unitPrice":1.488007,
"unitPriceVAT":0.297601,
"consumption":49.763,
"consumptionUnit":"kWh"
},
{
"from":"2021-11-27T00:00:00.000+01:00",
"to":"2021-11-28T00:00:00.000+01:00",
"cost":99.7744653,
"unitPrice":1.950206,
"unitPriceVAT":0.390041,
"consumption":51.161,
"consumptionUnit":"kWh"
},
{
"from":"2021-11-28T00:00:00.000+01:00",
"to":"2021-11-29T00:00:00.000+01:00",
"cost":106.976069475,
"unitPrice":2.395398,
"unitPriceVAT":0.47908,
"consumption":44.659,
"consumptionUnit":"kWh"
},
{
"from":"2021-11-29T00:00:00.000+01:00",
"to":"2021-11-30T00:00:00.000+01:00",
"cost":143.3477653375,
"unitPrice":2.984174,
"unitPriceVAT":0.596835,
"consumption":48.036,
"consumptionUnit":"kWh"
},
{
"from":"2021-11-30T00:00:00.000+01:00",
"to":"2021-12-01T00:00:00.000+01:00",
"cost":114.7596663375,
"unitPrice":1.655004,
"unitPriceVAT":0.331001,
"consumption":69.341,
"consumptionUnit":"kWh"
},
{
"from":"2021-12-01T00:00:00.000+01:00",
"to":"2021-12-02T00:00:00.000+01:00",
"cost":97.4594623875,
"unitPrice":1.484802,
"unitPriceVAT":0.29696,
"consumption":65.638,
"consumptionUnit":"kWh"
},
{
"from":"2021-12-02T00:00:00.000+01:00",
"to":"2021-12-03T00:00:00.000+01:00",
"cost":110.122295375,
"unitPrice":2.129037,
"unitPriceVAT":0.425807,
"consumption":51.724,
"consumptionUnit":"kWh"
},
{
"from":"2021-12-03T00:00:00.000+01:00",
"to":"2021-12-04T00:00:00.000+01:00",
"cost":150.9669601,
"unitPrice":1.854768,
"unitPriceVAT":0.370954,
"consumption":81.394,
"consumptionUnit":"kWh"
},
{
"from":"2021-12-04T00:00:00.000+01:00",
"to":"2021-12-05T00:00:00.000+01:00",
"cost":108.9824185625,
"unitPrice":1.722116,
"unitPriceVAT":0.344423,
"consumption":63.284,
"consumptionUnit":"kWh"
},
{
"from":"2021-12-05T00:00:00.000+01:00",
"to":"2021-12-06T00:00:00.000+01:00",
"cost":110.660477,
"unitPrice":1.59359,
"unitPriceVAT":0.318718,
"consumption":69.441,
"consumptionUnit":"kWh"
},
{
"from":"2021-12-06T00:00:00.000+01:00",
"to":"2021-12-07T00:00:00.000+01:00",
"cost":110.5067917125,
"unitPrice":2.064351,
"unitPriceVAT":0.41287,
"consumption":53.531,
"consumptionUnit":"kWh"
}
]
}
}
]
}
}
}
But when i try to get data for the current month, it fails:
sensor:
- platform: rest
name: Tibber This Month
resource: https://api.tibber.com/v1-beta/gql
method: POST
payload: '{ "query": "{ viewer { homes { consumption(resolution: DAILY, last: {{ (now().day-1) }}) { nodes { from to cost unitPrice unitPriceVAT consumption consumptionUnit }}}}}" }'
json_attributes_path: "$.data.viewer.homes[0].consumption"
json_attributes:
- nodes
value_template: 'Ok'
headers:
Authorization: !secret tibber_token
Content-Type: application/json
User-Agent: REST
This is shown in the log:
> 2021-12-07 12:20:42 DEBUG (MainThread) [homeassistant.components.rest.sensor] Data fetched from resource: {"error":"syntax error"}
After reading several threads on the forum it seems like it should be possible to use templates in the payload. After testing with several template values, it seems like they are working, but that I’m probably have a fault in my template.
According to the Tibber documentation the last parameter should be an int. When I try:
payload: '{ "query": "{ viewer { homes { consumption(resolution: DAILY, last: {{ (now().day-1)|int }}) { nodes { from to cost unitPrice unitPriceVAT consumption consumptionUnit }}}}}" }'
I get a different reply:
> 2021-12-07 12:26:00 DEBUG (MainThread) [homeassistant.components.rest.sensor] Data fetched
from resource: {"errors":[{"message":"Syntax Error: Expected Name, found {","locations":[{"line":1,"column":58}],"extensions":{"code":"GRAPHQL_PARSE_FAILED"}}]}
For another forum post it was recomended to use double quotes,
payload: '{ "query": "{ viewer { homes { consumption(resolution: DAILY, last: "{{ (now().day-1)|int }}") { nodes { from to cost unitPrice unitPriceVAT consumption consumptionUnit }}}}}" }'
but then I get:
> 2021-12-07 12:36:01 DEBUG (MainThread) [homeassistant.components.rest.sensor] Data fetched from resource: {"error":"syntax error"}
I have also tried to slice the reply into the attributes without sucsess, it seems like the json_attributes doesn’t accept templates?
json_attributes: '{{ value_json.data.viewer.homes[0].consumption.nodes[-(now().day-1)]}}'
Not sure if this is a Tibber API or rest sensor/template question. Do anyone see what I’m doing wrong? Any hints on how I can proceed to get this working?