How to write a rest sensor (or Python script) for this data in GraphQL query format

on Notion – The all-in-one workspace for your notes, tasks, wikis, and databases.

it states we should create aPOST call to https://graphcdn.frankenergie.nl/ and use a ‘GraphQL query’ (?)

query MarketPrices {
	marketPricesElectricity(startDate: "2021-10-01", endDate: "2021-10-30") {
    till
    from
    marketPrice
    priceIncludingMarkup
	}
	marketPricesGas(startDate: "2021-10-01", endDate: "2021-10-30") {
    from
    till
    marketPrice
    priceIncludingMarkup
  }
}

I have something similar for the easyenergy api, but that is very unreliable and throws many hundreds (thousands) of errors:

rest:

  - resource_template: >
      https://mijn.easyenergy.com/nl/api/tariff/getapxtariffs?startTimestamp={{now().strftime('%Y-%m-%d')}}&endTimestamp={{(now()+ timedelta(days=2)).strftime('%Y-%m-%d')}}

translates to

https://mijn.easyenergy.com/nl/api/tariff/getapxtariffs?startTimestamp=2022-10-13&endTimestamp=2022-10-15

which should be fine mostly, but shows

<Error>
<Message>An error has occurred.</Message>
</Error>

in the browser, and

Logger: homeassistant.components.rest.sensor
Source: components/rest/sensor.py:170 
Integration: RESTful (documentation, issues) 
First occurred: October 11, 2022, 22:36:05 (3912 occurrences) 
Last logged: 07:43:23

JSON result was not a dictionary or list with 0th element a dictionary

because of that in the HA log…

so I hope to fix that with the reversed.notion data (powered by Frenk )

please have a look how to re-write that?

btw, here’s Get Frank Energie prices with Python and GraphQL a python solution we could use maybe?

And a Tibber variant on RESTful Sensor with GraphQL payload - #12 by magnus.overli

- platform: rest
  name: Tibber prices
  resource: https://api.tibber.com/v1-beta/gql
  method: POST
  payload: '{ “query”: “{ viewer { homes { currentSubscription { priceInfo { today { total startsAt }}}}}}” }'
  json_attributes_path: "$.data.viewer.homes[0].currentSubscription.priceInfo"
  json_attributes: 
    - today
  value_template: Ok
  headers:
    Authorization: !secret tibber_token
    Content-Type: application/json
    User-Agent: REST

since we dont need authentication, I tried some basic translation to the Frank format:

sensor:

  - platform: rest
    resource: https://graphcdn.frankenergie.nl
    name: Frank energie prijzen
    method: POST
    params:
      startDate: >
        {{now().strftime('%Y-%m-%d')}}
      endDate: >
        {{(now()+ timedelta(days=2)).strftime('%Y-%m-%d')}}
    payload: >
      {
       "query": "query MarketPrices {\n\tmarketPricesElectricity(startDate: startDate, endDate: endDate)
       {\n    till\n    from\n    marketPrice\n    priceIncludingMarkup\n\t}\n\tmarketPricesGas(startDate: startDate, endDate: endDate)
        {\n    from\n    till\n    marketPrice\n    priceIncludingMarkup\n  }\n}"
      }

dont get an error, but no result either :wink:

edit

using:

  - platform: rest
    resource: https://graphcdn.frankenergie.nl
    name: Frenk energie prijzen
    method: POST
#     params:
#       startDate: >
#         {{(now().strftime('%Y-%m-%d')}}
#       endDate: >
#         {{(now()+ timedelta(days=+1)).strftime('%Y-%m-%d')}}
    payload: >-
      {
          "query": "query MarketPrices {\n\tmarketPricesElectricity(startDate: \"2021-10-01\", endDate: \"2021-10-30\") {\n    till\n    from\n    marketPrice\n    priceIncludingMarkup\n\t}\n\tmarketPricesGas(startDate: \"2021-10-01\", endDate: \"2021-10-30\") {\n    from\n    till\n    marketPrice\n    priceIncludingMarkup\n  }\n}"
      }

which is the straight copy of the linked page, I have some progress… it now returns:

homeassistant.exceptions.InvalidStateError: Invalid state encountered for entity ID: sensor.frenk_energie_prijzen. State max length is 255 characters.

meaning there is data being collected, but its too much and I should have that in the attributes. But how can I check a path for it if we dont see it anywhere in the logs.

need to add json_attributes, and json_attributes_path ?

Did you ever solve this. I got it working in C++ yesterday so if you need help let me know