Trying to add switches to a picture-elements card

I've been looking for an example of a dashboard where someone has put switches on top of a live video image and come up dry. A Google search said to use picture-elements and then in yaml you can place buttons in specific places. Great but I haven't found an example that I can use as a starting point.

Here is the yaml I've got that shows my driveway camera feed:

type: picture-elements
elements: []
image:
  media_content_id: https://demo.home-assistant.io/stub_config/floorplan.png
camera_image: camera.d1_drive_fluent
camera_view: live

And here is the yaml of my basic switch for my infantile gate control card using the momentary relay. It doesn't need to be this form of switch but I want to trigger the gate from a view where I can see the driveway. (Later I'll be adding the parking flood, driveway lights, and porch lights.)

type: tile
entity: switch.digital_or_analog_voltage_input_and_or_dry_contact_relay
icon: mdi:gate
color: light-green
hide_state: false
vertical: true
features:
  - type: toggle
features_position: bottom

Any relevant examples or suggestions would be appreciated.
Also any really good tutorial or course on coding this sort of stuff would be incredible. I don't have enough of a grasp to understand much past the simple formatting rules.

Thanks
Wayne

Hi, I have done something similar with a picture glance card:

aspect_ratio: 0%
camera_image: camera.parking
entities:
  - entity: sensor.temp_home
    icon: mdi:thermometer
    show_state: true
  - entity: script.gate
    icon: mdi:gate-arrow-right
    hold_action:
      action: call-service
      service: script.gate
type: picture-glance

Hi @gamblehouse, you're closer than you think. With picture-elements, the elements: list is exactly where the controls go, and each one gets a style: with top/left percentages to place it on the image. Tap actions work just like on a normal card.

Two quick cleanups first: since you're using camera_image, you can delete the whole image: block (that floorplan line is the leftover stub, and the media_content_id form isn't valid for picture-elements anyway).

Here's a working starting point that drops your gate switch into the bottom-left of the live feed:

type: picture-elements
camera_image: camera.d1_drive_fluent
camera_view: live
elements:
  - type: state-icon
    entity: switch.digital_or_analog_voltage_input_and_or_dry_contact_relay
    icon: mdi:gate
    tap_action:
      action: toggle
    style:
      top: 88%
      left: 12%
      color: white
      background: rgba(0, 0, 0, 0.55)
      border-radius: 50%
      padding: 8px
      "--mdc-icon-size": 36px

How it works:

  • state-icon shows the entity's icon and colours it by state (on vs off). By default a tap opens the details dialog, so I added tap_action: action: toggle to make a tap actually flip it. For your momentary gate relay, that tap fires the pulse, which is what you want.
  • top and left are percentages of the card and set the CENTRE of the icon, so top: 88%, left: 12% is near the bottom-left. Just nudge those two numbers to move it anywhere.
  • The background / border-radius / padding lines draw a translucent dark circle behind the icon so it stays readable over a busy video frame. Delete them if you don't want it.

Adding the rest later is just more entries in the same elements: list, each with its own top/left:

elements:
  - type: state-icon
    entity: switch.digital_or_analog_voltage_input_and_or_dry_contact_relay
    icon: mdi:gate
    tap_action:
      action: toggle
    style:
      top: 88%
      left: 12%
  - type: state-icon
    entity: light.parking_flood
    icon: mdi:track-light
    tap_action:
      action: toggle
    style:
      top: 88%
      left: 37%
  - type: state-icon
    entity: light.driveway
    icon: mdi:outdoor-lamp
    tap_action:
      action: toggle
    style:
      top: 88%
      left: 62%
  - type: state-icon
    entity: light.porch
    icon: mdi:coach-lamp
    tap_action:
      action: toggle
    style:
      top: 88%
      left: 87%

(add the same color/background/padding style lines to each if you want the readable pill behind every icon.)

A few tips:

  • Prefer a labelled button to an icon? Swap state-icon for state-label (shows the state as text) or service-button (a text button that calls a service).
  • For fancier buttons down the road, the custom button-card (HACS) can be used as a picture-elements element, but the built-in state-icon is the easiest way to get moving.
  • Watch out for YAML and commas: if you ever write the style inline like {background: "rgba(0,0,0,0.5)"}, the rgba value must be quoted because of the commas. In the indented block form above it doesn't need quotes.

Learning resources:

  • The official Picture Elements card docs list every element type with examples, and the Actions docs cover tap_action / hold_action. Those two pages are the whole game for this card.
  • On YouTube, searching "Home Assistant picture elements" pulls up good walkthroughs; Smart Home Junkie, Everything Smart Home, and BeardedTinker all cover dashboards well.
  • The forum's "Share your Projects" category is full of picture-elements floorplans you can borrow styling from.

Post a screenshot once the lights are in!

1 Like

Holy Crap-A-Rino!
Thank you for such a detailed answer! I've got to finish cutting a concrete slab but I'll try this as soon as I'm done.

Thanks!!!

@fr0styh0e - Dropped in your code and it instantly worked. I just adjusted the vertical location but it is a functional start. (Now comes all the "Make it look nice.")

Here is a screenshot of it on my phone in landscape.

The camera is a 16MP Reolink Duo3 so it is very very wide. It would be nice if I could default zoom the view to about the right 3/5ths but if wishes were fishes...
The key thing is I can see what's in the driveway before I trigger the gate. This has been the key stumbling block before I transfer all the lights over from Hubitat.

Thanks again... Especially for the training resources!

Wayne

Awesome! Glad to hear it.

The June update might be of a huge help with making it look nice, the new dashboard editor is fancy :heart_eyes:

1 Like