[SOLVED] How to reedit the value from rest sensor after retrieve value

I’ve create a sensor which gets me a json, and i’ve got a value im looking for from this json and updating in to an entity.

My second though is reuse this data, but with some value edit.
For example, i am getting ‘新北市板橋區123路abc巷78號
I want to create another sensor, but remove the first 6 characters and remove all characters after 巷 (if there are).
From example above, i wish to get ‘123路abc巷’. The’新北市板橋區’ and ‘78號’ are removed.

I am having hard time to test below code as most of the time the value is ‘none’ when i am able to do test.

sensor:
  - platform: rest
    name: "垃圾車當前位置"
    resource: https://crd-rubbish.epd.ntpc.gov.tw/dispProject/api/line-status.ashx?lineid=244025
    value_template: "{{ value_json['data']['place'] }}"
    scan_interval: 300

  - platform: template
    sensors:
      garbage_truck_location:
        friendly_name: "Garbage Truck Location"
        value_template: >-
          {% set location = states('sensor.垃圾車當前位置') %}
          {% set location = location[6:] %}
          {% set location = location.split('巷')[0] + '巷' %}
          {{ location }}

Another questions is, if i am getting multiple value from a json. Would it be possible to select which one to use by checking the value from json? Like using a if ‘value’ contain ‘abc’, then json[0], else json[1] type of logic?

What is “none” — your rest sensor or your template? Looking at that URL now:

# I set the value in place of the rest sensor which I don't have
{% set value = "新北市林口區文化北路一段299巷69號" %}
{{ (value[6:].split("巷"))[0]~"巷" }}

Yes. Give an example of what you’d like to do with real data.

What is “none” — your rest sensor or your template? Looking at that URL now:

I will get empty place value during the time when i am able to do test with entity, due to ‘place’ value will only be valid during sometime of the day. Didn’t know i could try that in Developer Tools! but your answer should do the trick.

<data>
<lineid>244025</lineid>
<total>19</total>
<arrival>0</arrival>
<place/>
<time/>
<diff>0</diff>
<LonVac>0</LonVac>

Yes. Give an example of what you’d like to do with real data.

So i will input above reedited data in to ‘street’ in openstreetmap API to get lat & lon. Sometimes i will get multiple result, and i want the use the one that contain ‘南勢里’ in “display_name”.

In below’s data, i should be using the 1st response, and getting the lat & lon.

https://nominatim.openstreetmap.org/search.php?street=%E6%96%87%E5%8C%96%E5%8C%97%E8%B7%AF%E4%B8%80%E6%AE%B5&city=%E6%96%B0%E5%8C%97%E5%B8%82&county=%E6%9E%97%E5%8F%A3&country=%E5%8F%B0%E7%81%A3&postalcode=244&polygon_geojson=1&format=jsonv2

Result:

[{
	"place_id": 167469631,
	"licence": "Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright",
	"osm_type": "way",
	"osm_id": 238900896,
	"boundingbox": ["25.0758923", "25.0804825", "121.3641188", "121.3687055"],
	"lat": "25.0779117",
	"lon": "121.3662417",
	"display_name": "文化北路一段, 南勢里, 林口區, 新北市, 244, 臺灣",
	"place_rank": 26,
	"category": "highway",
	"type": "secondary",
	"importance": 0.5300099999999999,
	"geojson": {
		"type": "LineString",
		"coordinates": [
			[121.3687055, 25.0804825],
			[121.3680204, 25.0797557],
			[121.3675904, 25.0793124],
			[121.367319, 25.0790273],
			[121.3662417, 25.0779117],
			[121.3658363, 25.0775202],
			[121.3654411, 25.0771401],
			[121.3650419, 25.0767597],
			[121.3646359, 25.0763794],
			[121.3641188, 25.0758923]
		]
	}
}, {
	"place_id": 272822368,
	"licence": "Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright",
	"osm_type": "way",
	"osm_id": 851877429,
	"boundingbox": ["25.0759206", "25.0805737", "121.3639228", "121.3685892"],
	"lat": "25.078278",
	"lon": "121.3663978",
	"display_name": "文化北路一段, 湖南里, 林口區, 新北市, 244, 臺灣",
	"place_rank": 26,
	"category": "highway",
	"type": "secondary",
	"importance": 0.5300099999999999,
	"geojson": {
		"type": "LineString",
		"coordinates": [
			[121.3685892, 25.0805737],
			[121.3672862, 25.0792137],
			[121.3663978, 25.078278],
			[121.3661303, 25.0780074],
			[121.3657361, 25.0776114],
			[121.3653364, 25.0772299],
			[121.3649399, 25.0768496],
			[121.3645371, 25.0764696],
			[121.364, 25.0759867],
			[121.3639228, 25.0759206]
		]
	}
}]

I’ve compressed that JSON to a single line using this tool just so it doesn’t fill the screenshot, and used selectattr (documented here, which is the Jinja2 link on the Developer Tools / Template page) to pull out any items from the JSON list where display_name contains your characters.

That’s still a list (selectattr provides a generator so is converted with |list) — you can see in the screenshot that it’s contained in [].

In the next line, I use the same method but choose the first (the only) matching item and then pull out its lat and lon.

{% set x = [{"place_id":167469631,"licence":"Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright","osm_type":"way","osm_id":238900896,"boundingbox":["25.0758923","25.0804825","121.3641188","121.3687055"],"lat":"25.0779117","lon":"121.3662417","display_name":"文化北路一段, 南勢里, 林口區, 新北市, 244, 臺灣","place_rank":26,"category":"highway","type":"secondary","importance":0.5300099999999999,"geojson":{"type":"LineString","coordinates":[[121.3687055,25.0804825],[121.3680204,25.0797557],[121.3675904,25.0793124],[121.367319,25.0790273],[121.3662417,25.0779117],[121.3658363,25.0775202],[121.3654411,25.0771401],[121.3650419,25.0767597],[121.3646359,25.0763794],[121.3641188,25.0758923]]}},{"place_id":272822368,"licence":"Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright","osm_type":"way","osm_id":851877429,"boundingbox":["25.0759206","25.0805737","121.3639228","121.3685892"],"lat":"25.078278","lon":"121.3663978","display_name":"文化北路一段, 湖南里, 林口區, 新北市, 244, 臺灣","place_rank":26,"category":"highway","type":"secondary","importance":0.5300099999999999,"geojson":{"type":"LineString","coordinates":[[121.3685892,25.0805737],[121.3672862,25.0792137],[121.3663978,25.078278],[121.3661303,25.0780074],[121.3657361,25.0776114],[121.3653364,25.0772299],[121.3649399,25.0768496],[121.3645371,25.0764696],[121.364,25.0759867],[121.3639228,25.0759206]]}}] %}
{{ x|selectattr('display_name','contains','南勢里')|list }}
{% set y = x|selectattr('display_name','contains','南勢里')|first %}
{{ y.lat }}, {{ y.lon }}

Hope that helps.

1 Like