Help with Scrape Sensor :)

Hi,
Can anybody help me with a scrape sensor I am trying to enable.
It is for the public transport, believe it or not, they don’t have a working public API… need to rely on a scrape sensor.

site is the flowing:
http://www.citram.es/HORARIOS/info.aspx?estacion=8_12201

  - platform: scrape
    resource: http://www.citram.es/HORARIOS/info.aspx?estacion=8_12222
    select: '#dtgHorasPorLinea > tbody > tr:nth-child(1) > td.Minutes_css'
    index: 3
    name: autobus casa 1 time
    scan_interval: 60
    headers:
      User-Agent: Mozilla/5.0

That’s not going to work, sadly. The web site is dynamically-generated with Ajax. If you use View Source in your browser (Ctrl-U in Chrome) you’ll see that the data simply isn’t there to be read by the scrape sensor.

It pulls the data in from the response to a POST request to http://www.citram.es/HORARIOS/infoM.aspx?estacion=8_12201 (note the extra “M”) but there a complex VIEWSTATE parameter in the request which you may not be able to pull out.

Ill try to mess with postman app to see the Requests…
Didn’t notice the extra M at the beginning.
Thanks!

Can you help me with this other one?


  - platform: scrape
    resource: https://zzzzzz
    select: "div > div:nth-child(1) > div > div.PollenBreakdown--outlookContainer--3Jjts > ul > li:nth-child(1) > strong"

    name: zzzzzzz
    scan_interval: 310

This sensor returns me a “Alto” Value with is ok, but I want to map the values to numbers so I can graph them, I tried this but of course is not working:


  - platform: scrape
    resource: https://zzzzz
    select: "div > div:nth-child(1) > div > div.PollenBreakdown--outlookContainer--3Jjts > ul > li:nth-child(1) > strong"
    value_template: >
          {% if is_state("value", "Ninguno") %}
            "0"
          {% elif is_state("value", "Muy bajo") %}
            "1"
          {% elif is_state("value", "Bajo") %}
            "2"
          {% elif is_state("value", "Moderado") %}
            "3"
          {% elif is_state("value", "Alto") %}
            "4"
          {% elif is_state("value", "Muy alto") %}
            "5"
          {% elif is_state("value", "unavailable") %}
            false
          {% else %}
            true
          {%- endif %}
    name: zzzzz
    scan_interval: 310

Wrong use of is_state(), and value should not be in quotes. Try this instead (and repeat for your other tests):

{% if value == "Alto" %}
   0

Or if you want to get more advanced:

value_template: >
    {% set values = ['Ninguno', 'Muy bajo', 'Bajo', 'Moderado', 'Alto', 'Muy alto'] %}
    {% if value in values %}
      {{ values.index(value) }}
    {% elif value == 'unavailable' %}
      false
    {% else %}
      true
    {% endif %}
1 Like