Scrap a number from a website - issue with remove a string from the price

Hi folks,

i have a short questions regarding the scrape sensor. I wanted to scrape a price but in the value i have a € at the front so i dont cant get the history to work, tried to cut it of with value_template parameter from the scrap sensor , but then the result is unknown, the code worked in the template editor but the sensor gave me then unknown.

value_template: "{{ states('sensor.dying_light_pc_test_cdkeys_com').split('€')[1] }}"

or

value_template: "{{ states('sensor.dying_light_pc_cd_keys_com').replace('€', '') }}" .

has the price with the € in the number.

- platform: scrape
  resource: "https://www.cdkeys.com/pc/dying-light-2-steam-pc"
  unit_of_measurement: "€"
  name: Dying Light - PC - Test (CDKEYS.com)
  select: 'span#product-price-3430'
  scan_interval: 600  

give me unknown value:

- platform: scrape
  resource: "https://www.cdkeys.com/pc/dying-light-2-steam-pc"
  unit_of_measurement: "€"
  name: Dying Light - PC (CDKEYS.com)
  select: 'span#product-price-3430'
  value_template: "{{ states('sensor.dying_light_pc_cd_keys_com').replace('€', '') }}"
  scan_interval: 600`

Somebody can give a advice ?

thx in advance.

Benni

Have you tried this? it will remove the first character in the string

value_template: "{{ states('sensor.dying_light_pc_test_cdkeys_com')[1:] }}"

Tested it, but didnt worked, unknown value, in the Template Editor it worked but not in the scape sensor.

Template-Editor

{{ states('sensor.dying_light_pc_test_cdkeys_com').split('€')[1] }}
{{ states('sensor.dying_light_pc_test_cdkeys_com').replace('€', '') }}
{{ states('sensor.dying_light_pc_test_cdkeys_com')[1:] }}

Ergebnistyp: string


59.99 
59.99 
59.99

So why its working in template editor ? And not in the value_template from the scrape sensor?

Shouldn’t it be

{{ value.replace('€', '') }}

Otherwise you are trying to change the previous state of the sensor, not the new state.

I did this

- platform: scrape
  resource: "https://www.cdkeys.com/pc/dying-light-2-steam-pc"
  unit_of_measurement: "USD"
  name: Dying Light - PC (CDKEYS.com)
  select: 'span#product-price-3430'
  value_template: "{{ value[1:] }}"
  scan_interval: 600

and got this

as Andrew said above you were modifying previous state

1 Like

Thanks

{{ value[1:] }}

worked :wink: