Complete newbie. Need help creating my first widget template

Hi, so I figured out how to show sensor info, such as temperature, is state, etc.

How can I add a clickable button that switches lights for a widget template? Could you please show an example of a clickable button light switch code in a widget template for Android?

What about multiple buttons in one widget?

Also, how to add a dynamic light icon to that light switch in a widget template?

Thank you

template widget is not controllable, only shows the rendered template

not a current feature

you cant add an icon but you can use emoji

Before I start showing some template examples, I have to say that I use the Nova Launcher on my Android - it allows me to ‘layer’ multiple widgets on top of each other.

So, I normally use a ‘Service Button’ widget that I use to call e.g. a switch.toggle service in the background; I use an image for the icon, but I don’t use the ‘Label’ field.

I then overlay it with a smaller/half-size ‘Template’ widget that allows me to not only show the status, but apply some basic formatting, e.g. font color.

The Service Button is straightforward and the template widget looks like this:

{% if is_state('automation.frigate_notification_poolcam','on') -%}
  <font color="green">
{% else %}
  <font color="red">
{% endif %}
<b> {{ states('automation.frigate_notification_poolcam') }}</b>

This is the result on my screen:
image image

Tapping on the camera symbol turns the automation on and off.
I don’t think it’s straightforward to dynamically change the icon/symbol itself, but I’m good with the 2-layer setup.

To display info from multiple sensors I also use the template widget, e.g.

WS {{ ((states("sensor.temp") | float(0) - 32)*5/9) | round(1) }}C<br>Pt {{ ((states("sensor.temp_1") | float(0) - 32)*5/9) | round(1) }}C<br>Pl {{ ((states("sensor.temp_2") | float(0) - 32)*5/9) | round(1) }}C

This widget converts and shows temperature values in Celsius, which are in Fahrenheit in my Home Assistant installation, without having to create these sensors in the Home Assistant installation itself:
image

The only way I’ve so far been able to drive multiple actions with one button is to use the setup from above and trigger an actionable notification.

Example:
I have two outdoor fans that are each switched on and off remotely.

I use the ‘Service Button’ widget to execute a script:

service: notify.mobile_app_pixel
data:
  message: "Choose the Fan to toggle:"
  title: Outdoor Fan Action
  data:
    ttl: 0
    priority: high
    tag: outdoor_fan_action
    actions:
      - action: toggle_outdoor_fan_both
        title: Both
      - action: toggle_outdoor_fan_patio
        title: Patio
      - action: toggle_outdoor_fan_terrace
        title: Terrace
    channel: Alert
    importance: high

I am then presented the three options:
image

I select the option I want and the widget on my screen changes e.g. from
image

to
image

And this is the code for the widget that changes the text color, the text itself comes from a separate helper:

{% if is_state('sensor.outdoor_fan_status','On') -%}
  <font color="red">
{% elif is_state('sensor.outdoor_fan_status','Off') -%}
  <font color="green">
{% else %}
  <font color="yellow">
{% endif %}
<b>{{ states('sensor.outdoor_fan_status') }}</b>
1 Like

@chairstacker your widget setup is inspiring, very cool and creative! Thank you so much for sharing it. The code examples were very helpful, and your post wonderfully highlights the limitations and possibilities of the custom template widget. :star_struck:

I’m completely new to home assistant, so could please explain where this custom service button script goes? Where do I insert this script? Maybe you could give me directions on how to get to that page in home assistant? Thank you!

Aww, this is very sad that home assistant doesn’t have buttons grouped into a single resizeable widget, as well as dynamic icons.
I hope soon they will add this feature. That would be really amazing

The script goes where all the scripts are - in the ‘Scripts’ tab of the ‘Automations & Scenes’ section of the ‘Settings’:

You might want to read up on the ‘Actionable Notifications’ - here’s the automation that toggles the switch for the terrace fan as a response to the pop-up notification on my phone:

alias: Action - Outdoor Fan Toggle Terrace
description: ""
trigger:
  - platform: event
    event_type: mobile_app_notification_action
    event_data:
      action: toggle_outdoor_fan_terrace
condition: []
action:
  - service: switch.toggle
    target:
      entity_id:
        - switch.sonoff_mini_02
    data: {}
mode: single

You can see that the action: toggle_outdoor_fan_terrace is the same in the script in my previous post and the automation above.

Hey @chairstacker ,

thank you for helping me out! I have managed to create the script and the automation. Wasn’t easy at first, as I’m just a beginner, but thanks to your help it was way easier than, if I tried to figure it out on my own.

Thank you so much! :star_struck:

1 Like

The only weird issue that I’ve found is that the notification is capped at three buttons. I have actually added four buttons, but only the first three are showing up. The fourth one doesn’t show up.

Do you know anything about that?

Never needed more than three - documentation doesn’t indicate a limitation either:
Actionable Notifications | Home Assistant Companion Docs (home-assistant.io)

@dshokouhi - is this a limitation on the app end or from the server side?

its a bit hidden but its mentioned under https://companion.home-assistant.io/docs/notifications/actionable-notifications/#building-actionable-notifications

android only allows 3 actions total in a notification

Duh - thanks, and I thought I’d read it thoroughly :frowning:

:thinking: is there any creative away guys you could think of putting more than the buttons into one widget button on Android? :thinking::thinking:

Hmmm… What about something like…
action 1
action 2
action 3
wait 3 seconds, then call script B if not clicked to show:
action 4
action 4

You can do that from the same script, I’d say.

1 Like

Can you please help me to create the script that waits x seconds, and flips the other notification, and clears the previous one, until the button is pressed?

This might actually be more complicated than I thought. Or is it?

The simplest way would be to add a delay between sending the first and the second actionable notification like this:

delay:
  hours: 0
  minutes: 0
  seconds: 3
  milliseconds: 0

Not sure how to remove a notification from a mobile device once it’s been sent - never needed it.

But I am sure that it gets much more complicated if you want to send the 2nd notification only if there was no reaction to the first one - certainly possible but beyond what I’ve done in the past.
Hint: look at the various options for ‘wait’ actions that are offered.

1 Like

Hmm, I was trying to find a way to remove the notification automatically. I tried the "ttl"key, but it didn’t do anything though. Does anyone know how to remove the notification automatically xx seconds after it was sent?