alnavasa
(A)
December 30, 2020, 2:06pm
1
I have a scrape sensor that outputs:
Generado el 30/12/2020 a las 14:56
In the value template I added
value_template: '{{ value.lstrip("Generado el ") }}'
Which outputs:
30/12/2020 a las 14:56
If I want to remove the “a las” I would need to use:
value_template: '{{ value.replace(" a las ", " ")}}'
How do I combine both?
Is there a better way to output it as a time reference so I can create with math a “how long ago was it generated” template sensor
You could replace both parts in one step and then convert it to a date object.
{% set value = 'Generado el 30/12/2020 a las 14:56' %}
{% set valueStripped = value.replace("Generado el ", "").replace(" a las", "") %}
{% set valueTimestamp = strptime(valueStripped, '%d/%m/%Y %H:%M') %}
alnavasa
(A)
December 30, 2020, 2:52pm
3
Cant get it to work
- platform: scrape
resource: https://xxxxxx
select: "#hoy > div:nth-child(1) > section > div > div.container.parte-dia-header > p"
value_template: '{% set valueStripped = value.replace("Generado el ", "").replace(" a las", "") %}{% set valueTimestamp = strptime(valueStripped, '%d/%m/%Y %H:%M') %}'
name: sierra nevada last update
scan_interval: 320
alnavasa
(A)
December 30, 2020, 5:17pm
4
This one gives no value :S
- platform: scrape
resource: https://xxxxxx
select: "#hoy > div:nth-child(1) > section > div > div.container.parte-dia-header > p"
value_template: >
{% set valueStripped = value.replace("Generado el ", "").replace(" a las", "") %}
{% set valueTimestamp = strptime(valueStripped, '%d/%m/%Y %H:%M') %}
name: sierra nevada last update
scan_interval: 320
You template needs to be like this.
value_template: >
{% set valueStripped = value.replace("Generado el ", "").replace(" a las", "") %}
{% set valueTimestamp = strptime(valueStripped, '%d/%m/%Y %H:%M') %}
{{ valueTimestamp }}
123
(Taras)
December 30, 2020, 7:23pm
6
You can also use python’s slice function to directly extract the date and time from the string.
- platform: scrape
resource: https://xxxxxx
select: "#hoy > div:nth-child(1) > section > div > div.container.parte-dia-header > p"
value_template: "{{ strptime(value[12:22] ~ value[28:34], '%d/%m/%Y %H:%M') }}"
name: sierra nevada last update
scan_interval: 320
In the template, the date string is extracted with value[12:22]
and the time string is extracted with value[28:34]
(includes the space character before the time). The two extracted strings are combined (with the tilde ~
character) and converted with strptime
.
Note
If you want to experiment with slice, copy-paste this example into the Template Editor.
{% set value = 'Generado el 30/12/2020 a las 14:56' %}
Raw: {{ value[12:22] ~ value[28:34] }}
Cooked: {{ strptime(value[12:22] ~ value[28:34], '%d/%m/%Y %H:%M') }}
Screenshot:
1 Like
Having another issue, value not being float value, being a string
# Pollen / health levels
- platform: scrape
resource: https://weather.com/es-ES/forecast/air-quality/l/d292a1bbe81187bb17ce64cfa1ed7369b2fd2459fe68149cb07d6a775a0bf8da
select: "#WxuAirQuality-main-7de6dc7e-8c44-46e2-bc70-fc9b603bb596 > div > section:nth-child(2) > div > div:nth-child(1) > div > div:nth-child(2) > div > div > span.AirQualityText--measurement--2AGlG.AirQuality--pollutantMeasurement--dlvtB"
name: Ozono Torrelodones
value_template: '{{ value.rstrip(" μg/m3") | float }}'
scan_interval: 320
123
(Taras)
January 5, 2021, 7:54pm
8
Add:
unit_of_measurement: 'μg/m3'
That helps to change the graph’s appearance.