Scraper Sensor combine with Template Sensor

Hello

I have a contact sensor from mobile alerts (technoline). There is no real API but from a webpage I could scrape the state value “Open”/“Closed”. This works very well. It looks like this:

- platform: scrape
    name: MyName
    resource: The URL
    select: "td:nth-of-type(2)"

In an entity card I can display the value. Now I want to change the symbol/icon (like the switches) depending on the state. I think I need the sensor template. But I dont understand how to combine the scrape sensor with the template sensor.

Thanks for the help.

Gilbert

it would look something like this:

- platform: template
  sensors:
    my_sensor:
      entity_id: sensor.myname
      value_template: '{% if states("sensor.myname") == "Open"%}on{% else %}off{% endif %}'
      friendly_name: My Sensor
      device_class: window # or other class for the iron to change

Check this for more info on device_class:

Hi lolouk44
Thanks for your help. This is exactly what I did. But it doesn’t work. Here my real code:

sensor:
  - platform: scrape
    name: Wäscheküche Fenster
    resource: http://.......
    select: "td:nth-of-type(2)"
  - platform: scrape
    name: Kinder Spielzimmer Süd
    resource: http://.......
    select: "td:nth-of-type(2)"

binary_sensor:
  - platform: template
    sensors:
      my_sensor:
        entity_id: sensor.waschekuche_fenster
        value_template: '{% if states("sensor.waschekuche_fenster") == "Open"%}on{% else %}off{% endif %}'
        friendly_name: Wäscheküche Fenster
        device_class: window # or other class for the iron to change
      my_sensor2:
        entity_id: sensor.kinder_spielzimmer_sud
        value_template: '{% if states("sensor.kinder_spielzimmer_sud") == "Open"%}on{% else %}off{% endif %}'
        friendly_name: Kinder Spielzimmer
        device_class: window # or other class for the iron to change

Sensor1 (Wäscheküche Fenster)=Open
Sensor2 (Kinder Spielzimmer Süd)=Closed.
But the binary sensors have always the state “Closed”. What did I wrong?

Thanks again for the help
Gilbert

that’s most likely because your sensor does not return “Open” but something else.
Try the templates editor and when you know a sensor is closed, enter this and see if it says true:
{{ states("sensor.kinder_spielzimmer_sud") == "Closed" }}

Here an example of two contact sensors. One is open, the other is closed.

The code in the template editor:

Sensor1: {{ states("sensor.waschekuche_fenster") }}
Sensor2: {{ states("sensor.schlafzimmer_fenster") }}

Sensor1: {{ states("sensor.waschekuche_fenster") == "Closed" }}
Sensor2: {{ states("sensor.schlafzimmer_fenster") == "Closed" }}

Sensor1: {% if states("sensor.waschekuche_fenster") == "Open"%}on{% else %}off{% endif %}
Sensor2: {% if states("sensor.schlafzimmer_fenster") == "Open"%}on{% else %}off{% endif %}

The result is:

Sensor1: Closed
Sensor2: Open

Sensor1: True
Sensor2: False

Sensor1: off
Sensor2: on

So I think the first sensor which I used was broken. The other sensors are working well. Thanks for your help!

1 Like

In the template editor I have this 3 lines of code:

{% if states("sensor.kuche_fenster") == "Open"%}on{% else %}off{% endif %}
{% if states("sensor.waschekuche_fenster") == "Open"%}on{% else %}off{% endif %}
{% if states("sensor.schlafzimmer_fenster") == "Open"%}on{% else %}off{% endif %}

The result is:

on
off
off

Then I create the binary_sensors:

binary_sensor:
  - platform: template
    sensors:
      contact_kueche_window:
        entity_id: sensor.kuche_fenster
        value_template: '{% if states("sensor.kuche_fenster") == "Open"%}on{% else %}off{% endif %}'
        friendly_name: Küche Fenster
        device_class: window
  - platform: template
    sensors:
      contact_waeschekueche_window:
        entity_id: sensor.waschekuche_fenster
        value_template: '{% if states("sensor.waschekuche_fenster") == "Open"%}on{% else %}off{% endif %}'
        friendly_name: Wäscheküche Fenster
        device_class: opening
  - platform: template
    sensors:
      contact_schlafzimmer_window:
        entity_id: sensor.schlafzimmer_fenster
        value_template: '{% if states("sensor.schlafzimmer_fenster") == "Open"%}on{% else %}off{% endif %}'
        friendly_name: Schlafzimmer Fenster
        device_class: moving

