A shiny new carl.saggs.co.uk

I’ve just completed the first major update to my personal profile, carl.saggs.co.uk, in the last couple of years.

Way back, in the days of MySpace, when emo music and dark, brooding designs were all the rage, my page bucked the trend with bright colours, cartoon graphics and light-hearted content in the form of my self-created hero – Kain Toad Man (Yep, it was indeed misspelt). My personal slices of the internet have followed much the same path ever since.

The web has obviously changed and matured a fair bit since then, and my day-to-day work sits far more in the serious, business-critical side of tech these days. There’s definitely a time and a place for the polished, corporate approach – and I have LinkedIn for that – but I still enjoy keeping some aspects of the earlier, more casual, fun internet alive and well. Be it IRC handles I refuse to move on from, or “Boz”, my long-term avatar. Boz himself first appeared as a recurring doodle (and was likely the bane of many of my high school teachers’ lives) and has since formed part of pretty much every avatar I’ve used, from old blogs and forums right through to my current work Slack profile.

At the end of the day, I love technology, solving tricky problems, and unpicking interesting code. Even when work is more serious, I’d never want it to feel like a slog. I enjoy working with people who are equally passionate about what they build and solve, and wherever I can, I want my presence, both professionally and not, to reflect that.

As such, with this update, I’ve attempted to cram as many little animations, interactions, and subtle easter eggs into the site as I could. Please give it a prod over on carl.saggs.co.uk and let me know what you think!

Screenshot of the original websiteScreenshot of the new website

By Carl on November 29th, 2026 in General

Building reactive datastore’s using delegated events in JavaScript

Every time I update my blog I end up coming back to this draft that I’ve never quite managed to polish up into something sensible before. It relates to an interesting pattern I was playing about with – terrifyingly now 5 or so years ago – that I still think is quite cool – even if the wider JS ecosystem has likely now moved past it.

Rather than leaving it sitting in my drafts forever, I thought I’d have a crack at tidying it up and sharing, as I do think there are still a couple of interesting properties, even if these do come with trade-offs.

What I was trying to do

At the time, I was tinkering with my own little Backbone-inspired library called lump.js. I’ve always liked Backbone’s delegated event pattern – being able to manage listeners centrally without constantly binding and unbinding them – and I wanted to see if I could combine that with a more modern reactive datastore using JavaScript Proxies.

The key aims being

  • Make it difficult to accidentally orphan references
  • Interact with the store as if it’s just a normal JavaScript object.
  • Use listeners to react to changes anywhere in the data structure (including for data that doesn’t exist yet).

Broadly I think the solution I came up with does a decent enough job at managing all these goals – even if it does have a few pitfalls I’ll mention later.

Basic usage is – you make a “model” with whatever data you want in it. Then, you can use it just like a normal JS object – changing values, or even entire portions while the events fire so you can listen for these changes.

const data = Model({
    players: [{ id: 1, name: 'Steve', location: [23, 15] }]
});

// Listen for new players being added
data.on('create:players.*', (player) => {
    // Listen for changes on the sub-model itself
    player.on('update:name', (name) => console.log(`My name is now ${name}`));
    player.on('update:location[0]', (x) => console.log(`My x coord is now ${x}`));
    player.on('update:location.1', (y) => console.log(`My y coord is now ${y}`));
    player.on('update:location', (coords) => console.log('My location got updated'));
});

data.players.push({ id: 2, name: 'Ralph' });

Due to the way it works, there’s no risk of orphaning an object (e.g. the player) even if you go and replace big chunks of the object.

const test = Model({
    name: 'bob',
    abilities: {
        jump: { stats: 1 },
        duck: { stats: 2 }
    }
});

let jump = test.abilities.jump;

// Completely replace the parent object
test.abilities = {
    jump: { stats: 5 },
    duck: { stats: 2 },
    other: { stats: 3 }
};

console.log(jump.stats); // now = 5

test.abilities.jump.stats = 10;
console.log(jump.stats); // now = 10

jump.stats = 11;
console.log(test.abilities.jump.stats); // and it's now 11

