Scrape: Random index?

Hi everyone,

I’m trying to cast a random wallpaper to my chromecasts with no luck.

I successfuly configure a script to cast a sensor value containing the url but the problem is I can’t randomize the item selection

This is my scrape sensor

  - platform: scrape
    resource: https://wallpaper.dog/coolest-gaming-wallpapers
    name: scrape2
    select: "div[data-fullimg]"
    attribute: id
    index: 5
    value_template: '{{ "https://wallpaper.dog/large/" ~ value ~ ".jpg" }}'

Is there any way of using a random index without messing with pyhton?

Sry I’m a begginer and a non english speaker… be patient with me xD

PS: Just in case… this is my script (Initial)

'1649893800180':
  alias: Nuevo script
  sequence:
  - service: media_player.play_media
    target:
      entity_id: media_player.cocina
    data:
      media_content_id: '{{ states(''sensor.scrape2'') }}'
      media_content_type: image/jpeg
  mode: single

You can create a range of the numbers and take a random one.

{{ "https://wallpaper.dog/large/" ~ range(1,10) | random ~ ".jpg" }}
1 Like

Yes, but in that case many of them will be invalid or not in the same album. Can i template the index of the sensor? I think I’ve tried that…

No, I can’t use templates on the index attribute :cry:

Invalid config for [sensor.scrape]: expected int for dictionary value @ data['index']. Got '{{ range(1, 10) | random }}'. (See ?, line ?).

I didn’t know it was the index attribute you where talking about.
You had a template with “value” printed in the middle, that looked like what you wanted help with.

Given that the index does not allow for templates then I don’t think that is possible in HA.
You could try and do it in Node red, it doesn’t have the same limitations.

1 Like

I do not use nodered and considerating the learning curve maybe I 'll give a try with Python instead. If anyone thinks a workaround please let me know.

Thanks for ur help anyways

PS: Sorry for my bad english

In case you want to give it a go then here is a working example:

This sequence will give you a list of all wallpapers:

And in Developer tools it looks like: (I forgot to give it a name first)

And from that sensor you should be able to take {{sensor.state}} ~ {{ sensor.attributes.images |random}}

[{"id":"1236609655bb0e7e","type":"inject","z":"90cf9b8a.ce14c8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":780,"y":580,"wires":[["8ad61386349650bf"]]},{"id":"8ad61386349650bf","type":"http request","z":"90cf9b8a.ce14c8","name":"","method":"GET","ret":"txt","paytoqs":"ignore","url":"https://wallpaper.dog/coolest-gaming-wallpapers","tls":"","persist":false,"proxy":"","authType":"","senderr":false,"x":950,"y":580,"wires":[["3b9b786a069cdebf"]]},{"id":"3b9b786a069cdebf","type":"html","z":"90cf9b8a.ce14c8","name":"","property":"payload","outproperty":"payload","tag":"div[data-fullimg]","ret":"html","as":"single","x":1160,"y":580,"wires":[["f5dd43d90bd772c1"]]},{"id":"e71353dd48490af4","type":"debug","z":"90cf9b8a.ce14c8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1530,"y":580,"wires":[]},{"id":"f5dd43d90bd772c1","type":"function","z":"90cf9b8a.ce14c8","name":"","func":"var arr = new Array(msg.payload.length);\nvar i = 0;\n\nconst regex = /(\\/large\\/\\d+\\.jpg)/;\nvar match = new Array(2);\n\n\nmsg.payload.forEach(function (value,index) {\n    \n    match = value.match(regex);\n    arr[i] = [...new Set(match)].toString(); //match[0]\n    i++;    \n});\nmsg.payload = arr.filter(n => n)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1380,"y":600,"wires":[["e71353dd48490af4","f286bc4b6f06e871"]]},{"id":"f286bc4b6f06e871","type":"ha-entity","z":"90cf9b8a.ce14c8","name":"wallpapers","server":"4bbca37b.1700ec","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":""},{"property":"device_class","value":""},{"property":"icon","value":""},{"property":"unit_of_measurement","value":""}],"state":"https://wallpaper.dog/","stateType":"str","attributes":[{"property":"images","value":"payload","valueType":"msg"}],"resend":true,"outputLocation":"payload","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"$entity().state ? \"on\": \"off\"","outputPayloadType":"jsonata","x":1530,"y":500,"wires":[[]]},{"id":"4bbca37b.1700ec","type":"server","name":"Home Assistant","version":1,"legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

You can change the setting of the inject node to repeat at some interval to update the list (if the images change on the webpage).

It look nice!

But I’ve solved it using a 3rd party component based on scrape (multiscrape)

multiscrape:
  resource: https://wallpaper.dog/coolest-gaming-wallpapers
  scan_interval: 30
  sensor:
    - unique_id: wp_url
      name: Wallpaper URL
      select_list: "div[data-fullimg]"
      attribute: "id"
      value_template: '{{ "https://wallpaper.dog/large/" ~ value.split(",") | random ~ ".jpg" }}'

I really really thank u for the time u put into this and at some point I’ll give nodeRed a try

1 Like

A possible solution could be to use the multiscrape integration, which has a select_list option that will return a comma-separated string of all matching elements. Inside the value template you could then split that string into separate items and pick a random one ({{ value.split(',') | random }}).

@Aldopez beat me to it :wink:

1 Like

Almost the same time xD. Thanks anyway