In lovelace I add an entity it looks:

entities:
  - entity: binary_sensor.contact_kueche_window
  - entity: binary_sensor.contact_waeschekueche_window
  - entity: binary_sensor.contact_schlafzimmer_window
show_header_toggle: false
title: Window Contact Sensors
type: entities

The result in lovelace is:

Closed
Closed
off

Can someone explain why?

Thanks
Gilbert

Your 3rd sensor has device_class: moving maybe?

Changed, does not help.

What did you change it to?
Also what should be the status?

I have changed for all 3 sensors device_class=moving
The result is now: I always get “off” as result. Even the window is open.

But in the template editor: the result is correct. It is on if the window are open and off if the window is colsed.

So the conclusion: The results in the template editor are correct. But if I copy this line into the binary_sensor value_template, the result is always off.

they are windows, why are you setting device_class: moving?
You should really use device_class: window or device_class: opening
what I can’t get my head round is this: if the template editor shows you the correct results, then assigning that code to the value_template should give you the same info.

{% if states("sensor.schlafzimmer_fenster") == "Open"%}on{% else %}off{% endif %}
{% if states("sensor.schlafzimmer_fenster") == "Closed"%}off{% else %}on{% endif %}

This should give you the same results

Only thing that could potentially be an issue would be your scrape sensor catching trailing spaces which means the value could be Open (with an invisible space)
So you could try something like this to double check:

{% if states("sensor.schlafzimmer_fenster") | trim == "Open"%}on{% else %}off{% endif %}
{% if states("sensor.schlafzimmer_fenster") | trim  == "Closed"%}off{% else %}on{% endif %}

This should give you the same results

Hi lolouk40, I will have time on weekend to check again and do some other tests. First I need to check why my database becomes so fast so big.

Hi lolouk44

I have tried your solution. I am sorry, it doesnt work. Same result like before.
I have tested:

  • your code
  • deleted everything in the configuration.yaml file except this 4 test sensors
  • i have installed the sqlite plugin to read the database: the state value is “Open” or “Closed”. No spaces.
  • And it is always the same. It works in the template editor, but not in real.

An interesting hint: If I add this simple binary_sensor, I have the same problem. Correct value in the template editor, but always the value “off” in real.

binary_sensor:
  - platform: template
    sensors:
      sun_up:
        friendly_name: "Sun is up"
        value_template: "{{ states.sun.sun.attributes.elevation}}"

So it looks that something with my template engine is not working. But only with the binary_sensors. For the entity “sensor” the templates work.

A basic question, what is correct?

binary_sensor:
  - platform: template
    sensors:
      sun_up1:
        value_template: "{{ states.sun.sun.attributes.elevation}}"
      sun_up2:
        value_template: "{{ states.sun.sun.attributes.elevation}}"
      sun_up3:
        value_template: "{{ states.sun.sun.attributes.elevation}}"

or

binary_sensor:
  - platform: template
    sensors:
      sun_up1:
        value_template: "{{ states.sun.sun.attributes.elevation}}"
  - platform: template
    sensors:
      sun_up2:
        value_template: "{{ states.sun.sun.attributes.elevation}}"
  - platform: template
    sensors:
      sun_up3:
        value_template: "{{ states.sun.sun.attributes.elevation}}"

First bit is better, takes less space. Other than that it achieves the same

For your sensor let’s try something different. It’s a binary sensor si can only have on or off state. Therfore value try value template life this and let me know
{{ states("sensor.schlafzimmer_fenster") | trim == "Open"}}

Hi lolouk44: This was the solution! It works perfect now. Thanks very much.

Solution with binary_sensor:

binary_sensor:
      - platform: template
        sensors:
          contact_kueche_window_friendly:
            entity_id: sensor.kuche_fenster
            value_template: '{{ states("sensor.kuche_fenster") | trim == "Open"}}'
            friendly_name: Küche
            device_class: window

The solution with sensor (does not highlight the icon if open):

sensor:
      - platform: template
        sensors:
          contact_kueche_window_friendly:
            value_template: '{{ states("sensor.kuche_fenster") }}'
            friendly_name: Küche
            icon_template: "{% if is_state('sensor.kuche_fenster', 'Open') %}mdi:window-open{% else %}mdi:window-closed{% endif %}"