Normally with something like this, you’d expect replacing test.abilities like that would leave jump pointing at the old object in memory, detached from the store, and any listeners on it would quietly stop firing. Something this setup avoids, letting jump stay in sync and all it’s listeners carry on working as normal. If that data gets deleted entirely, reading from jump just gives you undefined back instead of stale data. It then starts working again as soon as it’s recreated.

That’s all very well but how does it work?

The approach works by setting up two separate Proxy objects: a Data Proxy (to hold information) and an Access Proxy (to read it). The Data Proxy wraps the actual data in the store. It’s job is to intercept changes and compare the affected path against an internal copy of the previous state, then fire the right create, update, or remove events.

Whenever you read an object out of the store, however, you don’t get the Data Proxy directly. Instead, it gets wrapped in the second proxy – the Access Proxy. The Access Proxy is more or less a wrapper around a data path (for example, abilities.jump). Any reads, writes, or .on() calls you make against it are simply routed back to the real store at that path.

Because of this, if your code holds onto let jump = test.abilities.jump, it isn’t really holding a reference to that specific object, instead it’s holding an Access Proxy pointing at abilities.jump. Even if the entire abilities object gets swapped out , jump.stats still resolves against the store at abilities.jump.stats under the hood, so nothing goes out of sync.

To properly put this all through it’s paces, I dogfooded the library heavily against another little experiment at the time RPGQuickEncounter – a stand alone browser based tool for running encounters for tabletop RPG’s like D&D over slack & zoom (Something necessitated by the Covid lockdowns at the time.)

The game effectively lets near instantly setup an encounter on an arbitrary map image, create and manage characters, fog of war, custom icons and all that fun – which all persists nicely into local storage.

This provided a nice way to see how the pattern felt to use in practice – the sorts of things it allowed you to stop worrying about, as well as the things it added you now need to. If you want to have a poke around the code (and the test suite alongside it), the full source for the model is up on GitHub in lump.js/src/model.js. Overall I actually think it worked pretty well – given the code is all hand rolled and libraryless – having components subscribe to paths in the model – without needing to worry about whether a character was edited one field at a time or overwritten wholesale from saved state – kept things clean and easy to reason about – even as the complexity started to grow.

Even some of the quirks with arrays (e.g. if an access proxy ref referenced player[0], and the array got shifted to add a new one in that position – all the refs would now be pointed to that) were actually pretty easy to work with – for example as players were all rendered based on their property, swapping index’s didn’t actually make any odds as positions/data were all preserved.

Great, should we all swap over?

Err, no. While pretty cool (in my opinion), the world has moved on a tad in the past 5 years. Lump was only ever experimental code and never really designed for proper production use and libraries like Vue/Solid have all adopted Proxies in their own way to fit their usage patterns. There’s now even a proper pattern name for parts of this approach – Proxy-backed Cursor – which can be seen in libraries like Legend-state.

If someone ever did want to push this further, there are a few rough edges that need some focus

  • Arrays/shifting indexes – While this actually caused me less issues that I expected once I was used to it, its not an intuitive behaviour and I’m sure could cause all sorts of fun issues if used in production. Figuring out a nice balance between immutable path references, and tracking array index’s in an intuitive way is probably the biggest challenge with this sort of approach.
  • Method collisions – As get, set, on, off are intercepted, these can’t be used a normal attributes. This is probably fixable, but something that’d need a bit of thought
  • Overheads – The diff approach to detecting changes requires the full tree be held twice – not an issue for a small library, but in a bigger complex system this could become a bottleneck.

Whatever the case, while I don’t think any revolution in how people use proxies and manage objects in JS is afoot, it was a fun approach to experiment with and provided a decent developer UX in use. The concept of separating accessing data from storing data is fairly neat trick – and honestly for a tiny self built app – I’d be quite tempted to use it again, even with all its quirks. Who knows, coming back to it fresh I may even solve a few of them.

Home Assistant: Exploring location history

If you’ve managed to set up a good source of location data in Home Assistant, then there is a good chance you will have tried and been a bit disappointed by the out of the box MapCard. While it can get your trackers showing on a map, when it comes with digging into the history data the functionality is painfully limited.

