Error: expected str instance, Optional found

Hello all,

I’ve been facing a problem recently where certain button that call services fail to action with the following error:

Failed to call service light/turn_on. sequence item 0: expected str instance, Optional found

This happens after a while that I navigate through the interface and is temporarily resolvable by reloading home assistant in my browser 3 times (which I believe drops the cache in Google Chrome).
I experienced the above on Google Chrome running on my Linux machine, however my android phone exhibits the same problem and is temporarily fixable in the same way.

Same, again, happens on the android app, however I don’t know of an easy way to drop the cache on that. I believe it uses the android web viewer as a backend anyway, so same difference really…

I’ve tried deleting the whole cache and cookies but unfortunately that didn’t help.

Here’s a visual example of this problem:

The home assistant logs report this error when it happens:

Logger: homeassistant.components.websocket_api.http.connection
Source: helpers/config_validation.py:132
Integration: Home Assistant WebSocket API (documentation, issues)
First occurred: May 2, 2021, 10:44:13 PM (62 occurrences)
Last logged: 11:15:37 PM

[2772796776] sequence item 0: expected str instance, Optional found
[2794859560] sequence item 0: expected str instance, Optional found
[2784769696] sequence item 0: expected str instance, Optional found
[2788180896] sequence item 0: expected str instance, Optional found
[2848991648] sequence item 0: expected str instance, Optional found
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/websocket_api/commands.py", line 143, in handle_call_service
    await hass.services.async_call(
  File "/usr/src/homeassistant/homeassistant/core.py", line 1434, in async_call
    processed_data = handler.schema(service_data)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/validators.py", line 218, in __call__
    return self._exec((Schema(val) for val in self.validators), v)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/validators.py", line 337, in _exec
    v = func(v)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/schema_builder.py", line 272, in __call__
    return self._compiled([], data)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/schema_builder.py", line 817, in validate_callable
    return schema(data)
  File "/usr/src/homeassistant/homeassistant/helpers/config_validation.py", line 132, in validate
    raise vol.Invalid("must contain at least one of {}.".format(", ".join(keys)))
TypeError: sequence item 0: expected str instance, Optional found

Here’s the code of one of the buttons that triggers this error:

type: button
tap_action:
  action: call-service
  service: light.turn_on
  service_data:
    brightness_pct: 10
  target:
    entity_id:
      - light.master_bedroom_lamp
show_name: false
icon: 'mdi:lightbulb-outline'

My installation is running the following:

  • Home Assistant: 2021.4.6
  • Frontend: 20210407.3 - latest
  • Raspberry Pi 4B (4GB)

I’m not sure what’s causing this behaviour but I’d be very grateful if someone with more knowledge could help me figure our this problem, as it’s been driving me bonkers!

Happy to test any theories you might have.

Thank you,

Alex

I think your target key is not correctly indented. That should be part of service_data and not sit next to it on the same level.

Thank you for replying!

As I mentioned, the code does work when I refresh the page a few times, but I tested what you suggested anyway and I got the following error:

Failed to call service light/turn_on. extra keys not allowed @ data['target']

I should have said that I lazily made these buttons using the gui, I only copied the code as I thought it’d be easier to display on here.

Oh, I wasn’t aware that target as a separate key was allowed now. The docs do not mention it.

I tested your card config in my dev instance and it works fine there. No errors raised.

Oh nice thank you for trying!
The error only occurs after a while that I click round the interface (after I refresh a few times).
I think it happens after I switch to another view in Home Assistant and then go back.
Sounds more and more like a bug to me…

If you find a reliable way to reproduce it, feel free to open a bug ticket in the frontend repository and we will have a look.

Thank you Philip, I will!

Well funny you should say that, I just had a play around and noticed that this error only starts happening when I switch to the home view and not other tabs.

Maybe there’s some card in there that’s causing this?
I might setup another view to play around with and copy cards in it to see what could be causing this problem.

I was able to narrow down the problem.

Turns out the error might be related to a custom card I’ve recently started using called local-condition-card.

To replicate the problem, simply install it and create the following two views:

First one contains the local-condition-card:

  - icon: 'mdi:home'
    path: test
    title: Test
    badges: []
    cards:
      - cards:
          - type: button
            tap_action:
              action: call-service
              service: local_conditional_card.toggle
              service_data:
                ids:
                  - test
              target: {}
            icon: 'mdi:home'
            show_name: false
            hold_action:
              action: none
          - type: 'custom:local-conditional-card'
            default: show
            id: test
            card:
              type: button
              icon: 'mdi:lightbulb'
              show_name: false
              show_state: false
              tap_action:
                action: none
              hold_action:
                action: none
        title: Test
        type: vertical-stack

The second one contains only a button that calls a service (doesn’t matter what AFAIK):

  - title: Test2
    path: test2
    icon: 'mdi:home-outline'
    badges: []
    cards:
      - type: button
        tap_action:
          action: call-service
          service: light.turn_on
          service_data:
            brightness_pct: 10
          target:
            entity_id:
              - light.test
        show_name: false
        icon: 'mdi:lightbulb-outline'

To see the problem in action:

  1. Go to the second view, refresh the page, and trigger the button. No errors pop up
  2. Switch to the first view
  3. Switch back to the second view and trigger the same button as step 1 (without refreshing).
  4. This time you should see the original error I reported:
Failed to call service light/turn_on. sequence item 0: expected str instance, Optional found
  1. Still on the second view, refresh the page and trigger the button. There should be no errors again.

It’d be good if someone could try and replicate the problem, just to confirm this happens to them also.
I’ll be reporting this behaviour to the original creator of that custom card, but I’m open to suggestions if someone has any idea on what I could try next (I’m looking at you @spacegaier :slight_smile: ).

Cheers!

Update:

I posted an issue on the local-condition-card github page and the developer replied immediately.

@spacegaier I think you were onto something with the target being odd.

What the developer of the custom card suggested worked, here’s an example:


Created via GUI, error raised:

type: button
tap_action:
  action: call-service
  service: light.turn_on
  service_data:
    brightness_pct: 10
  target:
    entity_id:
      - light.test

Fixed:

type: button
tap_action:
  action: call-service
  service: light.turn_on
  service_data:
    brightness_pct: 10
    entity_id: light.test

Another example:
Created via GUI, error raised:

type: button
tap_action:
  action: call-service
  service: cover.set_cover_position
  service_data:
    position: 60
  target:
    entity_id:
      - cover.mancave
hold_action:
  action: none
show_icon: false
show_name: true
name: 60%

Fixed:

type: button
tap_action:
  action: call-service
  service: cover.set_cover_position
  service_data:
    position: 60
    entity_id: cover.mancave
hold_action:
  action: none
show_icon: false
show_name: true
name: 60%

This is a bit of a conundrum as they code created via the GUI technically works on its own but seems to be affected by this custom card, which in terms technically works if the code is in the correct format.
I’m not sure where I’d land on this and whether a bug should be filed in the frontend repository?

As always, any input is greatly appreciated!

This issue is caused by custom:local-conditional-card, as described here, no need for further investigation :slight_smile:

1 Like

As Piotr said: This issue comes from the custom card, so filing an issue in the frontend repo will not help :grinning_face_with_smiling_eyes:. The config YAML created by the HA GUI editor is valid and correct. The custom card needs to be adjusted to support the HA config target key.

1 Like

Thank you both for your help with this, much appreciated!
I’ll be using the adjusted configuration for the time being, no big deal.
Hopefully this will be of use to others in the meantime if they encounter the same problem.
Cheers!