Need help with REST or SCRAPE Sensor

Hello,

I’m trying to get a REST sensor to work, now after 3 days of searching, I’m turning to the community for help.

I’m from South Africa where we have load shedding on our power and I want to get some values from a website (https://www.poweralert.co.za/#), I’ve looked at the API and from other discussions, someone mentioned a callback JSON. I have never worked with JSON so I’m clueless.

The link he provided is https://www.poweralert.co.za/PowerAlertAPI/api/PowerAlertForecast/CurrentSystemStatus?callback=maintainCurrentStatus

Which gives result:
/**/ typeof maintainCurrentStatus === ‘function’ && maintainCurrentStatus({“Timestamp”:“2021-03-23T20:00:00”,“Date”:“2021-03-23T00:00:00”,“Hour”:20,“ColorId”:3,“DirectionId”:2,“Color”:“Orange”,“Direction”:“Down”,“DeclaredAvailabilty”:27024.000,“LoadForecast”:26683.000,“MaxAvailability”:30437.529});

I want to get attributes Color, DeclaredAvailabilty, LoadForecast, and MaxAvailability.

My config is as below but when I want to check my configuration.YAML just keeps on processing and doesn’t give me an error or Validation.

Sensor:
  - platform: rest
    name: power_alert
    resource: https://www.poweralert.co.za/PowerAlertAPI/api/PowerAlertForecast/CurrentSystemStatus?callback=maintainCurrentStatus
    value_template: 'OK'
    json_attributes:
      - maintainCurrentStatus
  - platform: template
    sensors:
      power_alert_color:
      value_template: '{{states.sensor.power_alert.attributes["maintainCurrentStatus"]["Color"]}}'

Hope someone can help me, thanks in advance!

1 Like

Unfortunately that is not a JSON response. That URL that you have found generates a JavaScript callback function that a website can then use to display the information. The rest sensor won’t work with this.

Thanks much appreciated, how would I get this info? I have no experience in JavaScript.

I just thought about a possible way out, but it’s not really elegant. You could try to extract the JSON by removing the surrounding JavaScript code. This will store the JSON payload as the sensor’s state, and there is a risk: The sensor’s state can only store up to 255 characters, and the response you currently get is very close.

sensor:
  - platform: rest
    name: power_alert
    resource: https://www.poweralert.co.za/PowerAlertAPI/api/PowerAlertForecast/CurrentSystemStatus?callback=maintainCurrentStatus
    value_template: "{{ value | replace('/**/ typeof maintainCurrentStatus === \\'function\\' && maintainCurrentStatus(', '') | replace(');', '') }}"
  - platform: template
    sensors:
      power_alert_color:
        value_template: '{{ (states.sensor.power_alert.state | from_json)["Color"] }}'

1 Like
- platform: command_line
  name: power_alert_raw
  command: "curl \"https://www.poweralert.co.za/PowerAlertAPI/api/PowerAlertForecast/CurrentSystemStatus?callback=maintainCurrentStatus\""
  value_template: "{{ value.split('(')[1].split(')')[0] }}"

- platform: template
  sensors:
    power_alert_color:
      value_template: "{{ (states('sensor.power_alert_raw')|from_json)['Color'] }}"
    power_alert_declaredavailability:
      value_template: "{{ (states('sensor.power_alert_raw')|from_json)['DeclaredAvailabilty'] }}"
    power_alert_loadforecast:
      value_template: "{{ (states('sensor.power_alert_raw')|from_json)['LoadForecast'] }}"
    power_alert_maxavailability:
      value_template: "{{ (states('sensor.power_alert_raw')|from_json)['MaxAvailability'] }}"

Just beaten to it by @exxamalte, but the above works. Note that the response has a typo in “DeclaredAvailabilty” which briefly caught me out.

The value_template line with the two splits in it is extracting the bit of the response between the brackets, which is then a string that looks like valid JSON.

3 Likes

Thanks man, you are my Hero!

1 Like

Thanks man, you are my Hero!