Picture_entity but entity not existing

Hi guys,

I have added some picture entities including state_image

As I have some devices which are switched off using the original wall switch, the entity is not available after I restart home assistant (in case the lamp is switched off). Due to this, I always get this error message.

How can I ignore the availability of the entity (light.wohnzimmerlampe)?
Or better so say: If entity is not available, the state off picture shall be displayed.

This is the code:

      - entity: light.stehlampe
        show_name: false
        show_state: false
        state_image:
          'off': /local/stehlampe-aus.jpg
          'on': /local/stehlampe-an.jpg
          unavailable: /local/stehlampe-aus.jpg
        tap_action:
          action: toggle
        type: picture-entity

Error message:
Home%20Assistant%202019-08-18%2014-25-29

Instead of state_image you could use conditional cards. Since you’re only using two images you only need two conditions; state: "on" and state_not "on":

    type: conditional
    conditions:
      - entity: light.stehlampe
        state: "on"
    card:
      - entity: light.stehlampe
        show_name: false
        show_state: false
        image: /local/stehlampe-an.jpg
        tap_action:
          action: toggle
        type: picture-entity

    type: conditional
    conditions:
      - entity: light.stehlampe
        state_not: "on"
    card:
      - entity: light.stehlampe
        show_name: false
        show_state: false
        image: /local/stehlampe-aus.jpg
        type: picture-entity

…But if you decide to use a third image for ‘unavailable’ then you’d need four conditions contained in three conditional statements:

    type: conditional
    conditions:
      - entity: light.stehlampe
        state: "on"
    card:
      - entity: light.stehlampe
        show_name: false
        show_state: false
        image: /local/stehlampe-an.jpg
        tap_action:
          action: toggle
        type: picture-entity

    type: conditional
    conditions:
      - entity: light.stehlampe
        state: "off"
    card:
      - entity: light.stehlampe
        show_name: false
        show_state: false
        image: /local/stehlampe-aus.jpg
        tap_action:
          action: toggle
        type: picture-entity

    type: conditional
    conditions:
      - entity: light.stehlampe
        state_not: "on"
      - entity: light.stehlampe
        state_not: "off"
    card:
      - entity: light.stehlampe
        show_name: false
        show_state: false
        image: /local/stehlampe-nicht_verfügbar.jpg
        type: picture-entity

Thank you Jason.

Tried this code first only to check whether it works:

type: conditional
conditions:
  - entity: light.stehlampe
    state: 'on'
card:
  - entity: light.stehlampe
    image: /local/stehlampe-an.jpg
    tap_action:
      action: toggle
    type: picture-entity

If state of the lamp is on, I get the following result:

No card type configured.
[
  {
    "entity": "light.stehlampe",
    "image": "/local/stehlampe-an.jpg",
    "tap_action": {
      "action": "toggle"
    },
    "type": "picture-entity"
  }
]

but card type is configured? Or do I miss anything?

Oops, I forgot an array can’t follow card: (that’s why it doesn’t say cards: :sweat_smile:)…

      - type: conditional
        conditions:
          - entity: light.stehlampe
            state: "on"
        card:
          entity: light.stehlampe
          show_name: false
          show_state: false
          image: /local/stehlampe-an.jpg
          tap_action:
            action: toggle
          type: picture-entity

      - type: conditional
        conditions:
          - entity: light.stehlampe
            state_not: "on"
        card:
          entity: light.stehlampe
          show_name: false
          show_state: false
          image: /local/stehlampe-aus.jpg
          type: picture-entity

Edit: But the types do need to be an array.

Thank you.

Unfortunately the error message (entity unavailable) from the beginning is still there.

it seems to be because of this:

card:
          entity: light.stehlampe

Even if state_not: ‘on’ the system checks the following code and realizes, that this entity is not existing (as I haven’t switched on the lamp after restarting home assistant)

Hmm, I really thought that would work. :thinking:

You could try changing state_not: "on" to state: "unavailable" assuming that’s what the state actually shows (you can check under the States tab in Developer Tools). You’d then need a third conditional statement to also cover state: "off"

If that doesn’t work, I know a template_sensor will.

If the above doesn’t work this should…

sensor:

  - platform: template
    sensors:
      light_stehlampe:
        value_template: "{{ states('light.stehlampe') }}"

(^ requires restart)

lovelace:

  - type: conditional
    conditions:
      - entity: sensor.light_stehlampe
        state: "unknown"
    card:
      entity: sensor.light_stehlampe
      show_name: false
      show_state: false
      image: /local/stehlampe-aus.jpg
      type: picture-entity

  - type: conditional
    conditions:
      - entity: sensor.light_stehlampe
        state_not: "unknown"
    card:
      entity: sensor.light_stehlampe
      show_name: false
      show_state: false
      state_image:
        'off': /local/stehlampe-aus.jpg
        'on': /local/stehlampe-an.jpg
      tap_action:
        action: toggle
      type: picture-entity

While working on something else it struck me that this may be the simplest solution for your issue.

Just create a Template Binary Sensor
binary_sensor:

  - platform: template
    sensors:
      light_stehlampe:
        device_class: light
        value_template: "{{ is_state('light.stehlampe', 'on') }}"

(^ requires restart)

…then keep your original Card configuration (sans the ‘unavailable’ state) and just change the entity to the new sensor name…

      - entity: binary_sensor.light_stehlampe
        show_name: false
        show_state: false
        state_image:
          'off': /local/stehlampe-aus.jpg
          'on': /local/stehlampe-an.jpg
        tap_action:
          action: toggle
        type: picture-entity

This sensor configuration will only return on or off, so your (slightly modified) original picture-entity card should always work.

Edit: typo

Awesome, this should definitely work!
Thanks mate!!!

Now I only need to create an script (toggle lamps) as the toggle action won’t work as changing the status of the template sensor, would not turn of or on the lights :wink:

1 Like

Oh, good catch – I totally overlooked that. :roll_eyes:

This should work though:

      - entity: binary_sensor.light_stehlampe
        show_name: false
        show_state: false
        state_image:
          'off': /local/stehlampe-aus.jpg
          'on': /local/stehlampe-an.jpg
        tap_action:
          action: call-service
          service: light.toggle
          service_data:
            entity_id: light.stehlampe
        type: picture-entity
1 Like

You are the best! Thank you very very much!

Works like a charm :slight_smile:

1 Like

No problem, glad to hear it finally works. :sweat_smile: