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.
- 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
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?
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" }}
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 %}
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.
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"}}