React to a motion sensor

React to a motion sensor

A wall panel is mostly looked at from across the room. A widget can show one big thing then, and more when someone walks up to it: the date under the clock, the coming hours under the weather. A motion sensor near the panel tells it when. The TileView widgets in the Community all work this way.

This page shows how to build that into a widget of your own: ask for the sensor in a setting, read it in the template, and let the style move between the two looks.

Ask for the sensor

Give the widget a value setting that takes only yes/no values:

{  "type": "value",  "name": "near",  "label": "Near sensor",  "help": "When it is on, someone is close: the details show.",  "required": false,  "kinds": ["reading", "variable"],  "types": ["boolean"]}

A value setting lets each tile pick any yes/no reading, not only a motion sensor’s: a presence sensor, a door contact, or a Homey variable a flow turns on and off. Leave it optional, so the widget still works on a tile without a sensor nearby.

If the widget also needs other readings of the sensor, such as its battery, ask for the device instead:

{  "type": "device",  "name": "hall",  "label": "Motion sensor",  "requires": { "all": ["alarm_motion"] }}

The template then reads settings.hall.cap.alarm_motion.value.

Read it in the template

settings.near is null while the tile has picked nothing, and otherwise holds the value with value and lastUpdated. Work out one name for the two looks and put it on the outside as a class:

{% let near = settings.near.value == true %}<div class="face{% if near %} near{% endif %}">  <hd-clock class="time" show="time"></hd-clock>  <div class="more">    <hd-clock class="date" show="date" date-style="long"></hd-clock>  </div></div>

Reading settings.near.value when nothing is picked gives nothing, and nothing is not true, so a tile without a sensor stays in its far look.

Keep it a little longer

A motion sensor turns off again after a short while, often within a minute, even when someone is still standing there reading. To keep the details up, count from the moment it last changed:

{% let near = settings.near.value == true or since(settings.near.lastUpdated, 'minutes') < 2 %}

When the sensor turns off, lastUpdated is that moment, so the details stay for two more minutes. A template that reads the clock with since() is drawn again every minute, so the details go by themselves, at most a minute after the two are up.

Let the style move

When the sensor changes, Tiledash draws the widget again, but it keeps the elements that are still there and only changes what is different. So a CSS transition on the near class runs as it does on any web page:

.time {  transition: scale var(--hd-motion-slow) var(--hd-ease-spring);}.near .time {  scale: 0.8;}.more > * {  opacity: 0;  translate: 0 8px;  transition:    opacity var(--hd-motion-base) var(--hd-ease-out),    translate var(--hd-motion-base) var(--hd-ease-out);}.near .more > * {  opacity: 1;  translate: 0 0;}.near .more > :nth-child(2) {  transition-delay: 60ms;}

Take the times from the theme’s --hd-motion-* and --hd-ease-* variables rather than writing your own. On a theme or screen without motion they are zero, so the change happens at once instead of being slow on a screen that can’t keep up. A few delays of 40 to 80 ms between the details make them rise one after the other.

Here it is as a widget on a motion sensor itself, reading the tile’s own device (cap.alarm_motion) instead of a setting, and without the two minutes, so you can see it move when you switch the sensor on and off:

near.liquid
{% let near = cap.alarm_motion.value == true %}<div class="face{% if near %} near{% endif %}">  <hd-clock class="time" show="time"></hd-clock>  <div class="more">    <hd-clock class="date" show="date" date-style="long"></hd-clock>    <p class="who">Someone is here</p>  </div></div>

Without a widget of your own

Some of this needs no template at all:

  • When to show on any tile can test a reading, so a tile appears only while the hall sensor sees motion.
  • A stack page can Open by itself when something starts to hold, so the stack turns to your controls when someone comes in.
  • A popup when something happens can open another dashboard when the sensor turns on. A sleeping wall panel wakes for it, so the panel shows a quiet face while nobody is there and your controls when someone walks up.

The TileView widgets

Tiledash’s own TileView widgets in the Community are made for a square wall panel and do all of the above: TileView Clock, Photos, Ambient and Weather each have a Near sensor setting, and Ambient brightens for a few seconds on a second Motion sensor. Copy to my widgets on one’s Community page makes a copy you can open in the editor to see how it is built (Make it yours), or start from the example above.