Template and binary sensor showing incorrect icon or none at all

using template sensor:

      count_earthquakes:
        friendly_name: Earthquakes
        value_template: >
          {{ states.geo_location|selectattr('attributes.source','eq','usgs_earthquakes_feed')|list|count }}
        icon_template: >
          {% if states('sensor.count_earthquakes')|int != 0 %} mdi:google-earth
          {% else %} mdi:earth
          {% endif %}

and binary template sensor:

      earthquakes_active:
        friendly_name: Earthquakes active
        value_template: >
          {{states('sensor.count_earthquakes')|int > 0 }}
        icon_template: >
          {% if is_state('binary_sensor.earthquakes_active','on') %} mdi:flash-alert
          {% else %} mdi:flash-off
          {% endif %}

shows me:

24

which is beyond me, because the bottom Earthquakes sensor show this in the dev-template:

iow, it should be showing mdi:google-earth, while the dev-state shows:

31

which is what is actually shown…

what am I missing here?

the binary sensor is incorrect also, in that it doesn’t show at all.

it should however show mdi:flash-alert according to the template:

as is confirmed in the dev-state:

30

so.both sensors puzzle me.

please have a look, thanks!

You may want to try and add the entity that the template sensor needs to monitor to update itself:

earthquakes_active:
        friendly_name: Earthquakes active
        entity_id: states.geo_location
        value_template: >

ok I will do that, to ensure proper updating of the sensor.

It wouldn’t however explain the icon being displayed would it? To be sure the entity state was updated, I had manually serviced that, so if everything was as it should be, the templates above and the actual state should have been identical…

as an update:

the states.geo_location wouldn’t trigger the template sensor. I had to do it manually to have the sensor change its state, even while the states.geo_location sensors where created.
so I ended up using sensor.time, which now shows correct sensors even at startup.

using this for now:

binary-sensor:
  - platform: template
    sensors:
      lightning_active:
        friendly_name: Lightning active
#        entity_id: sensor.count_lightning_strikes
        value_template: >
          {{states('sensor.count_lightning_strikes')|int > 0 }}
        icon_template: >
          {% if is_state('binary_sensor.lightning_active','on') %} mdi:flash-alert
          {% else %} mdi:flash-off
          {% endif %}

      earthquakes_active:
        friendly_name: Earthquakes active
#        entity_id: sensor.count_earthquakes
        value_template: >
          {{states('sensor.count_earthquakes')|int > 0 }}
        device_class: safety
#        icon_template: >
#          {% if is_state('binary_sensor.earthquakes_active','on') %} mdi:alert
#          {% else %} mdi:shield-check
#          {% endif %}

and

sensor:
  - platform: template
    sensors:

# https://community.home-assistant.io/t/conditional-map/132739/4
      count_lightning_strikes:
        entity_id: sensor.time
        friendly_name: Lightning strikes
        value_template: >
          {{ states.geo_location|selectattr('attributes.source','eq','wwlln')|list|count }}

      count_earthquakes:
        entity_id: sensor.time
        friendly_name: Earthquakes
        value_template: >
          {{ states.geo_location|selectattr('attributes.source','eq','usgs_earthquakes_feed')|list|count }}

customizations work now too, (I’ve left the previous tests commented as reference)

    binary_sensor.lightning_active:
      templates:
        icon_color: >
          if (state === 'on') return 'red';
          return 'green';
#        icon: >
#          if (state === 'on') return 'mdi:flash-alert'
#          return 'mdi:flash-off';

    sensor.count_lightning_strikes:
      templates:
        icon_color: >
          if (state !== '0') return 'red';
          return 'green';
        icon: >
          if (state !== '0') return 'mdi:weather-lightning';
          return 'mdi:flash-off';

    binary_sensor.earthquakes_active:
      templates:
        icon_color: >
          if (state === 'on') return 'red';
          return 'green';
#        icon: >
#          if (state === 'on') return 'mdi:flash-alert'
#          return 'mdi:flash-off';

    sensor.count_earthquakes:
      templates:
        icon_color: >
          if (state !== '0') return 'red';
          return 'green';
        icon: >
          if (state !== '0') return 'mdi:google-earth';
          return 'mdi:earth';

to make this in Lovelace:

  - type: custom:vertical-stack-in-card
    cards:
      - type: entities
        title: Quakes
        show_header_toggle: false
        entities:
          - binary_sensor.earthquakes_active

      - type: custom:vertical-stack-in-card
        cards:
          - type: conditional
            conditions:
              - entity: binary_sensor.earthquakes_active
                state: 'on'
            card:
              type: map
        #      dark_mode: true
              entities:
                - device_tracker.life360_daughter2
              geo_location_sources:
                - usgs_earthquakes_feed
    #          title: Quakes conditional map
              default_zoom: 6
              aspect_ratio: '2'

          - type: custom:monster-card
            show_empty: false
            card:
              type: entities
              show_header_toggle: false
    #            title: Earth quakes
            filter:
              include:
                - entity_id: sensor.count_earthquakes
                - domain: geo_location

and show up as:

09

1 Like