As a big user the rich capabilities available in other areas of Home Assistant, such as the energy dashboards, I set out to find a way of getting similar capabilities for location data on maps as well.

One of the great things with Home Assistant, and Open source projects in general, is that when you do find a gap like this you have the chance to get involved and solve your own itch. As someone with very limited experience with Home Assistants frontend codebase, I decided my best bet was find someone who’d created a HACS Map Card already, and see if I could use that as the basis to build out my Wishlist

As luck would have it @nathan_gs‘s ha-map-card perfectly fitted the bill.

Rather than jumping straight into the deep end with and trying to make the date picker work, I initially started off picking up some more standard features and bugs from the Github issues. I find this is a good way to get a feel for a new codebase and its conventions. The use of web components and the Lit library were all new to me, so despite my best efforts I did have to read through a little documentation as well.

Fortunately, Nathan turned out to be incredibly receptive to the random features I started throwing his way, which is always a huge encouragement when you start tinkering with something new.

Getting date ranges working

Once I felt I had a basic handle on the codebase, I decided to go after my main wish list feature. Integrating with Home assistants built in energy-date-selection component. The component itself is great, providing the ability to easily navigate history, select date ranges and more. Right now its very much tied to Home Assistants energy dashboard – although I hope at some point in the future Home Assistant choose to make it available as a more general feature.

Despite it being meant for the energy dashboards, as the screenshot above shows, it turns out it can indeed be used to drive other features. Most notably the map history.

I knew setting out that hooking into the date selection card must be possible as I already used a number of Home Assistant Cards that do so, the awesome ha-sankey-chart for one. Inspired by the Sankey Chart Card approach, I followed the model to create a “DateRangeService” that would take responsibility for waiting for the Home Assistant _energy connection to be ready, then manage the subscription to it once it was. I was then able to use the service to register call-backs in the card, allowing each entity to trigger a redraw of history data whenever the date range was changed.

Following on from the initial prototype, I ended up cranking out a number of minor features for the Map Card over the last week or two, further exploring some of home assistants internal capabilities.

Highlights include

  • The ability to have card level history date ranges and optionally override these individually for each entity.
  • The ability to use external entities to provide history date range values (For example an input_number to drive the hours or days ago.) This provides an alternative mechanism for exploring the history data. You can even mix and match them.
  • Adding more detail to the tooltips and entities themselves, including handling the click events and triggering home-assistant modals.

In Summary

Although still fairly new, I think the ha-map-card represents a pretty monumental improvement to home-assistants mapping and location data exploration capabilities. It provides significantly greater control over how location information is used and displayed, as well as several other advanced features including the ability to configure custom tile layers and web mapping services.

For people looking to get more location data into tehri Home Assistant, if you are a google maps user I’d recommend installing pnbruckner/ha-google-maps via HACs as a nicer way to setup access to your location history.

If you would like to set up a page similar to my screenshot does, you’ll need to install card-mod (to add a little extra CSS) then setup a new tab with the “Panel (1 Card view)”.

My YAML for the card then looks roughly as follows

type: grid
square: false
cards:
  - type: energy-date-selection
    card_mod:
      style: |
        ha-card {
              z-index: 1;
              position: absolute;
              height: 80px !important;
              width: 80%;
              top: 75px;
              right: 10px;
              max-width: 450px;
        }
  - type: custom:map-card
    history_date_selection: true
    card_size: 16
    entities:
      - entity: device_tracker.your_entity_1
      - entity: device_tracker.your_entity_2
      - entity: device_tracker.your_entity_3
      ...
    card_mod:
      style: |
        ha-card {
          margin-top: -10px;
        }
        ha-card #map {
          height:calc(100vh - 56px);
          border-radius:0;
        }
columns: 1

Not perfect, but hopefully a nice starting point for a more easily explorable map.

Final thoughts

Having now worked on this and another simple card, I have to say I’ve quite enjoyed the experience of developing in the Home Assistant ecosystem.

That said, the learning curve was somewhat steeper than I expected, simply due to the difficulty in finding any definitive docs on how to go about most things. While there is a boilerplate card and a lot of info filed away in forum posts, I ended up spending the majority of my time reading the source of other cards in order work out how achieved things. Looking back, a lot of it now seems pretty obvious, but that was definitely not the case when I first started poking around.

