Skip to content

Instantly share code, notes, and snippets.

@bphermansson
bphermansson / gist:b889b73372f24e853f0c3180647849b4
Created September 10, 2023 07:18
Template for Home Assistant to get prediction from the SMHI integration in swedish language
prediction:
friendly_name: "Weather prediction"
value_template: >-
{%- set state = states('weather.smhi_home') -%}
{% if state == 'clear-night' %} klart, natt
{% elif state == 'cloudy' %} molnigt
{% elif state == 'exceptional' %} exceptionellt
{% elif state == 'fog' %} dimma
{% elif state == 'hail' %} hagel
{% elif state == 'lightning' %} åska
@bphermansson
bphermansson / home_assistant_template
Last active February 16, 2023 17:55
Code snippet to get number of hours until the electricity is cheap(?).
{% set hour_when = as_timestamp(state_attr('binary_sensor.nordpool_planner_time_when_low_electricity_price', 'starts_at'))|int%}
{% set real_hour = (state_attr('binary_sensor.nordpool_planner_time_when_low_electricity_price', 'starts_at')) %}
Electricity is cheap at: {{ real_hour }}
{% set time_now = as_timestamp(now())|int%}
{% set hour_now = time_now|timestamp_custom('%H') %}
{% set hours_until = -((time_now - hour_when)|int/3600)|int %}
Hours left until cheap electricity: {{ hours_until }}
@bphermansson
bphermansson / PIA11_exercise2_phermansson
Created February 9, 2023 14:13
Code for my take on Exercise2
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
##############
class PersonAdapter(
# Code inspired by https://projects.raspberrypi.org/en/projects/physical-computing/11
from gpiozero import MotionSensor
import paho.mqtt.client as paho
import json
pir = MotionSensor(22)
mqtt_pir_topic = "magicmirror/pir"
broker="192.168.1.190"
client= paho.Client("MagicMirrorSensors")
#!/usr/bin/python
# Script that reads various I2C sensors.
# CCS811, BH1750, HTU21. (Air quality, light, temp & humidity).
# CCS811. "This part will measure eCO2 (equivalent calculated carbon-dioxide) concentration
# within a range of 400 to 8192 parts per million (ppm), and TVOC (Total Volatile Organic
# Compound) concentration within a range of 0 to 1187 parts per billion (ppb)."
# "The BH1750 provides 16-bit light measurements in lux"
@bphermansson
bphermansson / jsmqtt.html
Last active August 15, 2019 11:05
Code for the blog post at http://paheco.nu/?p=323
<!DOCTYPE html>
<head>
<title>Javascript MQTT test</title>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/paho-mqtt/1.0.1/mqttws31.min.js"></script>
</head>
<body>
<script>
// Connection settings
var broker = new Object();
broker.hostname = '192.168.1.190';
@bphermansson
bphermansson / pymqttpub.py
Last active August 15, 2019 08:48
Simple Python script that just publish a message to a MQTT-broker
import sys
import paho.mqtt.publish as publish
publish.single("mqttjs", "Hello from mqttjs", hostname="192.168.1.190")
// If using web sockets:
publish.single("mqttjs", "Hello from mqttjs", hostname="192.168.1.190", port=9001, transport='websockets')
@bphermansson
bphermansson / getVlcData.py
Created June 11, 2019 13:56
This script fetches information from the VLC media player. For this to work VLC has to be started with it's http-server activated.
#!/usr/bin/env python
'''
getVlcData.py
This script fetches information from the VLC media player.
For this to work VLC has to be started with it's http-server activated.
This can be done with a command line like this:
vlc --aout=alsa --alsa-audio-device="hw:0,0" --daemon --syslog -I http --http-port 8080 --http-password <password>
@bphermansson
bphermansson / sounddetector.py
Last active March 4, 2019 14:39
Detects sound playing on a Linuxsystem. Sends messages via Mqtt when sound starts and stops.
#!/usr/bin/env python
'''
Send a Mqtt message if system is playing sound, another if it's stopped
Put Mqtt server password in a separate file named "mqttpass.txt".
'''
import os, time, json, sys
import paho.mqtt.client as mqtt
MQTT_HOST = '192.168.1.79'
MQTT_PORT = 1883
@bphermansson
bphermansson / waitformqtt
Last active October 23, 2018 07:03
Script that waits for a service on another server
# Started by a Systemd script called 'waitformqtt.service':
#
# [Unit]
# Description=Checks if Mosquitto is running
# After=network-online.target
# [Service]
# User=rock64
# Type=oneshot
# ExecStart=/home/rock64/waitForMosquitto