Split value from a sensor.. or how to make sense for a newbie

So I have setup a sensor that been tracking bus times that I want to have announce via TTS in the morning between a certain time but confused on templating. To be honest I haven’t really grasped the concept yet. Now I would like to learn as they look really useful.

trying to start with this one but its giving me what looks like a bit of unnecessary data.

I have:

The next bus is in {{ states.sensor.kids_school }} minutes

this gives me -

The next bus is in <state sensor.kids_school=15; Later Bus=xxx in 40, Due in=15, unit_of_measurement=min, friendly_name=Kids School, Stop ID=xxxx, Route=xxx, Due at=16/06/2017 21:05:17, icon=mdi:bus, attribution=Data provided by data.dublinked.ie @ 2017-06-16T20:49:58.900690+01:00> minutes

Can someone explain how I would extract basically the minutes left to the first bus, and then if I needed the second one also?.. without having 'data provided by dub…

Thanks in advance.

Try sticking .state on the end of kids_school, not much clue how to go about getting the later bus probably some form of template or maybe scrape, maybe attributes.later_bus, i’m not sure.

Look at the sensor.kids_school object in the states tool of developer tools.


There, you will find the “STATE” of the object as well as the “ATTRIBUTES” of the object.
If the value you want is in the state, you can get it with states.sensor.kids_school.state
If the value you want is in an attribute, you can get it with states.sensor.kds_school.attribute.Later_Bus

thank you Keithh666 and treno. Information was so useful and I get it now. Anyone interested in the code I used

The next bus is in {{ states.sensor.kids_school.state }} minutes. The next bus after that is the {{ states.sensor.kids_school.attributes["Later Bus"] }} minutes

Thanks for the info. I couldn’t get this to work until I put the attribute name in single quotes, i.e. ‘Later Bus’ instead of “Later Bus”. Single quotes is indicated in the Templates documentation:

http://jinja.pocoo.org/docs/dev/templates

Thanks for that. Mine still working for showing the bus time but was wondering why the automation stopped firing. Will change this later and hopefully should fix. Cheerss

I have since discovered that my problem was using double quotes inside of double quotes when the value_template has to be set up with double quotes as in this example. When I put single quotes inside the double quotes, all worked fine. The example also show how to strip out the leading characters of the next bus string to just show the mins.

      later_bus:
        friendly_name: Later Bus
        entity_id: sensor.next_bus
        value_template: "{{ states.sensor.next_bus.attributes['Later Bus']|replace('16 in ', '') }}"
        icon_template: mdi:bus

Might be remedial to some but any ideas how to make a script to announce next bus? I’m not looking for any trigger but looking to use a dash button to announce time via Chromecast?

Have setup dasher (although maybe buggy) but was able to turn off lights with it so will just swap out the code…

Spent too much time last night over thinking it but going to give it another go later.

Ps formatting gets me every time so paste bin welcomed :slight_smile:

Belated reply, I don’t have any experience with dasher or Chromecast. I used your custom python module as the basis for a further customisation. I get it to extract up to four bus times, i.e. next 4 buses. I live on a single bus route (16) so not interested in the other routes as in your ‘next bus’ set up. Still have a bug in it though and sometimes it goes dead on me. Haven’t figured out why. Thanks for the initial version.