Some of this may well just be me being rusty, I’ve primarily been an API developer the last few years, so my familiarly with things like Lit and TypeScript were minimal. (Although I did manage to sidestep the Typescript for now at least).

It did also seems a bit odd that so far as I can tell, there is no proper way to ask Home Assistant to load built in card components without needing to have Home Assistants own card on the page to do so. As a result you end up having to reinvent wheels, despite there being a perfectly good component in Home Assistant already.

All that said, after a bit of effort I managed to get my head around the basics and build out some features I’ve been desperate for for ages. You can’t really ask for anything more than that.

Updates

  • 2024-12-20: Updated the code example with some changes to work with the latest home-assistant version. Includes logic to make the map full height.

Make use of Octopus Power-ups using a Solis Hybrid inverter.

Octopus energy recently announced they would be offering Power-ups; short periods of free electricity to customers in certain areas.

As one of the lucky people living in a Power-ups area, I wanted to quickly put together some basic notes on how to configure a Solis Hybrid Inverter to charge from the grid during these times. See https://octopus.energy/power-ups/ to find out more about the scheme.

Note: This is all just my personal thoughts. I’m not an expert and information may be inaccurate so listen to me at your own risk. If you are not comfortable changing these settings – don’t. I’m fairly sure you could break your install or worse by incorrectly changing settings within these menus.

While everything mentioned here can be managed directly on the Inverter, the steps below our done using the remote management tool on Solis Cloud. If you have not already, you will need to manually request access to this at https://solis-service.solisinverters.com/en/support/solutions/articles/44002373796-inverter-remote-control-application

Once approved, you will likely need to log in and out again to see the new buttons.

Setting the Inverter to charge from grid during the Power-up.

To start, enter the management console. From the plant overview screen select your plant (there should normally just be the one).

In the left hand menu you should then see a tab labelled “Device”, click this and you should see a list of inverts. Select the inverter serial number to view the Inverter itself

Then in the top right you should see the following buttons. Select “Inverter control”.

At this point you will need to re-enter your password and agree to the warning.

Now you should see a Control panel similar to the following.

To start select Work Mode, then select Self-consumption from the options avaiable.

You should then see a screen that looks like the below.

First, confirm self-use mode switch is on by clicking into Self-Use Mode Switch and confirming current value is “enable”.

Next enter Charge and Discharge.

Here I set charging current1 to 50Amp (the default), leaving discharge as 0 (as I don’t want to export). I also set the charging time as the period for the upcoming “Power up”. In my case 2pm till 4pm.

Unfortunately you can’t schedule this in advance so you’ll have to set this on the day, then turn it back off again afterwards or you risk it charging the battery on days when grid power is noticeably less free.

Now that is done, you can click in to Time of use switch and enable it. This pretty much just tells the inverter listen to the charging/discharging times you selected previously. These are ignored once this is turned back off.

Finally, select Allow Grid Charging, and enable it.

And then it is just a case of waiting for the “Power up” to start.

Assuming all is well your battery should begin to charge at around 2.5kW. This will first use any solar generation you have (as this goes direct to the battery), and then top’s up the rest from either spare power capacity in your home or the grid.

That said….

Please do remember to disable the “Time of use switch” and potentially “Allow grid charging” options after the power up is complete. If you forget you will end up paying for the grid import the next day when the defined charging slot comes around again.

A few more notes.

  • Everything described here was tested on my own solar “plant”, a 3.4kW Solis Hybrid Inverter with 2 Us3000c PylonTech batteries.
  • My home setup was previously in its default “self use” mode state after it’s installation.

I did find two particularly interesting things that were not originally clear to me when setting this up.

  1. While in self use mode, even with allow grid charging on – your system will not charge from the grid unless its during a charging period (configured in the charge and discharge settings).
  2. Better still – the charge from grid settings naming is slightly misleading. What it actually appears to mean is “Allow inverter to charge battery from an AC source”.

Why is this interesting?

