Good Morning,
Iam failing to scrape a very simple website
(its about reading values from a BHKW dachs senertec)
Basically it has a webservice like
http://192.168.1.218:8080/getKey?k=Hka_Mw1.sWirkleistung
with auth
the result ist straight
Hka_Mw1.sWirkleistung=5.534
No div, body, nothing
Any ideas how I can get the 5.534 in a sensor (without hardcoding it ) ?
I tried rest and scrape sensor
-
Rest did return the result but I got a state that is Hka_Mw1.sWirkleistung=5.534 and Iam not able to work with that
-
Scrape results in error I tried to scrape for Hka_Mw1.sWirkleistung or body, pre but did not work.
Hope someone can help me
tom_l
2
You can remove everything but the value fairly easily with the value_template and regex or just the function replace().
What was your rest sensor config?
Thank you for your fast reply it was pretty basic Iam not very good working with value templates and regexp, but I hope I can learn a bit from you
the sensor looked like
which results in the sensor bhkw Test state:
Hka_Mw1.sWirkleistung=5.523
or more values
which results in the state:
Hka_Mw1.sWirkleistung=5.523
Hka_Mw1.ulMotorlaufsekunden=4800
tom_l
4
Please have a read of point 11 here: How to help us help you - or How to ask a good question
then edit your post.
But this should do it:
- platform: rest
name: BHKW Test
resource: http://192.168.1.218:8080/getKey?k=Hka_Mw1.sWirkleistung
headers:
authorization: Basic Z2x0Og==
value_template: "{{ value | replace('Hka_Mw1.sWirkleistung=', '') }}"
You could also use this value template:
value_template: "{{ value | regex_findall_index('\-?\d+\.?\d+') }}"
Sorry for the format and thank you very much for your help. It works perfect for one value !
Sorry last question do you see any options how could I extract multiple values at once and add this as atributes ?
e.g.
name: BHKW Test
resource: http://192.168.1.218:8080/getKey?k=Hka_Mw1.sWirkleistung&k=Hka_Mw1.ulMotorlaufsekunden
headers:
authorization: Basic Z2x0Og==
value_template: "{{ value | replace('Hka_Mw1.sWirkleistung=', '') }}"
Results in
Hka_Mw1.ulMotorlaufsekunden=3.486
Hka_Mw1.sWirkleistung=5.528
I checked https://www.home-assistant.io/docs/configuration/templating/ and found the reg exp but I am unsure about placing multiple statements and adding them to value atributes. If you have articles to read Iam also happy to try it myself
tom_l
6
The restful sensor does not support attribute templates so you will have to make two sensors.
First one:
- platform: rest
name: 'BHKW Test 1'
resource: http://192.168.1.218:8080/getKey?k=Hka_Mw1.sWirkleistung&k=Hka_Mw1.ulMotorlaufsekunden
headers:
authorization: Basic Z2x0Og==
value_template: "{{ value | regex_findall_index('\-?\d+\.?\d+', index=0) }}"
Second one:
- platform: rest
name: 'BHKW Test 2'
resource: http://192.168.1.218:8080/getKey?k=Hka_Mw1.sWirkleistung&k=Hka_Mw1.ulMotorlaufsekunden
headers:
authorization: Basic Z2x0Og==
value_template: "{{ value | regex_findall_index('\-?\d+\.?\d+', index=1) }}"
thank you very much for your time ! It was a great help and I was able to learn something