Post to Mastodon from Home Assistant

I wanted to post my daily solar generation statistics to my Mastodon account (bazcurtis@mastodon.social). I got some feedback after I did that. Some people pointed out that this was really a bot doing the posting and I should make a Mastodon bot account. I have made bazcurtissolar@mastodon.social

These are the steps I followed to get this working. It wasn’t very difficult, but did take some tinkering.

This is the Home Assistant documentation. Further down this post you will see my YAML code

These are the access rights the article talks about

Account access rights read:accounts and write:statuses

Mastodon API account access rights

Once you have made the new application we need to add some YAML to the Home Assistant configuration.yaml file. Obviously I can’t show it with the API keys. The key names in the documentation and Mastodon are slightly different. I am showing how they match up in the YAML example.

This is also where I had an issue. The documentation says the base_url is optional. At the time I was on masto.ai which meant I had to add it as I could never see it in services. Now I have moved to mastodon.social, I have still left it in.

The name can be whatever you want the notifier to be. It will be added to notify. In the case it will be notify.mastodon_notify

This is my configuration.yaml code

# Example configuration.yaml entry
notify:
  - name: mastodon_notify
    platform: mastodon
    access_token: Your access token
    client_id: Client key
    client_secret: Client secret
    base_url: https://mastodon.social

Once you have restarted Home Assistant, go to Developer Tools > services, and see if you can see the new notifier. Mine looks like this.

Home Assistant Mastodon notify service

Home Assistant Mastodon notify service

We now need to go and make the notification. In my case I want it to post the solar generation for the front and back panels and the total for the day.

Go to Settings > Automations & Scenes. Creat a new automation and switch to the YAML editor. This is my YAML code.

alias: Mastodon solar generation notification
description: Mastodon solar generation notification
trigger:
  - platform: time
    at: "20:00:00"
condition: []
action:
  - service: notify.mastodon_notify
    data:
      message: >
        East/South East (8x 400w panels) facing panels generated {{
        state_attr('sensor.luxpower','e_pv_2_day') }} kWh today

        West/North (6x 400w panels) facing panels generated {{
        state_attr('sensor.luxpower','e_pv_1_day') }} kWh today

        Total solar generated {{ states("sensor.lux_solar_output_daily") }} kWh
        today

        #SolarPanels #SolarGeneration
mode: single

Just in case the formatting above is a bit out, this is what it looks like.

Home Assistant YAML notification code

Home Assistant YAML notification code

You should now be good to go.

 
Michael Curtis

My introduction to computers started at my middle school in 1981 when our maths teacher brought in a ZX80. That led the computer club being founded and using a Research Machine 380Z

My first computer was a 48K ZX Spectrum which I loved to programme. Once I left school I worked as a photocopier engineer, then a fax engineer and finally moving on the Apple computers.

For the next 30 years I worked as a system administrator. I now work in the cyber security industry as a Sophos Professional Services consultant

https://www.bazmac.me
Previous
Previous

Broken Solar iBoost

Next
Next

Home Assistant Resources