Well, if you happen to have a second solar setup in your home (like I do from an older install), enabling this will actually allow your battery to charge using the spare AC capacity generated by the system, while not touching anything from the Grid itself. I’m fairly sure this only works due to my hybrid inverters CT clamp being on the main power from the house, allowing it to detect if there is spare capacity that’s currently being exported. I assume this is the standard approach, but best to confirm if you are unsure.

Its worth noting that the same configuration options could also be used to set a non “Power up” based charging schedule (given that is what they are actually intended for), for example if you get cheap power over night or similar. Given I don’t get that, the “Power up” was my first time touching any of these options. As such, I thought someone out there may find it useful to have this all written down, given the manual wasn’t quite as clear as I would have preferred

Thanks for reading.

That’s pretty much all there is to it.

Let me know if this was helpful or if I missed anything that’d be good to know.

Larahook: Hooks for Laravel

In SaaS or similar applications, there often comes a point when you want to start adding custom or tweaked functionality for clients without the codebase itself diverging. One of the most common solutions to this is to use hooks.

Hooks effectively allow you to make certain parts of your applications functionality open to modification by another part. My own usage of this for instance is allowing a single white label configuration file to adjust (in some cases quite deeply) how the larger application works. WordPress is probably the most well known for using this approach in order to allow its plugins and themes to easily interact with WordPress’s functionalty, without needing to make any changes WordPress’s own code.

To support this functionally in a Laravel application, for the last few years I have been using the wonderful esemve/Hook library by Bence Kádár. Unfortunately the library now appears to be mostly inactive – most critically lacking support for Laravel 8 (as well as PHP8 itself in some areas).

As such, with an growing requirement for additional functionality and updates, I decided to take the plunge and create a new maintained fork of the library.

Github profile for the coinvestor/larahook library.

Although for simple use cases coinvestor/larahook can be used as a drop in replacement for the original esemve/hook, I utilised the opportunity of it being a clean break to make some more involved changes to the libraries functionality.

The most important changes are listed below.

Laravel 8 and PHP 8 compatibility plus auto-discovery support.

The library has been updated to work with the latest version of Laraval, as well as to make use of the newer package auto-discovery features meaning you will no longer need to update your app.php directly.

Retired the initial content parameter, and replaced it with $useCallbackAsFirstListener.

In the original version of the library, you needed to specify both a call-back (to run when the hook was not being listened to) as well as optionally a default $output value to be passed in as the 4th parameter to the get hook method. This lead to some confusing code where a hook would need to invoke the original call-back directly to get a default value (where output was null), but if a second hook had run previously, may instead include data in the $output the hook would need to be aware of.

To simplify this I changed the 4th parameter on get to instead be a Boolean called $useCallbackAsFirstListener, as such by setting this as true, the hooks default callback will always run and pass its value in as the $output value for every subsequent listener. As such listener logic can be simplified to always expect to be working with the value of output. For now this option is left as false, as in the case where the default hook is taking an action (say sending an email) this behaviour would not be desired, and so for safety must be specifically enabled.

Listeners at the same hook at the same priority will no longer overwrite each other.

Unlike the original version of the library, lara-hook will allow multiple hooks to be registered on the same event at the same priority level. In the case this happens the hook listeners will be run in the order they are registered.

If you are making use of the original functionality where hooks at the same priority overwrite each other, code changes will be needed.

Support for falsely return values.

In our application there were a number of cases where we’d wanted a hook listener to return a falsey value back to an underlying function. This was previously not possible, as a hook returning false would trigger an abort – causing the hook to return the default value (rather than the falsey value itself).

This is no longer the case in larahook, meaning the Hook:stop(); method will now have to be used directly where a hook does need to abort, as 0/false/nulls will simply be returned from the hook like any other value.

New Methods: getListeners, removeListener and removeListeners.

Listeners can now be unregistered, both individually as well as all listeners for a specific hook.

Additionally a full list of listeners on a hook can be returned using the getListeners method.

Test and bugfixes.

In addition to the functionality changes, I also spent some time adding unit tests and basic CI for the library. As is always the case when adding tests I managed to find and fix a number of minor bugs and edge cases across the library.

If you’d like to swap over to the new library, please have a look at our repo at https://github.com/CoInvestor/larahook/